所以挖掘到的問題一點,經歷了幾個不同的迭代,我終於發現,我與中非常愉快的解決方案之後。隨着學校和工作的不斷推進,我很少有時間在額外的項目上工作,而我一直在坐着這個解決方案,但現在卻無法獲得發佈。
最後一塊我的難題是找到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);
}
}
我希望的不是讓我自己的對話更簡單的方法。爲什麼可以使用填充對話框的兩個字符串數組進行編輯,而不是使用光標信息進行編輯。對我來說沒有意義。 – ecoles 2012-04-03 18:09:13