我怎样才能更新在Android对话窗口多选择
问题描述:
我一直无法找到一个教程,帮助使用光标多选。截至目前,我的逻辑按照我想要的方式工作,但复选框不会正确更新。我忽略了什么?我怎样才能更新在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();
答
所以挖掘到的问题一点,经历了几个不同的迭代,我终于发现,我与中非常愉快的解决方案之后。随着学校和工作的不断推进,我很少有时间在额外的项目上工作,而我一直在坐着这个解决方案,但现在却无法获得发布。
最后一块我的难题是找到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