2

我有两个问题:

  1. 如果我CheckedTextView在 a 中使用ListView并且我的类只扩展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());

            }

        });
4

2 回答 2

3

您可以使用 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;
    }
于 2011-02-17T20:53:23.273 回答
2

你可以试试这个:

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);
    }
});
于 2011-02-18T14:44:49.173 回答