添加自定义复选框的ListView

添加自定义复选框的ListView

本文介绍了添加自定义复选框的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我getView()(PlanAdapter类)

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方法​​在规划类

getter, setter in Plan class

public CheckBox getCheckBox(){
        return checkBox;
    }

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

和我的复选框主类

    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));

    }

日志: http://wklej.to/RpBvr/html

推荐答案

您正试图每个复选框添加到多个家长的意见,你不能做...

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

不过一个ListView已经有许多功能来实现的复选框,这里是单向的:

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());
    }
}

您可以通过将自己的XML文件只自定义布局。

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

加入

行显示每次刷新输入的数据,并尝试添加该复选框,这就是为什么你可以向下滚动,但是不起来。如果你想保持你的自定义适配器,只需检查试图再次添加同一值之前该行已经被初始化。

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.

这篇关于添加自定义复选框的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:35