带有选中文本视图的列表视图

发布于 2024-10-17 16:29:58 字数 2628 浏览 6 评论 0原文

我有两个问题:

  1. 如果我在 ListView 中使用 CheckedTextView 并且我的类仅扩展 Activity (而不是 ListActivity),因为我的 ListView 下方底部有另一个按钮, 当在 CheckedTextView 中选中复选框时,我应该监听什么事件?

  2. 如果我扩展我的类以使用 ListActivity,我可以使用 onListItemClick 事件,对吧?如何向这种类型的布局添加新按钮?

这是我的代码..

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    m_versionText=(TextView)findViewById(R.id.versionText);
    m_versionText.setText("Android Version is:" + Build.VERSION.RELEASE);
    m_btnAllCalendars = (Button)findViewById(R.id.btn_allCalendars);
    m_btnAllCalendars.setOnClickListener(mCalendarListener);
    m_calendarList=(ListView)findViewById(R.id.calList);

    //populateList uses simplecursoradapter to add items to the listview..(this part is working)

    populateList();
    m_calCheckText = (CheckedTextView)findViewById(R.id.calTextView);
    m_calCheckText.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
        if (((CheckedTextView)v).isChecked())
        {
            Log.d(TAG,"i am inside ...its checked");
        }
    }

    });

     private void populateList(){

        Log.d(TAG,"inside populateList");
        Cursor cursor = getAllCalendars();
        startManagingCursor(cursor);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.cal_list,cursor,new String[]{"displayName"},new int []{R.id.calTextView});
        //m_calendarList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        m_calendarList.setAdapter(adapter);
      }

 }

更新:::

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.cal_list,cursor,new String[]{"displayName"},new int []{R.id.calTextView});
        m_calendarList=getListView();
        m_calendarList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        m_calendarList.setAdapter(adapter);
        m_calendarList.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View view,int position,long id)
            {
                Log.d(TAG,"I am inside onItemClick and position is:"+String.valueOf(position));
            CheckedTextView ctv = (CheckedTextView)view;
    the below code didnt work too           
          //CheckedTextView ctv = (CheckedTextView)view.findViewById(R.layout.cal_list);
                ctv.setChecked(!ctv.isChecked());

            }

        });

I have two questions:

  1. If I am using CheckedTextView in a ListView and my class only extends Activity (instead of ListActivity), since I have another button at the bottom below my ListView,
    what event should I listen on, when a checkbox is selected in the CheckedTextView?

  2. If I extend my class to use ListActivity, I can use onListItemClick event, right? How can I add a new button to this type of layout?

Here is my code..

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    m_versionText=(TextView)findViewById(R.id.versionText);
    m_versionText.setText("Android Version is:" + Build.VERSION.RELEASE);
    m_btnAllCalendars = (Button)findViewById(R.id.btn_allCalendars);
    m_btnAllCalendars.setOnClickListener(mCalendarListener);
    m_calendarList=(ListView)findViewById(R.id.calList);

    //populateList uses simplecursoradapter to add items to the listview..(this part is working)

    populateList();
    m_calCheckText = (CheckedTextView)findViewById(R.id.calTextView);
    m_calCheckText.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
        if (((CheckedTextView)v).isChecked())
        {
            Log.d(TAG,"i am inside ...its checked");
        }
    }

    });

     private void populateList(){

        Log.d(TAG,"inside populateList");
        Cursor cursor = getAllCalendars();
        startManagingCursor(cursor);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.cal_list,cursor,new String[]{"displayName"},new int []{R.id.calTextView});
        //m_calendarList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        m_calendarList.setAdapter(adapter);
      }

 }

UPDATE:::

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.cal_list,cursor,new String[]{"displayName"},new int []{R.id.calTextView});
        m_calendarList=getListView();
        m_calendarList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        m_calendarList.setAdapter(adapter);
        m_calendarList.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View view,int position,long id)
            {
                Log.d(TAG,"I am inside onItemClick and position is:"+String.valueOf(position));
            CheckedTextView ctv = (CheckedTextView)view;
    the below code didnt work too           
          //CheckedTextView ctv = (CheckedTextView)view.findViewById(R.layout.cal_list);
                ctv.setChecked(!ctv.isChecked());

            }

        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2024-10-24 16:29:58

您可以使用 CheckedTextView.setOnClickListener() 在侦听器的 onClick() 方法内,您可以使用 CheckedTextView.isChecked() 来查明它是否已选中或未选中。

编辑:这是适配器的 getView() 方法的示例。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        m_calCheckText = (CheckedTextView)v.findViewById(R.id.calTextView);
        m_calCheckText.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v){
                if (((CheckedTextView)v).isChecked())
                {
                   Log.d(TAG,"i am inside ...its checked");
                }
            }
        });

        return v;
    }

You can use CheckedTextView.setOnClickListener() Inside the onClick() method of your listener you can use CheckedTextView.isChecked() to find out whether it was it was checked or unchecked.

Edit: Here is an example of the getView() method for your Adapter.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        m_calCheckText = (CheckedTextView)v.findViewById(R.id.calTextView);
        m_calCheckText.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v){
                if (((CheckedTextView)v).isChecked())
                {
                   Log.d(TAG,"i am inside ...its checked");
                }
            }
        });

        return v;
    }
高速公鹿 2024-10-24 16:29:58

你可以尝试这个:

m_calendarList.setAdapter(adapter);
m_calendarList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // do something here...
        // m_calendarList.getCheckedItemPosition(); will also give you the position
    }
});

编辑:

m_calendarList.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View view,int position,long id) {
        View v = m_calendarList.getChildAt(position);
        CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.cal_id);
    }
});

You could try this:

m_calendarList.setAdapter(adapter);
m_calendarList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // do something here...
        // m_calendarList.getCheckedItemPosition(); will also give you the position
    }
});

EDIT:

m_calendarList.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View view,int position,long id) {
        View v = m_calendarList.getChildAt(position);
        CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.cal_id);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文