2012-04-03 62 views
2

我一直無法找到一個教程,幫助使用光標多選。截至目前,我的邏輯按照我想要的方式工作,但複選框不會正確更新。我忽略了什麼?我怎樣才能更新在Android對話窗口多選擇

return new AlertDialog.Builder(this).setTitle("Items") 
      .setMultiChoiceItems(cur, CHECK, EDATE, new DialogInterface.OnMultiChoiceClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int position, boolean checked) 
       { 
        DBM.open(); 
        AlertDialog AD = (AlertDialog) dialog; 
        ListView list = AD.getListView(); 
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

        itemCur = (Cursor) list.getItemAtPosition(position); 

        if (checked) 
        { 
         //update query 
         DBM.setChecked(checkCur.getInt(checkCur.getColumnIndex(ID)), itemId, userId, 1); 
         list.setItemChecked(1, true); 
        } else 
        { 
         DBM.setChecked(checkCur.getInt(checkCur.getColumnIndex(ID)), itemId, userId, 0); 
         list.setItemChecked(1, false); 
        } 
        DBM.close(); 
       } 
      }).setPositiveButton("OK", new DialogButtonClickHandler()).create(); 

回答

0

所以挖掘到的問題一點,經歷了幾個不同的迭代,我終於發現,我與中非常愉快的解決方案之後。隨着學校和工作的不斷推進,我很少有時間在額外的項目上工作,而我一直在坐着這個解決方案,但現在卻無法獲得發佈。

最後一塊我的難題是找到changeCursor函數,這解決了不再匹配數據庫加載的舊數據的問題。我目前的障礙是檢查一個盒子的時間,從點擊到更新有明顯的滯後。我發現多點記錄更新時,點擊一個。我一直無法找到這些額外更新的有效理由。

以下是我目前實施的多選工作代碼。這只是對話框代碼,對於一個工作演示,我將在GitHub上發佈一個項目的工作原型。 (現在公開,Multiselect Dialog

我是一個相當新的Android開發者,我的大部分Android知識都是通過在線資源的知識自學成才的。我正在研究一個學校項目,並希望在一個對話框中實現多重選擇,該對話框將使用所選擇的選項更新主要活動。請提供任何建議,如何改善這一點。

優點:
- 在加載時正確填充複選框。
- 單擊檢查時更新數據庫。
- 數據更改後保持顯示更新。

缺點:
- 必須單擊複選框以更新值。
- 無法撤消對話框中所做的更改。值保存onClick,我一直沒有能夠想辦法臨時存儲新的值,直到用戶確認。
- 只需點擊一下,更新多條記錄,有時也當選擇在屏幕上滾過值更新

@Override 
protected Dialog onCreateDialog(int id) 
{ 
    switch (id) { 

    case 0: 
     LayoutInflater factory = LayoutInflater.from(this); 

     // Setup of the view for the dialog 
     final View bindListDialog = factory.inflate(R.layout.multi_list_layout, null); 
     multiListView = (ListView) bindListDialog.findViewById(R.id.multiList); 

     // Because I do not know how to properly handle an undo in this situation 
     // I make the dialog only close if the button is pressed and confirms the changes 
     return new AlertDialog.Builder(MultiSelectDemoActivity.this).setTitle(R.string.multiSelectTitle) 
       .setCancelable(false).setView(bindListDialog) 
       .setPositiveButton(R.string.btnClose, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) 
        { 
         updateItemList(); // In my implementation there is a list view 
              // that shows what has been selected. 
        } 
       }).create(); 
    default: 
     return null; 
    } 
} 

private static final boolean ONCREATE = true; 
private static final boolean ONUPDATE = false; 

private void setupMultiList(Boolean newList) 
{ 
    demoDBM.open(); 
    multiCur = demoDBM.getList(userId); // Gets all items tied to the user. 
    startManagingCursor(multiCur); 
    // Uses the cursor to populate a List item with an invisible ID column, 
    // a name column, and the checkbox 
    demoDBM.close(); 

    if (newList) 
    { 
     // Creates a new adapter to populate the list view on the dialog 
     multiAdapter = new SimpleCursorAdapter(this, R.layout.check_list_item, multiCur, new String[] { DemoDBM.ID, 
       DemoDBM.NAME, DemoDBM.SEL }, new int[] { R.id.itemId, R.id.itemName, R.id.itemCheck }); 
     multiAdapter.setViewBinder(new MyViewBinder()); 
     multiListView.setAdapter(multiAdapter); 
    } else 
    { 
     // updates the previously made adapter with the new cursor, without changing position 
     multiAdapter.changeCursor(multiCur); 
    } 
} 

@Override 
protected void onPrepareDialog(final int id, final Dialog dialog, Bundle args) 
{ 
    setupMultiList(ONCREATE); 
} 

public class MyViewBinder implements ViewBinder 
{ 
    @Override 
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) 
    { 
     int checkId = cursor.getColumnIndex(DemoDBM.SEL); 

     if (columnIndex == checkId) 
     { 
      CheckBox cb = (CheckBox) view; 
      // Sets checkbox to the value in the cursor 
      boolean bChecked = (cursor.getInt(checkId) != 0); 
      cb.setChecked(bChecked); // Switches the visual checkbox. 

      cb.setOnCheckedChangeListener(new MyOnCheckedChangeListener()); 
      return true; 
     } 
     return false; 
    } 
} 

public class MyOnCheckedChangeListener implements OnCheckedChangeListener 
{ 
    @Override 
    public void onCheckedChanged(CompoundButton checkBox, boolean newVal) 
    { 
     View item = (View) checkBox.getParent(); // Gets the plain_list_item(Parent) of the Check Box 

     // Gets the DB _id value of the row clicked and updates the Database appropriately. 
     int itemId = Integer.valueOf(((TextView) item.findViewById(R.id.itemId)).getText().toString()); 
     demoDBM.open(); 
     demoDBM.setChecked(itemId, userId, newVal); 
     demoDBM.close(); 

     setupMultiList(ONUPDATE); 
    } 
} 
0

android上的對話框無法修改。如果您查看源代碼,您將看到對話框構建器將所有演示文稿工作委託給某些組件,並且您在創建後無法訪問它們。因此,改變用於構建對話框的組件的狀態將不會更新對話框組件。

你可以看到這個機制herehere:你沒有訪問的onCreate被稱爲警報控制器上後訪問控制器。

如果你想實現這個目標,最好的辦法是重建一個新的活動並給它一個對話框主題。

+1

我希望的不是讓我自己的對話更簡單的方法。爲什麼可以使用填充對話框的兩個字符串數組進行編輯,而不是使用光標信息進行編輯。對我來說沒有意義。 – ecoles 2012-04-03 18:09:13

0

您可以使用AlertDialog的setCursor()方法。它非常簡單,所以你可能不需要教程。

一個相關的SO問題是here,併爲它的文檔是here

相關問題