无法获取ListView中单击的CheckBox的相应值

时间:2012-07-31 11:58:16

标签: android listview checkbox

CheckBox中的这个ListView让我疯狂。我在StackOverflow上搜索了很多关于这一点但我无法解决这个问题。

问题是我有一个ListView的自定义适配器,我正在给CheckBox充气。

以下是代码:

public class settingadapter extends BaseAdapter{

    private Activity recievedactivity;

    private String[] recieveddescription;
    private String[] recusername;
    private String allitems="";
    private String[] itemchecked;

    private static LayoutInflater inflator=null;


    public settingadapter(Activity recactivity,String[] username, String[] recdescription ){
        recievedactivity=recactivity;
        recusername=username;
        recieveddescription=recdescription;

        inflator=(LayoutInflater)recievedactivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);



    }
    public settingadapter(Activity recactivity,String[] username, String[] recdescription ,String all){
        recievedactivity=recactivity;
        recusername=username;
        recieveddescription=recdescription;
        allitems=all;
        inflator=(LayoutInflater)recievedactivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    }


    public int getCount() {
        // TODO Auto-generated method stub
        return recusername.length;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    static class ViewHolder{
        TextView usernameTextView;
        TextView statusTextView;
        CheckBox checkView;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder viewHolder;

        if(convertView== null)
        {
            viewHolder=new ViewHolder();

            convertView=inflator.inflate(R.layout.item, null);

            viewHolder.usernameTextView=(TextView)convertView.findViewById(R.id.textView1);
            viewHolder.statusTextView=(TextView)convertView.findViewById(R.id.text);
            viewHolder.checkView=(CheckBox)convertView.findViewById(R.id.checkBox1);





            convertView.setTag(viewHolder);


        }else{
            viewHolder=(ViewHolder)convertView.getTag();
        }
            viewHolder.usernameTextView.setText(recusername[position]);
            viewHolder.statusTextView.setText(recieveddescription[position]);
             //Add listener to the checkbox


            System.out.println("testing alpha"+recusername[position]);

            if(allitems.equals("All")){
                viewHolder.checkView.setChecked(true);
            }
            else if (allitems.equals("---Select User---")){
                viewHolder.checkView.setChecked(false);
            }
            else
             return convertView;  
            //image.setBackgroundResource(R.drawable.check);



            return convertView;



    }

}

现在,我如何获得点击CheckBox的内容以及如何获取相应的文字呢?

1 个答案:

答案 0 :(得分:0)

请检查这些代码

此处人员已添加onclick事件并获取标记。 Android how to identify item in listview with checkbox

相同的代码没有onclick复选框 http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php

希望它会对你有所帮助:)。