How to delete check box items from list

后端 未结 6 1269
花落未央
花落未央 2021-01-14 02:24

I have 50 list item in the list. Now i have checked 10 item, so how to this 10 checked item delete(remove) from the listview when I click delete button.

Here is My C

6条回答
  •  Happy的楠姐
    2021-01-14 02:38

    man, your code lacks the relation between the check box and the object and also you didn't implement onCheckedchangeListener inside your adapter .... here is what you need http://www.vogella.de/articles/AndroidListView/article.html#listadvanced_interactive Or I'll wrap it for you:

    1. you'll need to create a class model.java which represents the cell of the list 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.
      }
      
    2. modify your adapter to extend ArrayAdapter

    3. modify the constructor of the adapter to be

      private Model[] model;
      public EfficientAdapter(Context context, Model model) {
      
           mInflater = LayoutInflater.from(context);
           this.model = model;
      
      }
      
    4. then you'll need to add the 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;
      }
      
    5. then create the model array inside your activity and pass it to the adapter.

    6. the last thing to be done is to delete the selected items from the list:

      final ArrayList newModel = new ArrayList();
      
      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]);
          }
      }
      
    7. that's it, pass the newModel to the adapter and rest the adapter to the list.

    step number 7 can be performed also by filling the model(the original one which is passed originally to the array) and then call notifyDataSetChanged() using the adapter.

    that's all and that's what always worked for me... hope this helps you too.

提交回复
热议问题