我创建了一个 AlertDialog
,其中包含项目的 ListView
以及复选框。我希望能够收集调用 Activity 中选中的项目,以便我可以使用它们。
我的 Activity 中有一个按钮:
<Button android:id="@+id/my_button" />
我是这样的:
Button button = (Button)findViewById(R.id.my_button);
然后我设置了一个监听器:
button.setOnClickListener(my_on_click_listener);
View.OnClickListener my_on_click_listener = new View.OnClickListener(){
@Override
public void onClick(View v){
my_method();
}
};
调用一个方法:
public void my_method(){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater layoutInflater = this.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.my_listview_layout, null);
dialogBuilder.setView(view);
final DatabaseHelper databaseHelper = new DatabaseHelper(this);
String[] fromColumns = {"_id","value"};
int[] toViews = {R.id.dropdown_id, R.id.dropdown_value};
final ListView listView = (ListView)view.findViewById(R.id.my_listview);
final Cursor cursor = databaseHelper.getData();
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.my_layout, cursor, fromColumns, toViews, 0);
listView.setAdapter(simpleCursorAdapter);
// I tried this, but I'm not sure what to use for the isCheckedColumn and labelColumn
dialogBuilder.setMultiChoiceItems(cursor, "", "", new DialogBuilder.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked){
// do stuff here ...
}
});
dialogBuilder.setPositiveButton("Save Selected", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
// I tried this, but it only collects the elements within view
for(int i=0; i<listView.getCount(); i++){
LinearLayout ll = (LinearLayout)listView.getChildAt($i);
if(null != ll){
CheckBox cb = (CheckBox)ll.findViewById(R.id.dropdown_checkbox);
TextView tv01 = (CheckBox)ll.findViewById(R.id.dropdown_id);
TextView tv02 = (CheckBox)ll.findViewById(R.id.dropdown_value);
}
}
}
});
final AlertDialog alert = dialogBuilder.create();
alert.setTitle("Select Choices");
alert.show();
}
这是my_listview_layout.xml:
<ListView android:id="@+id/my_listview" />
这是my_layout.xml:
<LinearLayout>
<CheckBox android:id="@+id/dropdown_checkbox" />
<TextView android:id="@+id/dropdown_value" />
<TextView android:id="@+id/dropdown_id" android:visibility="invidible"/>
</LinearLayout>
请参阅上面代码中我的两条评论。
那么,如何从 Android 中的 AlertDialog 中的光标填充的 ListView 中获取选中的值?
----
更新:
在我的代码中,我注释掉了这一行:
listView.setAdapter(simpleCursorAdapter);
并更改了这一行:
dialogBuilder.setMultiChoiceItems(cursor, "", "", new DialogBuilder.OnMultiChoiceClickListener();
对此:
dialogBuilder.setMultiChoiceItems(cursor, "_id", "value", new DialogBuilder.OnMultiChoiceClickListener()
现在我的对话框已正确呈现,我可以确定检查哪些项目。我猜想 [也许] ListView
和 setMultiChoiceItems()
都在争夺对绘制选项列表的控制权。
但是,我有一个新问题。如果我检查列表顶部的某些项目,然后向下滚动并向上滚动,则我检查的项目已被取消选中。那,列表中的第一项默认情况下是选中的,我不知道如何取消选中它。
有什么想法吗???
再次感谢...。
最佳答案
你可以这样做:
// Fetch the list of items from any of your sources and store it as an array list.
// Here I'm considering itemList as the source it is a string array but you
can use an array list rather.
final ArrayList<String> selectedItemList = new ArrayList<>();
final int itemListLength = itemList.length;
final boolean[] selectionPreference = new boolean[itemListLength];
for(int i=0; i < itemListLength; i++){
Arrays.fill(selectionPreference, Boolean.FALSE);
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Select one")
.setMultiChoiceItems(itemList, selectionPreference,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
String item = itemList[which];
if(isChecked){
selectionPreference[which] = true;
} else {
selectionPreference[which] = false;
}
}
})
.setPositiveButton("ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i=0; i<itemListLength; i++) {
if(selectionPreference[i]) {
selectedItemList.add(itemList[i]);
}
}
Log.i(TAG, String.valueOf(selectedItemList));
}
}).show();
您使用的过程可能有点乏味。因此,使用这种方式可以轻松访问和保留值。希望对您有所帮助。
关于android - 如何从 AlertDialog 中由光标填充的 ListView 获取选中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48120021/