简体   繁体   English

单击listview上的项目时如何刷新列表视图

[英]How to refresh listview when click an item on listview

I have a custom adapter extends from arrayadapter: 我从arrayadapter扩展了自定义适配器:

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

    final ViewHolder holder;

    if (convertView == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.list_item, null);

        holder = new ViewHolder();
        holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
        holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
        holder.ivItem = (ImageView) convertView.findViewById(R.id.ivItem);
        holder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
        holder.ivArrow = (ImageView) convertView.findViewById(R.id.ivArrow);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.tvName.setText(item.get(position).getName());
    holder.tvPrice.setText(item.get(position).getPrice());
    holder.ivItem.setImageBitmap(item.get(position).getAvatar());
    holder.ivArrow.setImageDrawable(item.get(position).getArrow());
    holder.ivIcon.setImageDrawable(item.get(position).getIcon());


            holder.ivIcon.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

        /*...My code...*/
    }
    });

    return convertView;
}

I try to click on item ivIcon , a row of listview will be removed and listview should refresh, but I don't have way to refresh listview. 我尝试点击项目ivIcon ,将删除一行listview并刷新listview ,但我没有办法刷新listview。 I need help 我需要帮助

Your choices are: 你的选择是:

  • Use the functions of the ArrayAdapter to modify the underlying List (add, insert, remove, clear, etc.) 使用ArrayAdapter的功能修改基础List(添加,插入,删除,清除等)
  • Re-create the ArrayAdapter with the new List data. 使用新的List数据重新创建ArrayAdapter。 (Uses a lot of resources and garbage collection.) (使用大量资源和垃圾收集。)
  • Create your own class derived from BaseAdapter and ListAdapter that allows changing of the underlying List data structure. 创建自己的派生自BaseAdapter和ListAdapter的类,允许更改基础List数据结构。
  • Use the notifyDataSetChanged every time the list is update. 每次更新列表时使用notifyDataSetChanged。 To call it on the UI-Thread use the runOnUiThread method of the Activity. 要在UI-Thread上调用它,请使用Activity的runOnUiThread方法。 Then notifyDataSetChanged will work. 然后notifyDataSetChanged将工作。

Example

final ArrayAdapter adapter = ((ArrayAdapter)getListAdapter());
runOnUiThread(new Runnable() {

    public void run() {
        adapter.notifyDataSetChanged();
    }
});

This code will help you. 这段代码可以帮到你。

Suppose that your have adapter like 假设你的适配器就像

 ListView = listview;
 customListView mcustomListView = new customListView(this);

when you click on list item in getView 当您单击getView中的列表项时

 holder.ivIcon.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

         customListView  mcustomListView = (customListView ) mlvItem.getAdapter();
mcustomListView .notifyDataSetChanged();

      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM