2014-09-29 65 views
0

我必須多檢查在我的Android程序無法從多檢查清除檢查

一切都很正常,但有一個問題 - 我不能取消所有標記

我的代碼:

//============ fill the array ============== 
CharSequence[] items={}; 
boolean[] itemsChecked; 

SQL = "SELECT Name FROM Goods order by Name"; 
items = new String [16]; 
z=0; 
try 
{ 
    Cursor c = MyParam.db.rawQuery(SQL, null); 
    if (c != null) 
    { 
     if (c.moveToFirst()) 
     { 
      do 
      { 
       items[z++] = String.valueOf(c.getString(c.getColumnIndex("Name"))) ; 
      } 
      while (c.moveToNext()); 
      } 
      c.close(); 
     } 
} 
catch (Exception e) 
{ 
} 
itemsChecked = new boolean [items.length]; 


//======== when i open the dialog =========== 
btnGoods = (TextView)findViewById(R.id.lblGoods); 
btnGoods.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     DOD=""; 
     showDialog(1); 
    } 
}); 


//================ i have this in my code - Not in used =================== 
public void onListItemClick(ListView parent, View v, int position, long id) { 
parent.setItemChecked(position, parent.isItemChecked(position)); 
} 


//================== the dialog ================ 
@Override 
protected Dialog onCreateDialog(int id, Bundle args){ 
    switch(id){ 
      case 1: 
       return new AlertDialog.Builder(this) 
       .setIcon(R.drawable.ic_launcher) 
       .setTitle("Pick some") 

       .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int whichButton) 
       { 
        DOD=""; 
        for (int i=0;i<itemsChecked.length;i++) 
        { 
         if (itemsChecked[i]==true) 
         { 
          DOD+=items[i].toString() + ","; 
         } 
        } 
        DOD = DOD.substring(0,DOD.length()-1); 
        Toast.makeText(getBaseContext(),DOD, Toast.LENGTH_LONG).show(); 
       } 
       }) 

       .setNegativeButton("cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int whichButton) 
       { 
        //Toast.makeText(getBaseContext(),"Cancel clicked!", Toast.LENGTH_SHORT).show(); 
       } 
       }) 

       .setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { 
       public void onClick(DialogInterface dialog, int which,boolean isChecked) { 
        //Toast.makeText(getBaseContext(),items[which] + (isChecked ? " checked!":" unchecked!"),Toast.LENGTH_SHORT).show(); 
       } 
       } 
       ) 
       .create(); 

其優秀的作品,但我不能全部取消選中。

我試試這個:

DOD=""; 
itemsChecked = new boolean [items.length]; 
for (int i=0;i<itemsChecked.length;i++) 
{ 
     itemsChecked[i]=false; 
} 

編輯:

我嘗試這也和我設法當我關閉dialod

如何時,這樣做是爲了控制複選框對話加載?

.setNegativeButton("cancel", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog,int whichButton) 
    { 
      ((AlertDialog) dialog).getListView().setItemChecked(2, true); 
    } 
    }) 

感謝

+0

這塊你只嘗試了代碼的每一布爾設置爲false的itemsChecked陣列英寸你對物品什麼都不做。 – Alboz 2014-09-29 13:13:56

+0

請在上面格式化代碼 – AndroidEnthusiast 2014-09-29 13:21:01

+0

如何更新對話框並取消選中所有複選框? – GoldSoft 2014-09-29 14:28:28

回答

0

的另一種方式是嘗試得到實際的複選框元件和使用肘節()或setSelected()方法。

+0

我可以爲此獲取樣本嗎? – GoldSoft 2014-09-29 13:18:45

+0

我更新我的問題 – GoldSoft 2014-10-02 07:12:24

0

嘗試使用這樣的:

((AlertDialog) dialog).getListView().setItemChecked(positioin, false); 
+0

我可以獲得更多信息嗎?在哪裏放? – GoldSoft 2014-09-29 13:56:26

+0

您必須創建一個私有類變量並將其分配給由'onCreateDialog()'返回的'Dialog',以便您可以在'onCreateDialog()'和返回對話框對象中分配它。並且每當你想取消選中項目時,使用相同的for循環並使用我發佈的代碼。 – 2014-09-29 13:59:51

+0

這樣的事情?保護對話框onCreateDialog(INT ID,捆綁參數){ \t \t \t開關(ID){ \t \t \t \t \t \t殼體99: \t \t \t \t AlertDialog對話框= NULL; \t \t \t \t int positioin = 0; \t \t \t \t((AlertDialog)dialog).getListView()。setItemChecked(positioin,false); – GoldSoft 2014-09-29 14:18:26