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
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:
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.
}
modify your adapter to extend ArrayAdapter
modify the constructor of the adapter to be
private Model[] model;
public EfficientAdapter(Context context, Model model) {
mInflater = LayoutInflater.from(context);
this.model = model;
}
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;
}
then create the model array inside your activity and pass it to the adapter.
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]);
}
}
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.