自定义列表使用编辑文本和复选框查看

时间:2012-05-17 04:08:42

标签: android checkbox android-listview

我是编程新手,在使用getCheckedItemPositions()复选框和从自定义列表视图中的编辑文本中获取编辑值时遇到问题。任何人都可以帮我一个例子来创建具有上述功能的自定义列表视图。谢谢。

*这是自定义listview xml代码

<CheckBox android:id="@+id/checkBox" android:layout_height="wrap_content"
 android:layout_width="wrap_content" 
 android:layout_alignParentRight="true"
 android:text="CheckBox"
 android:onClick="clickHandler"
 ></CheckBox>
<TextView android:layout_height="wrap_content"
 android:layout_width="fill_parent" android:text="TextView" 
 android:id="@+id/textView1" 
 android:layout_alignParentLeft="true"
  android:layout_centerHorizontal="true" android:layout_marginBottom="14dp"

机器人:layout_toLeftOf = “@ + ID /复选框” &GT;                   

*这是设置列表视图的代码

lv1 =(ListView)dialog.findViewById(R.id.l1);                 adapter2 = new SimpleCursorAdapter(                             这个,                             R.layout.custom,                             标记2,                             new String [] {“ItemName”},                             new int [] {R.id.textView1});

         lv1.setItemsCanFocus(false);
         lv1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
         lv1.setAdapter(adapter2);

*这是我在检查时尝试做的事情

public void onClick(查看v){

                int len = lv1.getCount();
                 SparseBooleanArray checked = lv1.getCheckedItemPositions();
                 for (int i = 0 ; i < len; i++)
                  if (checked.get(i)) {
                   String item = mArrayList.get(i);

                mItems.add(mArrayList.get(i));

             System.out.println(item);




                   /* do whatever you want with the checked item */
                  } 

但这不起作用。而且我也希望以这种方式使用编辑文本来获取值。当我选中并单击按钮应用程序终止时。 Logcat when Onclick() trigurred

1 个答案:

答案 0 :(得分:1)

以下代码将解决您的问题。

public class SimpleCursorAdapter extends ArrayAdapter {
    private Context mcontext;   
    private View rowview;
    LayoutInflater inflater;
    public static ArrayList<Boolean > itemchecked=new ArrayList<Boolean>();
    public SimpleCursorAdapter(Context context,ArrayList<String> mylist)
    {
        super(context,your layout id);
        mcontext=context;

        //this is the important step    
        for (int i = 0; i < this.getCount(); i++) 
        {
            itemchecked.add(i,false); // initializes all items value with false
        }
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        rowview=convertView;
        if(convertView==null)
        {
            rowview = inflater.inflate(R.yourlayout, parent, false);
        }

        TextView textView_heading = (TextView) rowview.findViewById(R.id.textView1);


        CheckBox checkbox_detail=(CheckBox) rowview.findViewById(R.id.checkBox1);
        checkbox_detail.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);

                if (cb.isChecked()) {
                    itemchecked.set(position, true);
                    // do some operations here
                } else if (!cb.isChecked()) {
                    itemchecked.set(position, false);
                    // do some operations here
                }   
            }
        });
        checkbox_detail.setChecked(itemchecked.get(position));
        textView_heading.setText(userheading_list.get(position));

        return rowview;
    }   
}
//now the custom list part finish

现在,要从列表中获取所有信息,还要查看选中的复选框:

for(int i=0;i<yourlistadapterobject.getCount();i++)
{  
    View content_view=msg_adapter.getView(i,null , user_detail_list);
    System.out.println("the list count"+user_detail_list.getCount());
    if(MyContactAdapter.itemchecked.get(i))
    {
        System.out.println("is checked true"); 
        TextView tv_heading=  (TextView) content_view.findViewById(R.id.textView1);

        String text=tv_heading.getText();
    }
}

通过执行此操作,您可以根据已选中的复选框获取所有信息。