0

am displaying names in list view, with check boxes,here i have button if i click that button that time i need to remove selected items and from list view, remain items only i need to show in list view,using below i code i can find what are the items i selected but i dont know how to remove those, can you any one suggest me

MainActivity.class: 
   public class MainActivity extends Activity {

 private ListView listview;  
 ArrayList<String> items = new ArrayList<String>();  
 private int count;  
 private boolean[] thumbnailsselection;  
 @Override  
 protected void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);  
      setContentView(R.layout.activity_main);  
      fillarray();  
      count = items.size();  
      thumbnailsselection = new boolean[count];  
      listview = (ListView) findViewById(R.id.listView1);  
      listview.setAdapter(new ImageAdapter(MainActivity.this));  
 }  
 private void fillarray() {  
      // TODO Auto-generated method stub  
      items.clear();  
      items.add("Android alpha");  
      items.add("Android beta");  
      items.add("1.5 Cupcake (API level 3)");  
      items.add("1.6 Donut (API level 4)");  
      items.add("2.0 Eclair (API level 5)");  
      items.add("2.0.1 Eclair (API level 6)");  

 }  
 @Override  
 public boolean onCreateOptionsMenu(Menu menu) {  
      // Inflate the menu; this adds items to the action bar if it is present.  
      getMenuInflater().inflate(R.menu.activity_main, menu);  
      return true;  
 }  
 public class ImageAdapter extends BaseAdapter {  
      private LayoutInflater mInflater;  
      private Context mContext;  
      public ImageAdapter(Context context) {  
           mContext = context;  
      }  
      public int getCount() {  
           return count;  
      }  
      public Object getItem(int position) {  
           return position;  
      }  
      public long getItemId(int position) {  
           return position;  
      }  
      public View getView(int position, View convertView, ViewGroup parent) {  
           ViewHolder holder;  
           if (convertView == null) {  
                holder = new ViewHolder();  
                convertView = LayoutInflater.from(mContext).inflate(  
                          R.layout.row_photo, null);  
                holder.textview = (TextView) convertView  
                          .findViewById(R.id.thumbImage);  
                holder.checkbox = (CheckBox) convertView  
                          .findViewById(R.id.itemCheckBox);  
                convertView.setTag(holder);  
           } else {  
                holder = (ViewHolder) convertView.getTag();  
           }  
           holder.checkbox.setId(position);  
           holder.textview.setId(position);  
           holder.checkbox.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                     // TODO Auto-generated method stub  
                     CheckBox cb = (CheckBox) v;  
                     int id = cb.getId();  
                     if (thumbnailsselection[id]) {  
                          cb.setChecked(false);  
                          thumbnailsselection[id] = false;  
                     } else {  
                          cb.setChecked(true);  
                          thumbnailsselection[id] = true;  
                     }  
                }  
           });  
           holder.textview.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                     // TODO Auto-generated method stub  
                     int id = v.getId();  
                }  
           });  
           holder.textview.setText(items.get(position));  
           holder.checkbox.setChecked(thumbnailsselection[position]);  
           holder.id = position;  
           return convertView;  
      }  

    public void removeItems()
      {
          notifyDataSetChanged();
      }
 }  
 class ViewHolder {  
      TextView textview;  
      CheckBox checkbox;  
      int id;  
 }  
 public void click(View v) {  
      if (v.getId() == R.id.button1) {  

           boolean noSelect = false;  

           for (int i = 0; i < thumbnailsselection.length; i++) {  
              if (thumbnailsselection[i] == true) {  
                   noSelect = true;  

                       items.remove(i);

                }  


         }  
          thumbnailsselection = new boolean[items.size()];
         adapter.removeItems(); 
      }  
 }  
}
5
  • Get the id / position of the element which you want to remove. Remove it first from your array array.remove(indexOfObjectWhichYouWantToRemove) and call adapter.notifySetDataChanged(). Commented Jun 3, 2013 at 11:45
  • ok..i want to remove multiple selected items same time,when i click delete button Commented Jun 3, 2013 at 11:49
  • ok, so just store these positions which you select in an array and depending on them just remove the items from the main array Commented Jun 3, 2013 at 11:52
  • ok,thnks..can you see my code delete button onclick once ,i did like that only na Commented Jun 3, 2013 at 11:57
  • this code will work only if the items in thumbnailsselection and items are equal as size and it will remove the right object/item. Commented Jun 3, 2013 at 12:05

4 Answers 4

1

Try the following solution

declare this before oncreate

ImageAdapter adapter;

in your oncreate change the

ImageAdapter adapter=new ImageAdapter(MainActivity.this);
listview.setAdapter(adapter);

Inside your ImageAdapter add the following method

public void removeItems()
{
    notifyDataSetChanged();
}

in your onclick method

for (int i = 0; i < thumbnailsselection.length; i++) {  
            if (thumbnailsselection[i] == true) {  
                 noSelect = true;  
                 Log.e("sel pos thu-->", "" + i);  
            //     posSel.add(i);  //
                     items.remove(i);
                 // break;  
            }  
       }  
thumbnailsselection = new boolean[items.size()];
adapter.removeItems();
Sign up to request clarification or add additional context in comments.

10 Comments

hi where i add to removwItems() i added inside imageadapter class...its showing invocation target exception
if add out side of imageadapter menas its asking to create mehod notifyDatachanged
java.lang.NullPointerException in on adapter.removeItems(); remove line
yes listdisplaying,if select items from listview then i click delete button..that time am getting nullpointer exception
removeItems() method should be inside ImageAdapter class
|
1
  1. Record the state of all CheckBox.

    • Data type SparseBooleanArray is better than boolean[] here.
    • Initialize thumbnailsselection in ImageAdapter constructer, and modify its values in checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {}).

    // Initialize

    for (int i = 0; i < itemCount; i++)
        thumbnailsselection.put(i, false);
    

    // Modify

    holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int position = buttonView.getId();
            thumbnailsselection.put(position, isChecked);
        }
    });
    
  2. Remove items from back to front, one by one, because the deletion only affects indices of items behind the removed one.

    for (int i = itemCount - 1; i >= 0; i--) {
        position = thumbnailsselection.keyAt(i);
        isChecked = thumbnailsselection.get(position, false);
    
        if (!isChecked)
            continue;
    
        if (position < items.size())
            items.remove(position);
    }
    
  3. Show new data.

    imageAdapter.notifyDataSetChanged();
    

Comments

0

You can try below code and write it in button click event

        SparseBooleanArray checked = keywordsList.getCheckedItemPositions();

        ArrayList<String> selectedKeywords = new ArrayList<String>();
        for (int i = 0; i < checked.size(); i++) {
            int position = checked.keyAt(i);
            if (checked.valueAt(i))
                selectedKeywords.add(keywordsAdapter.getItem(position));
        }

        for (int i = 0; i < selectedKeywords.size(); i++) {
            keywordsAdapter.remove(selectedKeywords.get(i));
        }

        keywordsAdapter.notifyDataSetChanged();

Here my adapter code is like this

        keywordsAdapter = new ArrayAdapter<String>(
                            IncidentEditActivity.this,
                            android.R.layout.simple_list_item_multiple_choice,
                            arrKeywords);
        keywordsList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        keywordsList.setAdapter(keywordsAdapter);

Comments

0

I'm a little confused because I don't think your adapter shows what you want even before deletion because of this:

public int getCount() {  
           return count;  
}  
public Object getItem(int position) {  
      return position;  
}  

Once your adapter will be correctly set with the array of data to display (in your case items I think) you just have to remove the objects you want delete from this array (items.remove(obj)) and then call notifyDataSetChanged() on the adapter.

Edit:

I think there is some logic problems in your code here. I assume that you want display data from an Array so you should probably extends ArrayAdapter in place of BaseAdapter. Next I think there is a confusion between item and item position. A classical implementation of getItem() is, for arrays, something like:

public Object getItem(int position) {  
      return array.get(position);  
}

Maybe I'm totally wrong, but I think you should first clean your code to fit exactly your needs, and then it will be pretty easy to achieve that you want.

6 Comments

here what is obj, i need write delete items code inside delete button onclick() only
obj is the string that you wan't delete from the list
selected items i am storing in one arraylist then i deleted using below
for(int i1=0;i1<posSel.size();i1++) { items.remove(i1); }
possel arrylist whatever i am selecting those values is storing
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.