2012-04-03 6 views
2

カーソルを使用した複数選択に役立つチュートリアルが見つかりませんでした。現時点では、私のロジックは私が望むように動作していますが、チェックボックスは正しく更新されません。私は何を見落としていますか?複数の選択をアンドロイドのダイアログウィンドウで更新するには

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関数を見つけることでした。これは、ロードするDBとのもはや一致しない古いデータの問題を修正しました。私の現在のハードルは、ボックスをチェックするのにかかる時間です。クリックしてから更新されるまでには、明らかな遅れがあります。私は、複数のレコードがクリックされたときに更新されることを発見しました。私はこれらの余分な更新の正当な理由を見つけることができませんでした。

以下は、私が現在マルチセレクト機能を実装しているコードです。これはちょうどダイアログコードです。実用的なデモのために、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

アンドロイドのダイアログは変更できません。ソースコードを見ると、dialogbuilderはすべてのプレゼンテーション作業をいくつかのコンポーネントに委譲し、作成後にそれらにアクセスすることはできません。したがって、ダイアログを構築するために使用するコンポーネントの状態を変更しても、後でダイアログコンポーネントは更新されません。

herehereが表示されます。警告コントローラでonCreateが呼び出された後にアクセスコントローラにアクセスできない。

これを達成したい場合は、新しいアクティビティを再作成してダイアログテーマを作成することをお勧めします。

+1

を更新オフにスクロールするときにも、時々私は自分のダイアログを作るよりも簡単な方法を期待していた、複数のレコードを更新します。なぜ、2つの文字列配列を編集して、カーソル情報を持つものではなくダイアログに記入することができますか。私には意味がありません。 – ecoles

0

AlertDialogにはsetCursor()メソッドを使用できます。そのシンプルなので、おそらくチュートリアルは必要ありません。

関連のSOの質問はhereあり、それのためのドキュメントはそう問題にビットを掘り、私は最終的に私はとかなり満足している解決策を見つけたカップルの異なるイタレーションを経た後にhere

関連する問題