rocky
rocky

Reputation: 339

How to refresh listview when click an item on listview

I have a custom adapter extends from 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. I need help

Upvotes: 1

Views: 9624

Answers (2)

samsad
samsad

Reputation: 1241

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

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

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

      }
  }

Upvotes: 0

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

Your choices are:

  • Use the functions of the ArrayAdapter to modify the underlying List (add, insert, remove, clear, etc.)
  • Re-create the ArrayAdapter with the new List data. (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.
  • Use the notifyDataSetChanged every time the list is update. To call it on the UI-Thread use the runOnUiThread method of the Activity. Then notifyDataSetChanged will work.

Example

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

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

Upvotes: 3

Related Questions