ListView:setItemChecked 仅适用于标准 ArrayAdapter - 使用自定义 ArrayAdapter 时不起作用? [英] ListView: setItemChecked only works with standard ArrayAdapter - does NOT work when using customized ArrayAdapter?

查看:26
本文介绍了ListView:setItemChecked 仅适用于标准 ArrayAdapter - 使用自定义 ArrayAdapter 时不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的很奇怪.

当我使用标准的 ArrayAdapter 调用 setItemChecked 的 ListView 工作正常

When I use the standard ArrayAdapter for a ListView calling setItemChecked works OK

但是当使用定制的 ArrayAdapter 时,它不会.

But when using a custom made ArrayAdapter it does not.

是什么原因?这是一个错误吗?还是我遗漏了什么?

What would be the reason? Is this a bug? Or am I missing something?

public class Test_Activity extends Activity {

    /** Called when the activity is first created. */
    private List<Model> list;
    private ListView lView;

    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // Create an array of Strings, that will be put to our ListActivity
    setContentView(R.layout.main);
    lView = (ListView) findViewById(R.id.ListView01);
    lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    list = getModel();

    //with this adapter setItemChecked works OK
    lView.setAdapter(new ArrayAdapter<Model>(this,
        android.R.layout.simple_list_item_multiple_choice, list));


  //**************************
    //PROBLEM: with this adapter it does not check any items on the screen
    // ArrayAdapter<Model> adapter = new Test_Class1(this, list);
    // lView.setAdapter(adapter);



    }

    private List<Model> getModel() {
       List<Model> list = new ArrayList<Model>();
       list.add(get("0"));
       list.add(get("1"));
       list.add(get("2"));
       list.get(1).setSelected(true);
       Model m = list.get(1);
       list.add(get("3"));
       list.add(get("4"));
       list.add(get("5"));
       list.add(get("6"));
       list.add(get("7"));
       // Initially select one of the items
       return list;
    }

    private Model get(String s) {
       return new Model(s);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.results_screen_option_menu, menu);
       return true;
    }

    /**
     * @category OptionsMenu
     */
    @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.select_all: {

        int size = lView.getAdapter().getCount();

        for (int i = 0; i <= size; i++) {

            //************************** PROBLEM
        lView.setItemChecked(i, true); // selects the item only for standard ArrayAdapter

    Log.i("xxx", "looping " + i);
        }
    }
        return true;
    case R.id.select_none:
        return true;
    }
    return false;
    }
}

//--------------------------------------------------------

//------------------------------------------------------------

public class Test_Class1 extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;

 public Test_Class1(Activity context, List<Model> list) {
    super(context, R.layout.rowbuttonlayout2, 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) {
    View view = null;
    Log.i("xxx", "-> getView " + position);
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.rowbuttonlayout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);
        viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
        viewHolder.checkbox
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
                Model element = (Model) viewHolder.checkbox
                    .getTag();
                Log.i("xxx", "-> onCheckedChanged");
                element.setSelected(buttonView.isChecked());
                Log.i("xxx", "<- onCheckedChanged");

            }
            });
        view.setTag(viewHolder);
        viewHolder.checkbox.setTag(list.get(position));
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());
    Log.i("xxx", "holder.checkbox.setChecked: " + position);

    holder.checkbox.setChecked(list.get(position).isSelected());
    Log.i("xxx", "<-  getView " + position);

    return view;
    }

}

推荐答案

您的行布局需要 Checkable 才能使 setItemChecked() 工作,在这种情况下,Android 将当用户单击该行时,管理在您的 Checkable 上调用 setChecked().您不需要设置自己的 OnCheckedChangeListener.

Your row layout needs to be Checkable for setItemChecked() to work, in which case Android will manage calling setChecked() on your Checkable as the user clicks on the row. You would not need to be setting up your own OnCheckedChangeListener.

有关更多信息,请参阅:

For more, see:

这篇关于ListView:setItemChecked 仅适用于标准 ArrayAdapter - 使用自定义 ArrayAdapter 时不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆