How to remove items from a list

I have 50 list items in a list. Now I checked 10 items, so like this 10 marked items remove (remove) from the list when I click the delete button.

Here is my code

Please see my code and the answer where the error is located :

public class BookmarksJokes extends Activity implements OnClickListener, OnItemClickListener { ListView lv; Button btn_delete; public String TAG = "horror"; private SQLiteDatabase db; public static final String PREFS_NAME = "MyPreferences"; static String[] tempTitle = new String[100]; static String[] tempBody = new String[100]; private static boolean bRequiresResponse; private static class EfficientAdapter extends BaseAdapter { private LayoutInflater mInflater; public EfficientAdapter(Context context) { mInflater = LayoutInflater.from(context); } public int getCount() { return tempTitle.length; } 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) { convertView = mInflater.inflate(R.layout.bookmarks_list_item, null); holder = new ViewHolder(); holder.text1 = (TextView) convertView .findViewById(R.id.title); holder.text2 = (TextView) convertView .findViewById(R.id.body); holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.text1.setText(tempTitle[position]); holder.text2.setText(tempBody[position]); // bRequiresResponse = checkBox.isChecked(); return convertView; } static class ViewHolder { TextView text1; TextView text2; CheckBox checkBox; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bokmarksjoks); try { db = (new DatabaseHelper(this)).getWritableDatabase(); } catch (IOException e) { e.printStackTrace(); } setUpViews(); title = (TextView) findViewById(R.id.body); SharedPreferences pref = getSharedPreferences(PREFS_NAME, 0); //String ids = pref.getString("jid", ""); String one = pref.getString("title", ""); String two = pref.getString("body", ""); tempTitle = one.split(","); tempBody = two.split(","); lv.setAdapter(new EfficientAdapter(this)); } private void setUpViews() { lv = (ListView) findViewById(R.id.list); btn_delete = (Button) findViewById(R.id.delete); btn_delete.setOnClickListener(this); // checkbox = (CheckBox) findViewById(R.id.checkbox); } private void removeData(){ //int pos= getIntent().getIntExtra("POSITION", 0); //lv.removeViewAt(pos); // notifyAll();*/ // int pos= getIntent().getIntExtra("POSITION", 0); // if(checkBox.isChecked()){ // Log.d(TAG, " checked d]"+pos); // Toast.makeText(this, "checked "+pos, Toast.LENGTH_SHORT).show(); // } } public void onClick(View v) { switch (v.getId()) { case R.id.delete: AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); alt_bld.setMessage("Are you Sure want to delete all checked jok ?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //removeJok(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = alt_bld.create(); alert.setTitle("Delete Jokes"); alert.show(); case R.id.checkbox: default: break; } } public void onItemClick(AdapterView<?> arg0, View view, int position, long ids) { try { // checkBox = (CheckBox)view.findViewById(R.id.checkbox); // checkBox.setChecked(true); Intent intent = new Intent(BookmarksJokes.this, BookmarkJokesDetails.class); intent.putExtra("POSITION", position); intent.putExtra("ID", ids); Cursor cursor = (Cursor) adapter.getItem(position); intent.putExtra("_ID", cursor.getInt(cursor.getColumnIndex("_id"))); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } Toast.makeText(BookmarksJokes.this, "Item in position " + position + " clicked", Toast.LENGTH_LONG) .show(); } } 

Here is my complete code:

http://pastebin.com/LB2WKHMP

+4
source share
6 answers

I performed the same task in one of my applications.

What you need to do is use an ArrayList with the same size as the list item, and each item contains a default. Now in your efficient adapter, for each checkbox set, assign 1 at this specific position to arrayList. Finally, by clicking the "Delete" button, go through ArrayList and remove all of these elements from the list that contain 1 in this entry in this ArrayList and atLast call the notifyDatasetChanged () method of the list to update the list and show the new list.

Here is a sample code to help you better understand:

  ArrayList<Integer> checks=new ArrayList<Integer>(); 

Now in the onCreate method

 for(int b=0;b<tempTitle.length;b++){ checks.add(b,0); //assign 0 by default in each position of ArrayList } 

Now in your efficient getView method

  public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.bookmarks_list_item, null); holder = new ViewHolder(); holder.text1 = (TextView) convertView .findViewById(R.id.title); holder.text2 = (TextView) convertView .findViewById(R.id.body); holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.checkBox.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub if(((CheckBox)v).isChecked()){ checks.set(position, 1); } else{ checks.set(position, 0); } } }); return convertView; } 

Finally, click the "Delete" button:

  delete.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub for(int i=0;i<checks.size();i++){ if(checks.get(i)==1){ //remove items from the list here for example from ArryList checks.remove(i); //similarly remove other items from the list from that particular postion i--; } } ((EfficientAdapter)lv.getAdapter()).notifyDataSetChanged(); } } 

Hope this solves your problem.

+10
source

I assume that I have identifiers that are selected. now just arrayList.remove (position); as well as call notifyDataSetChanged ();

+4
source

man, your code doesn’t have a relation between the flag and the object, and also you did not implement onCheckedchangeListener inside your adapter .... here is what you need http://www.vogella.de/articles/AndroidListView/article.html#listadvanced_interactive Or I I will close for you :

  • you need to create a model.java class that represents a list cell as follows:

     public class Model { private String title; private String body; private boolean isSelected; public Model(String title, String body) { this.title = title; this.body = body; this.isSelected = false; } // here you MUST create your set of setters and getters. } 
  • change your adapter to the extension ArrayAdapter<Model>

  • change the adapter constructor to

     private Model[] model; public EfficientAdapter(Context context, Model model) { mInflater = LayoutInflater.from(context); this.model = model; } 
  • then you need to add onCheckedchangeListener inside your adapter inside the getView method, so your getView method will look like this:

     @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; if (convertView == null) { LayoutInflater inflator = context.getLayoutInflater(); final ViewHolder viewHolder = new ViewHolder(); viewHolder.text1 = (TextView) convertView.findViewById(R.id.title); viewHolder.text2 = (TextView) convertView.findViewById(R.id.body); viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox); viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { Model element = (Model) viewHolder.checkbox.getTag(); element.setSelected(buttonView.isChecked()); } }); view.setTag(viewHolder); viewHolder.checkbox.setTag(list[position]); } else { view = convertView; ((ViewHolder) view.getTag()).checkbox.setTag(list[position]); } ViewHolder holder = (ViewHolder) view.getTag(); holder.text1.setText(tempTitle[position]); holder.text2.setText(tempBody[position]); return view; } 
  • then create an array of the model inside your activity and pass it to the adapter.

  • the last thing to do is remove the selected items from the list:

     final ArrayList<Model> newModel = new ArrayList<Model>(); for (int i = 0; i < model.length/*this is the original model*/; i++) { if(model[i].isChecked()){ // fill the array list ... newModel.add(model[i]); } } 
  • so that it passes newModel to the adapter and leaves the adapter in the list.

step 7 can also be performed by filling in the model (the original one, which is first passed to the array), and then calls notifyDataSetChanged() using the adapter.

that everything and what has always worked for me ... hope this helps too.

+2
source

If you are using listview with CHOICE_MODE_MULTIPLE mode, you should use something like: listView.getCheckedItemPositions ();

then if you have list items stored in any list or vector, you should be able to easily delete items with the same positions and update the list.

Although all my checkbox + list settings are based on this: Android list and checkbox

0
source

I think you do not need any large amount of code. You just need a concept. The answer is above. This is the best way to do this.

 I am assuming i got ids which are selected. now simply arrayList.remove(position); and call notifyDataSetChanged(); 

As a child of a list (CheckBox), you can directly use setOnCheckChangesListener in the adapter base class. Now you need to leave the check check check check checkbox and their corresponding position in the ArrayList. Now when you click on delete btn, remove the element from arraylist and call notifyDataSetChanged ()

0
source

Refresh the list again when you check the box and click the delete button. This means that after the items have been deleted, you need to install the adapter in the list view again. Thanks

0
source

Source: https://habr.com/ru/post/1393133/


All Articles