2014-01-07 48 views
0

我有一個Person類的ArrayList,其中包含該人的年齡和姓名。在適配器(擴展BaseAdapter)中,我有兩個TextViews(用於設置年齡和名稱的值)和一個複選框。這是需要膨脹到警報對話框。如何從多選項listview中獲取值alertdialog

如何使用警報對話框的multichoice來實現此功能。

而且我也看一些例子,但不瞭解有關布爾[]值的警告對話框中的屬性,這是我到目前爲止的代碼,但它仍然需要執行該multichoice模式..

<ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:choiceMode="multipleChoice"> 
    </ListView> 

我alertdialog ..

AlertDialog.Builder ab=new AlertDialog.Builder(CustomListActivity.this); 
       LayoutInflater linf=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
       View v1=linf.inflate(R.layout.dialog_list, null); 
       ab.setView(v1); 
       //ab.setTitle("Select a group"); 

       lv=(ListView) v1.findViewById(R.id.listView1); 
       ma=new MyAdapter(CustomListActivity.this, plist); 
       lv.setAdapter(ma); 

而且MYAdapter ..

public class MyAdapter extends BaseAdapter 
{ 
    Context ctx; 
    ArrayList<Person> plist; 
    LayoutInflater linf; 
    public PersonHolder ph=null; 


    public MyAdapter(Context ctx, ArrayList<Person> plist) { 
     super(); 
     this.ctx = ctx; 
     this.plist = plist; 
    } 

    public class PersonHolder 
    { 
     public TextView age; 
     public TextView name; 
     public CheckBox check; 
    } 


    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return plist.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return plist.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup arg2) { 
     // TODO Auto-generated method stub 

     linf=(LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE); 
     if(convertView==null) 
     { 
      convertView=linf.inflate(R.layout.row_item, null); 
      ph=new PersonHolder(); 
      ph.age=(TextView) convertView.findViewById(R.id.text1); 
      ph.name=(TextView) convertView.findViewById(R.id.text2); 
      ph.check=(CheckBox) convertView.findViewById(R.id.checkBox1); 
      convertView.setTag(ph); 
     } 
     else 
     { 
      ph=(PersonHolder) convertView.getTag(); 
     } 

     Person p=(Person) getItem(position); 
     ph.age.setText(p.getAge()); 
     ph.name.setText(p.getName()); 
     ph.check.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 
       // TODO Auto-generated method stub 
//    if(ph.check.isChecked()) 
//    { 
        ph.check.setSelected(arg1); 
//    } 
//    else 
//    { 
//     ph.check.setSelected(false); 
//    } 
      } 
     }); 
     return convertView; 
    } 

} 

回答

1

適配器更改爲以下並調用getSelected的適配器。

public class MyAdapter extends BaseAdapter 
{ 
    Context ctx; 
    ArrayList<Person> plist; 
    LayoutInflater linf; 
    public PersonHolder ph=null; 
    private ArrayList<Integer> selected=new ArrayList<Integer>(); 


    public MyAdapter(Context ctx, ArrayList<Person> plist) { 
     super(); 
     this.ctx = ctx; 
     this.plist = plist; 
    } 

    public class PersonHolder 
    { 
     public TextView age; 
     public TextView name; 
     public CheckBox check; 
    } 


    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return plist.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return plist.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup arg2) { 
     // TODO Auto-generated method stub 

     linf=(LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE); 
     if(convertView==null) 
     { 
      convertView=linf.inflate(R.layout.row_item, null); 
      ph=new PersonHolder(); 
      ph.age=(TextView) convertView.findViewById(R.id.text1); 
      ph.name=(TextView) convertView.findViewById(R.id.text2); 
      ph.check=(CheckBox) convertView.findViewById(R.id.checkBox1); 
      convertView.setTag(ph); 
     } 
     else 
     { 
      ph=(PersonHolder) convertView.getTag(); 
     } 

     Person p=(Person) getItem(position); 
     ph.age.setText(p.getAge()); 
     ph.name.setText(p.getName()); 
     ph.check.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 
       // TODO Auto-generated method stub 
//    if(ph.check.isChecked()) 
//    { 
        ph.check.setSelected(arg1); 
        if(arg1){ 
         selected.add(position); 
        }else{ 
         selected.remove(position); 
        } 
//    } 
//    else 
//    { 
//     ph.check.setSelected(false); 
//    } 
      } 
     }); 
     return convertView; 
    } 
public ArrayList<Integer> getSelected(){ 
    return selected; 
} 
} 
1

我自己創建了一個多選擇對話框,這就是我正在做的事情,基本上你需要每次點擊到列表視圖並記住它在你選擇/設置的地方。

private void startDayPickerDialog() { 

    String[] days = getActivity().getResources().getStringArray(R.array.dayNames); 
    days = Arrays.copyOf(days, 7); 

    final Adapter a = new Adapter(getActivity()); 

    AlertDialog d = new AlertDialog.Builder(getActivity()) 
    .setTitle(R.string.repeat) 
    .setAdapter(a, new OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 

     } 
    }).setPositiveButton(R.string.done, new OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 

     } 
    }).create(); 

    d.getListView().setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> lv, View view, int position, long id) { 
      CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox); 
      cb.setChecked(!cb.isChecked()); 
      a.getItem(position).setChecked(!a.getItem(position).isChecked());    
     } 


    }); 

    d.setCanceledOnTouchOutside(true); 
    d.show(); 
} 
+0

當複選框和列表視圖是顯示activity..Here我的情況是,我的列表視圖是在activity..while複選框處於adapter..but的對話框中,該解決方案可以應用無論如何通過你和Vipul的解決方案,我確實得到和想法..並實施它..感謝很多.. – AndroidMech

相關問題