Domanda

Ho due domande:

    .
  1. Se sto usando CheckedTextView in un ListView e la mia classe si estende solo Activity (anziché ListActivity), dal momento che ho un altro pulsante in basso sotto il mio ListView, Quale evento dovrei ascoltare, quando una casella di controllo è selezionata nel CheckedTextView?

  2. Se estendo la mia classe per utilizzare ListActivity, posso utilizzare l'evento onListItemClick, giusto?Come posso aggiungere un nuovo pulsante a questo tipo di layout?

    Ecco il mio codice ..

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

    Aggiornamento :::

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

È stato utile?

Soluzione

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

Altri suggerimenti

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;
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top