0

Hi i have a tab activity which show a list of items(ListView). This listview has a button in each row. I am handling this button clickListener in my custom adapter class which extends the BaseAdapter class. Now what i want is when i click on this button the item should be remove from the listview and listview should get a refresh. How to do this?

ok I think my code can explain it

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

    final ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);

    if(convertView==null) 
    {
             convertView = mInflater.inflate(R.layout.list_item, null);
             holder = new ViewHolder();
 //     holder.appSize = (TextView)convertView.findViewById(R.id.app_size);

              holder.appName = (TextView)convertView.findViewById(R.id.app_name);
              holder.app_icon = (ImageView)convertView.findViewById(R.id.app_icon);
              holder.button = (Button)convertView.findViewById(R.id.uninstall);
              if(kill_OR_uninstall)
              {

                       holder.button.setOnClickListener(new OnClickListener() {

                            public void onClick(View v) {

                       Intent intent = new Intent(Intent.ACTION_DELETE);
                       intent.setData(Uri.parse("package:"+package_names.get(position)));
                       context.startActivity(intent);



                     }
                    });
              }

              else
              {
                       holder.button.setText("End");
                       holder.button.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
                        am.restartPackage(package_names.get(position));

                        }
                    });

              }

      convertView.setTag(holder);

    }

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

     holder.appName.setText((String)app_details.get(position).get(APP_NAME));
     holder.app_icon.setImageDrawable((Drawable)app_details.get(position).get(APP_ICON));
  //   holder.appSize.setText(app_details.get(position).get(APP_SIZE)+ " KB");



     return convertView;

}

Sunny
  • 14,522
  • 15
  • 84
  • 129

3 Answers3

1

Based on the position of the row that was click remove an item from the Collection that is backing the Adapter example :

myArrayList.remove(position);

then notify the list that the data is changed using :

adapter.notifyDataSetChanged();
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
1

You can do it. First in your action listener of your button, update your adapter of list view(i mean add/remove the elements that you dont want from your adapter) and you need to call urlistview.notifydatasetchange(true). this will refresh your list view

Arun
  • 1,658
  • 1
  • 14
  • 21
0

Your code may help in getting exact answer, but high level this is what you may need to do, you need to delete the selected entry from the list of values you are passing while instantiating your custom adapter class. On First attempt, your selected entry will be empty, so you don't need to delete anything.

kosa
  • 65,990
  • 13
  • 130
  • 167