aptyp
aptyp

Reputation: 251

Add custom CheckBox to ListView

I add new CheckBox in getView() (PlanAdapter class)

public View getView(int position, View convertView, ViewGroup viewGroup) {
        Plan entry = listPlan.get(position);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.walkRow, null, false);

        }
        LinearLayout d = (LinearLayout)convertView.findViewById(R.id.checkBoxPlace);


        TextView distance = (TextView) convertView.findViewById(R.id.distance);
        distance.setText(" " + entry.getexerciseNumber());

        TextView time = (TextView) convertView.findViewById(R.id.time);
        time.setText(" " + entry.getwholeTime());

        CheckBox chckStart = new CheckBox(context);
        chckStart = entry.getCheckBox();
        chckStart.setFocusable(false);
        d.addView(chckStart); //here i get force close
        return convertView;
    }

when I scroll down it looks fine, but when I back and scroll up it crashes.

getter, setter in Plan class

public CheckBox getCheckBox(){
        return checkBox;
    }

    public void setCheckBox(CheckBox checkBox){
        this.checkBox = checkBox;
    }

and my checkBox in Main class

    for (byte i = 0; i < db.planView.size(); i++) {
        chcBox = new CheckBox(this);
        chcBox.setId(i);
        checkboxList.add(chcBox);

        listOfPlan.add(new Plan(db.planView.get(i).getText().toString(), db
                .count(db.planView.get(i).getText().toString(),
                        getApplicationContext()), chcBox));

    }

log: http://wklej.to/RpBvr/html

Upvotes: 0

Views: 788

Answers (1)

Sam
Sam

Reputation: 86958

You are trying to add each checkbox to multiple parent views, which you cannot do...

However a ListView already has many features to implement checkboxes, here is one way:

public class Example extends Activity implements OnItemClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] array = {"one", "two", "three"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, array);

        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setOnItemClickListener(this);
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.v("Example", "ItemClick: " + ((ListView) parent).getCheckedItemPosition());
    }
}

You can customize the layout simply by passing your own XML file.

Addition

Each time a row is shown you refresh the entry data and try to add the checkbox, which is why you can scroll down but not up. If you want to keep your custom adapter, simply check to see if the row has already been initialized before trying to add the same values again.

Upvotes: 2

Related Questions