Nachdem eine Nachricht gefallen ist, zeigt die Herz-Taste Rot und Zähler auf 1, aber nach dem Auf- und Abwärtsscrollen geht der Zähler auf Null und die Herz-Taste zurück wechselt von Rot zu Asche, wie vom Server geladen. Ich habe versucht, mit notifyDataSetChanged(), aber nicht im Adapter funktioniert .... unten ist der Code ich implementiert.Wie Knopf und ähnlicher Zähler nach dem Auf- und Abwärtsscrollen in der Listenansicht
public class ListViewAdapterSup extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapterSup(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
static class ViewHolder {
// Declare Variables
TextView supmessageTV;
ImageView ImageLike;
ImageView ImageComment;
}
ViewHolder holder;
public View getView(final int position, View itemView, ViewGroup parent) {
itemView = null;
if (itemView == null) {
//itemView.setTag();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.sups_list_item, parent, false);
// Get the position
resultp = data.get(position);
holder = new ViewHolder();
// Locate the TextViews in listview_item.xml
holder.messageTV = (TextView) itemView.findViewById(R.id.status);
// Locate the ImageView in listview_item.xml
holder.ImageLike = (ImageView) itemView.findViewById(R.id.likeImage);
holder.ImageComment = (ImageView) itemView.findViewById(R.id.ImageComment);
holder.viewLikesImageClick = (ImageView) itemView.findViewById(R.id.imageVieew);
}
else{
holder = (ViewHolder) itemView.getTag();
}
// Capture position and set results to the TextViews
holder.messageTV.setText(resultp.get(News.MESSAGE));
holder.likesCountTV.setText(resultp.get(News.LIKES_COUNT));
holder.commentsCountTV.setText(resultp.get(News.COMMENTS_COUNT));
holder.ImageLike.setTag(holder);
holder.ImageLike.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
holder = (ViewHolder)view.getTag();
resultp = data.get(position);
if(holder.ImageLike.getDrawable().getConstantState().equals(context.getResources().getDrawable(R.drawable.ic_favorite_white_24dp).getConstantState())){
resultp = data.get(position);
ConnectionDetector cd = new ConnectionDetector(context);
if (!cd.isConnectingToInternet()) {
Toast.makeText(context,"No internet connection",Toast.LENGTH_SHORT).show();
}
else
{
new LikeNews().execute();
String number = holder.likesCountTV.getText().toString();
int num = Integer.parseInt(number);
int nene = num + 1;
String str = String.valueOf(nene);
holder.likesCountTV.setText(str);
//holder.likeBTN.setText("LIKED");
holder.ImageLike.setImageResource(R.drawable.ic_favorite_red_24dp);
}
}
else if(holder.ImageLike.getDrawable().getConstantState().equals(context.getResources().getDrawable(R.drawable.ic_favorite_red_24dp).getConstantState())){
resultp = data.get(position);
ConnectionDetector cd = new ConnectionDetector(context);
if (!cd.isConnectingToInternet()) {
Toast.makeText(context,"No internet connection",Toast.LENGTH_SHORT).show();
}
else
{
new UnLikeNews().execute();
String number = holder.likesCountTV.getText().toString();
int num = Integer.parseInt(number);
int nene = num - 1;
String str = String.valueOf(nene);
holder.likesCountTV.setText(str);
// holder.likeBTN.setText("Like");
holder.ImageLike.setImageResource(R.drawable.ic_favorite_white_24dp);
}
}
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder = (ViewHolder)view.getTag();
resultp = data.get(position);
if(holder.ImageLike.getDrawable().getConstantState().equals(holder.ImageLike.getContext().getDrawable(R.drawable.ic_favorite_white_24dp).getConstantState())){
resultp = data.get(position);
ConnectionDetector cd = new ConnectionDetector(context);
if (!cd.isConnectingToInternet()) {
Toast.makeText(context,"No internet connection",Toast.LENGTH_SHORT).show();
}
else
{
new LikeNews().execute();
String number = holder.likesCountTV.getText().toString();
int num = Integer.parseInt(number);
int nene = num + 1;
String str = String.valueOf(nene);
holder.likesCountTV.setText(str);
holder.ImageLike.setImageResource(R.drawable.ic_favorite_red_24dp);
}
}
else if(holder.ImageLike.getDrawable().getConstantState().equals(holder.ImageLike.getContext().getDrawable(R.drawable.ic_favorite_red_24dp).getConstantState())){
resultp = data.get(position);
ConnectionDetector cd = new ConnectionDetector(context);
if (!cd.isConnectingToInternet()) {
Toast.makeText(context,"No internet connection",Toast.LENGTH_SHORT).show();
}
else
{
new UnLikeNews().execute();
String number = holder.likesCountTV.getText().toString();
int num = Integer.parseInt(number);
int nene = num - 1;
String str = String.valueOf(nene);
holder.likesCountTV.setText(str);
holder.ImageLike.setImageResource(R.drawable.ic_favorite_white_24dp);
}
}
}
}
});
return itemView;
}
public class LikeNews extends AsyncTask<String, String, String> {
boolean hasUserLiked = false;
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("www.example.com");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "01"));
nameValuePairs.add(new BasicNameValuePair("username", user));
nameValuePairs.add(new BasicNameValuePair("user_involved", "mark");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
hasUserLiked = true;
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String lenghtOfFile) {
}
}
public class UnLikeNews extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("www.example.com");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "01"));
nameValuePairs.add(new BasicNameValuePair("username", user));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String lenghtOfFile) {
//
}
}
}
ich habe es ausprobiert und es hat nicht meine bearbeiten –
Bitte sehen und sehen Sie arbeiten, wenn es –
hilft habe ich versucht es und es immer noch nicht funktioniert: ( –