Hello my dear Stackers,
there is a little problem that I have and I hope you'll be able to help me solve this. Let's cut to the chase.
What should my code do? -> My code should set up a ListView with CheckBoxes, then I should be able to check to boxes and afterwards to count how many boxes are check after pressing a button. Then I should pass on the Objects to the next Activity, but that should not be part of this question, I merely add this for context, it might be important to the solution.
What does my code not do? -> my code doesn't count the boxes. The listView.getCheckedItemIds().length)); line only returns 0.
After rereading the ListView, List, Adapter, View, etc. documents and trying a few different aproaches (and tutorials, some frome stackoverflow) I'm not shure how to handle this or where to start
Here is the Code --------------------> Main Class <-----------------
package com.example.christian.listviewwithcheckbox_model;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
int checkCounter = 0;
ListView listView;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();
private Button mButton;
private TextView mCounter;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.my_list);
adapter = new MyAdapter(this, getModel());
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
mCounter = (TextView) findViewById(R.id.counter_text);
mButton = (Button) findViewById(R.id.count_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = 0;
int size = list.size();
for (int i=0; i<size; i++){
if (list.get(i).isSelected()){
count++;
}
}
mCounter.setText(String.format("%d", count));
}
});
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
TextView label = (TextView) v.getTag(R.id.label);
CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
Toast.makeText(v.getContext(), label.getText().toString() + " " + isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
}
private String isCheckedOrNot(CheckBox checkbox) {
if (checkbox.isChecked()) {
return "is checked";
} else {
return "is not checked";
}
}
private List<Model> getModel() {
list.add(new Model("Linux"));
list.add(new Model("Windows7"));
list.add(new Model("Suse"));
list.add(new Model("Eclipse"));
list.add(new Model("Ubuntu"));
list.add(new Model("Solaris"));
list.add(new Model("Android"));
list.add(new Model("iPhone"));
list.add(new Model("Java"));
list.add(new Model(".Net"));
list.add(new Model("PHP"));
list.add(new Model("Linux"));
list.add(new Model("Windows7"));
list.add(new Model("Suse"));
list.add(new Model("Eclipse"));
list.add(new Model("Ubuntu"));
list.add(new Model("Solaris"));
list.add(new Model("Android"));
list.add(new Model("iPhone"));
list.add(new Model("Java"));
list.add(new Model(".Net"));
list.add(new Model("PHP"));
list.add(new Model("Linux"));
list.add(new Model("Windows7"));
list.add(new Model("Suse"));
list.add(new Model("Eclipse"));
list.add(new Model("Ubuntu"));
list.add(new Model("Solaris"));
list.add(new Model("Android"));
list.add(new Model("iPhone"));
list.add(new Model("Java"));
list.add(new Model(".Net"));
list.add(new Model("PHP"));
return list;
}
}
--------------> Adapter class <-------------------------
package com.example.christian.listviewwithcheckbox_model;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
} }
-------------------> Model class <-----------------------
package com.example.christian.listviewwithcheckbox_model;
public class Model {
private String name;
private boolean selected;
public Model(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
Well, that's my problem, I sorry to open up a thread so soon after the last one, but at this point I'm in quite some trouble and need a solution. Thank you in advance and have a nice day. Eomer
Edit: The problem was solved by Hoang Nguyen and Hardy, thank you very much The above code has been updated and shows now the working code.
You use custom checkbox, so cannot use method getCheckedItemIds()
, please refer the doc and ListView Mode
For your problem, easily change button click method like this:
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = 0;
int size = list.size();
for (int i=0; i<size; i++){
if (list.get(i).isSelected()){
count++;
}
}
mCounter.setText(String.format("%d", count));
}
});