单击监听器+复选框 - Android电子 [英] click listener + checkbox - on android

查看:224
本文介绍了单击监听器+复选框 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共类ChooseFavorites扩展活动实现OnItemClickListener
{
    StationManager ST;
    MyCustomAdapter ARR;
    ListView控件列表;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_choose_favorites);
        ST =新StationManager();
        名单=(ListView控件)findViewById(R.id.stationsList1);
        ARR =新MyCustomAdapter(这一点,android.R.layout.simple_list_item_multiple_choice,st.getNamesOfStations());
        list.setAdapter(ARR);
        list.setOnItemClickListener(本);
    }    类MyCustomAdapter扩展ArrayAdapter<串GT;实现OnClickListener
    {        LayoutInflater吹气;
        上下文CTX;
        复选框CB;
        的String [] stationNames;
        TextView的stationName1;        公共MyCustomAdapter(上下文的背景下,INT textViewResourceId,字符串[] stationNames){
            超(背景下,textViewResourceId,textViewResourceId,stationNames);
            CTX =背景;
            this.stationNames = stationNames;
            充气= LayoutInflater.from(上下文);        }
        @覆盖
        公众诠释的getCount(){
            // TODO自动生成方法存根
            返回23;
        }        @覆盖
        公共字符串的getItem(INT位置){
            // TODO自动生成方法存根
            返回stationNames [位置]
        }        @覆盖
        众长getItemId(INT位置){
            // TODO自动生成方法存根
            返回的位置;
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup)
        {
            // TODO自动生成方法存根
            查看排= convertView;
            如果(行== NULL)
            {//对象重用
                充气= getLayoutInflater();
                行= inflater.inflate(R.layout.favorite_row,父母,假);
            }
            stationName1 =(TextView中)row.findViewById(R.id.textFavoriteItemInRow);
            stationName1.setText(stationNames [位置]);
            CB =(复选框)row.findViewById(R.id.cbCheck);
            row.setOnClickListener(本);
            返回行;
        }
        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            吐司面包= Toast.makeText(getApplicationContext(),,Toast.LENGTH_SHORT);
            toast.show();
        }    }    @覆盖
    公共无效onItemClick(适配器视图<>为arg0,视图V,INT位置,长ARG3){
        // TODO自动生成方法存根        吐司面包= Toast.makeText(getApplicationContext(),+位置,Toast.LENGTH_SHORT);
        toast.show();    }}

我不知道,如果你只能通过code看到这...
但如果在课堂上我的服装适配器有不是的onClick ,什么都不会发生有...
在Android不要使用上项目点击监听器

但它的点击的方法工作....

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

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


解决方案

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
  viewHolder持有人;  //重用现有的观点,如果一个人提供,否则,创建一个新的
  //检查,看看这个人是没有创建?
  如果(convertView == NULL){
     //获取气筒
     LayoutInflater吹气=(LayoutInflater)
        parentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);     //为它创建一个视图
     convertView = inflater.inflate(R.layout.test_reprot_lv_items,父母,假);     //创建一个保持器类来包含一排的所有对象
     持有人=新viewHolder();
     holder.equipmentID =(TextView中)convertView.findViewById(R.id.equipmentID);
     holder.checkBox =(复选框)convertView.findViewById(R.id.checkBox);     // ++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++
     //设置CheckBox的标签与行的ListView中的位置
     holder.checkBox.setTag(位置);     //设置与此观点供以后重复使用相关联的标记。
     convertView.setTag(保持器);     // ++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
     holder.checkBox.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
           复选框C =(复选框)V;
           INT位置=(整数)c.getTag();           //设置选中状态与行的ListView中的位置
           itemsList.get(位置).setSelectedState(c.isChecked());
        }
     });
  }
  其他{
     //为了重复使用preallocated内存,从查看的标签中获取的持有人,
     //被分配并保存在这个视图对象。当视图的请求
     //被渲染,也没有被实例化,我们新的持有人,并保存
     它//对视图的供以后重复使用'标签'。
     支架=(viewHolder)convertView.getTag();     // ++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++
     //设置CheckBox的标签与行的ListView中的位置
     holder.checkBox.setTag(位置);     holder.checkBox.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
           复选框C =(复选框)V;
           INT位置=(整数)c.getTag();           // ++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++
           //设置选中状态与行的ListView中的位置
           //该方法setSelectedState,它通过自己的类提供,
           //被称为更新的复选框状态。
           itemsList.get(位置).setSelectedState(c.isChecked());
        }
     });
  }  // ++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++
  //设置复选框状态,调用该方法getSelectedState这
  //通过自己的类provied。
  holder.checkBox.setChecked(itemsList.get(位置).getSelectedState());  返回convertView;
}

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!

解决方案

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

这篇关于单击监听器+复选框 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆