click listener + checkbox - on android

久未见 提交于 2019-12-13 04:34:35

问题


public class ChooseFavorites extends Activity implements OnItemClickListener
{
    StationManager st;
    MyCustomAdapter arr;
    ListView list;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_favorites);
        st = new StationManager();
        list = (ListView)findViewById(R.id.stationsList1);
        arr = new MyCustomAdapter(this, android.R.layout.simple_list_item_multiple_choice, st.getNamesOfStations());
        list.setAdapter(arr);
        list.setOnItemClickListener(this);


    }

    class MyCustomAdapter extends ArrayAdapter<String> implements OnClickListener
    {

        LayoutInflater inflater;
        Context ctx;
        CheckBox cb;
        String[] stationNames;
        TextView stationName1;

        public MyCustomAdapter(Context context, int textViewResourceId, String[] stationNames) {
            super(context, textViewResourceId, textViewResourceId, stationNames);
            ctx = context;
            this.stationNames = stationNames;
            inflater = LayoutInflater.from(context);

        }


        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 23;
        }

        @Override
        public String getItem(int position) {
            // TODO Auto-generated method stub
            return stationNames[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            // TODO Auto-generated method stub
            View row = convertView;
            if(row==null)
            { // Object reuse
                inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.favorite_row, parent, false);
            }
            stationName1 = (TextView)row.findViewById(R.id.textFavoriteItemInRow);
            stationName1.setText(stationNames[position]);
            cb=(CheckBox)row.findViewById(R.id.cbCheck);
            row.setOnClickListener(this);
            return row;
        }


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
            toast.show();


        }

    }



    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
        // TODO Auto-generated method stub

        Toast toast = Toast.makeText(getApplicationContext(), "" + position, Toast.LENGTH_SHORT);
        toast.show();



    }

}

I don't know if you can see this only through code... but If there were not an onClick in the class my costume adapter, nothing would have happen... the android don't use the "on item click listener"

but it does work on the "on click" method....

the problem is : I DONT HAVE POSITION which I truly need...

hope anyone can help me about that! thank you all!


回答1:


You can use this event to get notified about the checkbox being checked/unchecked

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

   }
}



回答2:


Use Checkbox setOnCheckedChangeListener event.

Try like this...

CheckBox ChkBx = ( CheckBox ) findViewById( R.id.checkbox);
ChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
  {
    if ( isChecked )
    {
        // perform logic
    }

  }
});



回答3:


You can use following:

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        Toast.makeText(getApplicationContext(), 
                       "Click ListItem Number " + position, Toast.LENGTH_LONG)
                      .show();
    }
}); 

Or u can save position as tag to checkbox and check on checkbox clicklistener

@Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
//            ur code...

 **cb.setTag(position);**
            row.setOnClickListener(this);
            return row;
        }


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
**int position = v.getTag();** //cast & check for Tag not null too.
            Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
            toast.show();


        }



回答4:


        @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub
        View row = convertView;
        if(row==null)
        { // Object reuse
            inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.favorite_row, parent, false);
        }
        stationName1 = (TextView)row.findViewById(R.id.textFavoriteItemInRow);
        stationName1.setText(stationNames[position]);
        cb=(CheckBox)row.findViewById(R.id.cbCheck);

        cb.setOnCheckedChangeListener(new CompoundButton.
                OnCheckedChangeListener(){
               @Override
               public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                   if (isChecked)
                   {
                       cb.setChecked(isChecked);
                        Toast toast = Toast.makeText(getApplicationContext(), "checked" + position, Toast.LENGTH_SHORT);
                        toast.show();
                   }
                   else
                   {
                       cb.setChecked(isChecked);
                        Toast toast = Toast.makeText(getApplicationContext(), "unChecked" + position, Toast.LENGTH_SHORT);
                        toast.show();
                   }
               }
            });

        return row;
    }

}



回答5:


public View getView(int position, View convertView, ViewGroup parent) {
  viewHolder holder;

  // Reuse an existing view, if one is supplied, otherwise create a new one
  // Check to see if this one is created yet ?
  if (convertView == null){
     // Get inflater
     LayoutInflater inflater = (LayoutInflater)
        parentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     // Create a view for it
     convertView = inflater.inflate(R.layout.test_reprot_lv_items, parent, false);

     // Creates a holder class to contain all objects of a row 
     holder = new viewHolder();
     holder.equipmentID   = (TextView) convertView.findViewById(R.id.equipmentID);
     holder.checkBox      = (CheckBox) convertView.findViewById(R.id.checkBox);

     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Set checkBox's Tag with the position of the row in the listview
     holder.checkBox.setTag(position);

     // Sets the tag associated with this view for later reuse.
     convertView.setTag(holder);

     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     holder.checkBox.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
           CheckBox c = (CheckBox) v;
           int location = (Integer) c.getTag();

           // Set selected state with the location of the row in the listview
           itemsList.get(location).setSelectedState(c.isChecked());
        }
     });
  }
  else{
     // In order to reuse the preallocated memory, get the holder from View's tag, 
     // which is allocated and saved in this view object. When a view requests 
     // to be rendered and it has not been instantiated, we new a holder and save
     // it to View's 'Tag' for later reuse.
     holder = (viewHolder) convertView.getTag();

     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Set checkBox's Tag with the position of the row in the listview
     holder.checkBox.setTag(position);

     holder.checkBox.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
           CheckBox c = (CheckBox) v;
           int location = (Integer) c.getTag();

           //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
           // Set selected state with the location of the row in the listview
           // The method setSelectedState, which provided by your own class,
           // is called to update the checkbox state.
           itemsList.get(location).setSelectedState(c.isChecked()); 
        }
     });
  }

  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  // Set checkbox state with calling the method getSelectedState which 
  // provied by your own class.
  holder.checkBox.setChecked(itemsList.get(position).getSelectedState());

  return convertView;
}


来源:https://stackoverflow.com/questions/21650172/click-listener-checkbox-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!