单击侦听器 + 复选框 - 在安卓上


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

    }
}

我不知道您是否只能通过代码看到这一点......但是如果班上没有onClick我的服装适配器,什么都不会发生......安卓不使用"项目点击监听器"

但它确实适用于"点击"方法....

问题是:我没有我真正需要的职位......

希望有人能帮助我!谢谢大家!

您可以使用此事件接收有关正在选中/取消选中复选框的通知

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
   }
}

使用复选框 setOnCheckedChangeListener 事件。

试试这样...

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

您可以使用以下内容:

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

或者您可以将位置保存为标签以checkbox并检查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();

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

最新更新