监听器CheckedTextView中的ListView [英] Listener for CheckedTextView in ListView

查看:546
本文介绍了监听器CheckedTextView中的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的ListView包括checkedTextView。

I created a simple ListView includes checkedTextView.

我想添加一个监听器时的ListView行的用户点击,

I want to add a listener when user click on listView row,

1 - TextView的成为检查。

1 - The textView become checked.

2 - 如果存在,其中分别检查列表中的其他的TextView,成为未检查

2 - If there is another textview in the list which were checked, become unchecked.

这是code这是我写的到现在为止:

This is the code which I written until now:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayList<String> mStrings = new ArrayList<String>();
        for(int i=0; i<30; i++) {
            mStrings.add("Item " + i);
        }

        setListAdapter( new MultiAdapter(this, mStrings));

        /* Configure ListView */
        ListView lv = getListView();
        lv.setOnItemClickListener(new ItemClick());
    }

    class ItemClick implements OnItemClickListener {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    }

    private class MultiAdapter extends ArrayAdapter<String> {

        private ArrayList<String> mItems;

        public MultiAdapter(Context context, ArrayList<String> mStrings) {
            super(context, R.layout.list2, mStrings);
            this.mItems = mStrings;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View row;

            if (convertView == null)  {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.list2, parent, false);
            } else
                row = convertView;

            CheckedTextView txt = (CheckedTextView) row.findViewById(R.id.label);

            txt.setText(mItems.get(position));

            return row;
        }
    } 

你能不能帮我一下,我需要code监听器?

Could you help me about the Listener which I need to code?

我找不到关于checkedTextView任何教程,可以帮助我。请让我知道,如果你知道一些例如code或教程。

I couldn't find any tutorial about checkedTextView that can help me. Please let me know if you know some example code or tutorial.

推荐答案

如果你只想最多1在任何检查都给予时间,那么你可以做到以下几点:

If you want to have only max 1 checked in any give time then you can do following:


  1. 创建 INT checkedItem; 属性。此属性显示当前选中项目的索引。 -1,如果没有被选中。

  2. 项目单击刚刚成立 checkedItem =位置。并调用 getListAdapter()。notifyDataSetChanged()

  3. getView 添加 txt.setChecked(位置== checkedItem)

  1. Create int checkedItem; property. This property show index of currently checked item. -1 if none is checked.
  2. In ItemClick just set checkedItem = position. And call getListAdapter().notifyDataSetChanged().
  3. In getView add txt.setChecked(position == checkedItem).

这篇关于监听器CheckedTextView中的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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