在喜欢新闻项目后,心形按钮显示为红色,计数器设置为1,但在上下滚动后,计数器返回到零,心脏按钮从服务器加载时从红色变为灰色。我尝试使用notifyDataSetChanged()但在适配器中没有工作....下面是我实现的代码。
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) {
//
}
}
}
答案 0 :(得分:1)
resultp = data.get(position);
应该在if else循环之外。
答案 1 :(得分:0)
您需要遵循ViewHolder模式。
在setTag
上致电itemView
,在设置您的持有人项目后传递viewHolder
。请在关闭大括号之前查看最后一行:
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);
itemView.setTag(holder);
}
这意味着当你到达 p>行
holder = (ViewHolder) itemView.getTag();
getTag
会返回您设置为标记的holder
对象,因此当View
不为空时,它可以正确回收View
。
您还应该更新适配器中的getItem
以返回正确的对象。
@Override
public Object getItem(int position) {
return data.get(position);
}