AlertDialog中的多项选择

时间:2012-09-05 02:56:28

标签: android listview checkbox alertdialog

我使用“lv_del_transl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)”来设置此选项。

创建AlertDialog的代码:

Al_tr.setTitle("Title");

RelativeLayout view_T = (RelativeLayout)getLayoutInflater().inflate(R.layout.listofword_cmenu_del_transl, null);
Al_tr.setView(view_T);


TextView del_transl = (TextView)view_T.findViewById(R.id.del_transl);
ListView lv_del_transl = (ListView)view_T.findViewById(R.id.lv_del_transl);
Button del_transl_OK = (Button)view_T.findViewById(R.id.del_transl_OK);
Button del_transl_Cancel = (Button)view_T.findViewById(R.id.del_transl_Cancel);


al_del_tr=Al_tr.create();

Del_transl.setText("word");

Cursor c_adap_tr=cur_del_tr(......);
startManagingCursor(c_adap_tr);

String[] from_r = new String[]{NAME};
int[] to_r = new int[] {R.id.transl };
scAdapter_transl = new SimpleCursorAdapter(this, R.layout.listofword_cmenu_del_transl_item, c_adap_tr, from_r, to_r);
lv_del_transl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // for set Multiple property

lv_del_transl.setAdapter(scAdapter_transl);

del_transl_OK.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        String sel="0";

        SparseBooleanArray sbArray = lv_del_transl.getCheckedItemPositions();
        for (int i = 0; i < lv_del_transl.getCount(); i++) {

            if (sbArray.get(i))
                sel+=Integer.toString(lv_del_transl.getCheckedItemPosition())+" ";
        }
        Toast.makeText(getApplicationContext(), Integer.toString(lv_del_transl.getCount())+" "+sel, Toast.LENGTH_SHORT).show();


    }
});    
al_del_rt.show();

ListView的Xml文件

 <LinearLayout
        android:id="@+id/lv_del_translation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp" >

        <TextView
            android:id="@+id/del_translation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/text_del_translation"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/del_transl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="28dp"
            android:text="@string/text_del_transl"
            android:textSize="20dp" />
    </LinearLayout>

    <ListView
        android:id="@+id/lv_del_transl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lv_del_translation"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:choiceMode="multipleChoice" />

    <LinearLayout
        android:id="@+id/l_del_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center"
        android:background="@drawable/gradient_box"
        android:orientation="horizontal"
        android:padding="5dp" >

        <Button
            android:id="@+id/del_transl_OK"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/text_del_word_OK" />

        <Button
            android:id="@+id/del_transl_Cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/text_del_word_Cancel" />
    </LinearLayout>

</RelativeLayout>

其中listofword_cmenu_del_transl_item.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">

<TextView
 android:id="@+id/transl"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="20dp"
 android:layout_centerVertical="true"
 android:textSize="19sp">
</TextView>

<CheckBox 
android:id="@+id/checbox_id"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

 </RelativeLayout>

但是SparseBooleanArray sbArray没有效果。我无法定义检查位置。请帮我找个错误。

1 个答案:

答案 0 :(得分:0)

private void showPopUp()
{
    final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
    helpBuilder.setTitle("");

    LayoutInflater inflater = getLayoutInflater();
    final View PopupLayout = inflater.inflate(R.layout.jobselection, null);
    helpBuilder.setView(PopupLayout);

    final AlertDialog helpDialog = helpBuilder.create();
    helpDialog.show();

    okbtn           =   (Button)PopupLayout.findViewById(R.id.okBtn);
    caneclbtn       =   (Button)PopupLayout.findViewById(R.id.cancelBtn);
    selectallbtn    =   (Button)PopupLayout.findViewById(R.id.selectBtn);
    clearallbtn     =   (Button)PopupLayout.findViewById(R.id.clearallBtn);

    jobList         =   (ListView)PopupLayout.findViewById(R.id.list);

    mylist          =   new ArrayList<HashMap<String, String>>();

    for(int i=0;i<Punchedjobs.size();i++)
    {
        map = new HashMap<String, String>();
        map.put("name", Punchedjobs.get(i));
        mylist.add(map);
    }
    sd = new SimpleAdapter(StaffTimeClock.this,mylist,R.layout.jobslist,
                new String[]{"name"},new int[]{R.id.jobText});
    jobList.setAdapter(sd);

    okbtn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) 
        {
            //ur code
        }
    });

    caneclbtn.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            helpDialog.dismiss();
        }
    });

    selectallbtn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) 
        {
            CheckBox job_chk;
            for(int i=0;i<Punchedjobs.size();i++)
            {
                job_chk= ((CheckBox)jobList.getChildAt(i).findViewById(R.id.chk));
                job_chk.setChecked(true);
            }
        }
    });

    clearallbtn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) 
        {
            CheckBox job_chk;
            for(int i=0;i<Punchedjobs.size();i++)
            {
                job_chk= ((CheckBox)jobList.getChildAt(i).findViewById(R.id.chk));
                job_chk.setChecked(false);
            }
        }
    });

}

<强> jobselection.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".StaffTimeClock" >

  <TextView
    android:id="@+id/img"
    android:layout_width="fill_parent"
    android:text="@string/selectjob"
    android:gravity="center"
    android:textSize="30dp"
    android:textColor="#fff"
    android:background="#203C56"
    android:padding="10dp"
    android:layout_height="wrap_content"/>

 <ListView
     android:id="@+id/list"
     android:layout_weight="1"
     android:layout_width="fill_parent"
     android:layout_height="0dp"
     android:cacheColorHint="#222000" />

 <View 
     android:layout_width="fill_parent"
     android:layout_height="3dp"
     android:background="#000"/>

 <LinearLayout android:id="@+id/lin00"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:orientation="horizontal">

     <Button 
      android:layout_weight="0.1"
      android:contentDescription="@string/ok"
      android:id="@+id/okBtn"
      android:layout_gravity="center"
      android:layout_margin="10dp"
      android:padding="10dp"
      android:textSize="25dp"
      android:background="@drawable/toolbar_background"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/ok"/>

 <Button 
      android:layout_weight="0.1"
      android:contentDescription="@string/ok"
      android:id="@+id/selectBtn"
      android:layout_gravity="center"
      android:layout_marginTop="10dp"
      android:layout_marginBottom="10dp"
      android:padding="10dp"
      android:textSize="25dp"
      android:background="@drawable/toolbar_background"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/selectall"/>

  <Button 
      android:layout_weight="0.1"
      android:contentDescription="@string/ok"
      android:id="@+id/clearallBtn"
      android:layout_gravity="center"
      android:layout_margin="10dp"
      android:padding="10dp"
      android:textSize="25dp"
      android:background="@drawable/toolbar_background"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/clear"/>

  <Button 
      android:layout_weight="0.1"
      android:contentDescription="@string/cancel"
      android:id="@+id/cancelBtn"
      android:layout_gravity="center"
      android:layout_marginTop="10dp"
      android:layout_marginBottom="10dp"
      android:padding="10dp"
      android:textSize="25dp"
      android:background="@drawable/toolbar_background"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/cancel"/>

  </LinearLayout>
  </LinearLayout>

<强> jobslist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/lin01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:padding="10dp"
android:orientation="horizontal">

        <ImageView 
            android:id="@+id/image"
            android:contentDescription="@string/staff"
            android:src="@drawable/logo2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/jobText"
            android:layout_width="0dp"
            android:text="@string/jobtype"
            android:layout_weight="1"
            android:gravity="left|center_vertical"
            android:layout_marginLeft="10dp"
            android:textSize="25dp"
            android:textColor="#000"
            android:layout_height="50dp"/>

        <CheckBox 
            android:id="@+id/chk"
            android:layout_width="wrap_content"
            android:text=""
            android:gravity="center_vertical|right"
            android:button="@drawable/checkbox_selector_green"
            android:layout_height="wrap_content"/>


        </LinearLayout>