单击图像按钮时从适配器更新ListView

时间:2014-09-03 09:01:30

标签: android listview

我在社交网络应用中有List,当用户点击按钮时,我需要增加喜欢的数量。enter image description here

适配器内的onclicklistener,我正在重新加载整个列表,这使得列表在重新加载时返回顶部,我必须让它保持在同一个帖子,以便用户看到相同的帖子不是列表的顶部我的适配器代码就像这样:

public class PaListAdapter extends ArrayAdapter<GroupPostValues> {

public PaListAdapter(Context context, int resource) {
    super(context, resource);

}

public View getView(int position, View convertView, ViewGroup parent) {

    ImageView likeBtn = (ImageView) convertView
            .findViewById(R.id.likeImage);

    likeBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // some asyncTask to call web servic that will increase likes

        }
    });


    return convertView;
}

}  。当我通过intent传递此项的位置并重新加载片段制作列表lv.setSelectionFromTop(positionReceivedFromIntent,0);这不是达到同一个帖子,我做得对吗?

2 个答案:

答案 0 :(得分:1)

您无需调用notifyDataSetChanged()。

尝试以下代码:

public class Adapter extends BaseAdapter {

public LayoutInflater inflater;
public ArrayList<ListData> listObjects;

public Adapter(Context context, int textViewResourceId,
        ArrayList<ListData> objects) {

    super();
    this.inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.listObjects = objects;

}

public static class ViewHolder {
    TextView tvTotalLikes, tvLike;
}

public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder vHolder;

    if (convertView == null) {

        vHolder = new ViewHolder();

        convertView = inflater.inflate(R.layout.list, null);
        vHolder.tvTotalLikes = (TextView) convertView.findViewById(R.id.tvTotalLikes);
        vHolder.tvLike = (TextView) convertView.findViewById(R.id.tvLike);

        convertView.setTag(vHolder);

        vHolder.tvLike.setTag(listObjects.get(position));

        vHolder.tvLike.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                ListData data = (ListData) v.getTag();

                 if(!data.isAlreadyLiked()){
                    data.setLikes(data.getLikes() + 1);
                    data.setAlreadyLiked(true);
                    vHolder.tvTotalLikes.setText("" + data.getLikes());
                  }
            }
        });

    } else {
        vHolder = (ViewHolder) convertView.getTag();
        vHolder.tvLike.setTag(listObjects.get(position));

    }
    vHolder.tvTotalLikes.setText("" + listObjects.get(position).getLikes());

    return convertView;
}

public int getCount() {

    return listObjects.size();
}

public Object getItem(int position) {

    return listObjects.get(position);
}

public long getItemId(int position) {
    return position;
}

}

答案 1 :(得分:0)

更改getView方法。将notifyDataSetChanged();添加到您的onClick方法中:
不要手动更新列表或刷新UI。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final int pos = position;
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.list_item, parent, false);
    }

...
likeBtn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // some asyncTask to call web servic that will increase likes
        ...
        notifyDataSetChanged();
    }
});
...