本文介绍了列表视图与checkedtextview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题:


  1. 如果我使用 CheckedTextView 的ListView 和我的类只扩展活动(而不是 ListActivity ),因为我在下面的底部还有一个按钮,我的的ListView
    我应该听什么样的事件上,当 CheckedTextView

  2. 选中复选框
  3. 如果我致以类使用 ListActivity ,我可以使用 onListItemClick 事件,对吧?我如何添加一个新的按钮,这种布局的?


下面是我的code ..

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    m_versionText =(的TextView)findViewById(R.id.versionText);
    m_versionText.setText(Android版本:+ Build.VERSION.RELEASE);
    m_btnAllCalendars =(按钮)findViewById(R.id.btn_allCalendars);
    m_btnAllCalendars.setOnClickListener(mCalendarListener);
    m_calendarList =(ListView控件)findViewById(R.id.calList);    // populateList使用simplecursoradapter将项目添加到列表视图。(这部分工作)    populateList();
    m_calCheckText =(CheckedTextView)findViewById(R.id.calTextView);
    m_calCheckText.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
        如果(((CheckedTextView)ⅴ).isChecked())
        {
            Log.d(TAG,我是里面...它检查);
        }
    }    });     私人无效populateList(){        Log.d(TAG,里面populateList);
        光标光标= getAllCalendars();
        startManagingCursor(光标);        SimpleCursorAdapter适配器=新SimpleCursorAdapter(这一点,R.layout.cal_list,光标,新的String [] {显示名},新的INT [] {R.id.calTextView});
        //m_calendarList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        m_calendarList.setAdapter(适配器);
      } }

更新:::

  SimpleCursorAdapter适配器=新SimpleCursorAdapter(这一点,R.layout.cal_list,光标,新的String [] {显示名},新的INT [] {} R.id.calTextView) ;
        m_calendarList = getListView();
        m_calendarList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        m_calendarList.setAdapter(适配器);
        m_calendarList.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的ID)
            {
                Log.d(TAG,我是里面onItemClick和立场是:+将String.valueOf(位置));
            CheckedTextView CTV =(CheckedTextView)视图。
    下面code没有工作过
          // CheckedTextView CTV =(CheckedTextView)view.findViewById(R.layout.cal_list);
                ctv.setChecked(ctv.isChecked()!);            }        });


解决方案

您可以试试这个:

  m_calendarList.setAdapter(适配器);
m_calendarList.setOnItemClickListener(新OnItemClickListener(){
    公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
        //做一些事情在这里...
        // m_calendarList.getCheckedItemPosition();也会给你的位置
    }
});

编辑:

  m_calendarList.setOnItemClickListener(新OnItemClickListener(){
    公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
        视图V = m_calendarList.getChildAt(位置);
        CheckedTextView CTV =(CheckedTextView)v.findViewById(R.id.cal_id);
    }
});

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

            }

        });
解决方案

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

这篇关于列表视图与checkedtextview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 23:21