[英]How can I get my checked values from a ListView populated by a cursor within an AlertDialog?
I created an AlertDialog
that has a ListView
of items along with check boxes. 我创建了一个AlertDialog
,它具有项目的ListView
和复选框。 I want to be able to collect the checked items in the calling activity so that I can use them. 我希望能够在呼叫活动中收集选中的项目,以便可以使用它们。
I have a button in my activity: 我的活动中有一个按钮:
<Button android:id="@+id/my_button" />
That I get like so: 我得到这样的:
Button button = (Button)findViewById(R.id.my_button);
Then I set a listener: 然后我设置一个监听器:
button.setOnClickListener(my_on_click_listener);
View.OnClickListener my_on_click_listener = new View.OnClickListener(){
@Override
public void onClick(View v){
my_method();
}
};
That calls a 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();
}
Here is my_listview_layout.xml : 这是my_listview_layout.xml :
<ListView android:id="@+id/my_listview" />
Here is my_layout.xml : 这是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>
See my two comments in the code above. 请参阅上面的代码中的两个注释。
So, how can I get my checked values from a ListView populated by a cursor within an AlertDialog in Android? 因此,如何从Android的AlertDialog中的游标填充的ListView中获取检查值?
UPDATE: 更新:
Within my code, I commented out this line: 在我的代码中,我注释掉了这一行:
listView.setAdapter(simpleCursorAdapter);
and changed this line: 并更改此行:
dialogBuilder.setMultiChoiceItems(cursor, "", "", new DialogBuilder.OnMultiChoiceClickListener();
to this: 对此:
dialogBuilder.setMultiChoiceItems(cursor, "_id", "value", new DialogBuilder.OnMultiChoiceClickListener()
and now my dialog is rendering properly, and I can determine which items are checked. 现在我的对话框可以正确显示,我可以确定要检查的项目。 I guess [maybe] both the ListView
and setMultiChoiceItems()
were fighting for control over drawing the list of choices. 我想[也许] ListView
和setMultiChoiceItems()
都在争夺对绘制选择列表的控制权。
However, I have a new issue. 但是,我有一个新问题。 if I check some items at the top of the list, then scroll down and back up, the items I checked have been unchecked. 如果我检查列表顶部的某些项目,然后向下滚动并向上备份,则我选中的项目未选中。 That, and the first item in the list is checked by default and I cannot figure out how to uncheck it. 那,并且列表中的第一项是默认选中的,我无法弄清楚如何取消选中它。
Any ideas??? 有任何想法吗???
Thanks ... again. 再次感谢。
You can do something like this: 您可以执行以下操作:
// 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();
The process that you have used might be a bit tedious. 您使用的过程可能有点乏味。 So, use this way it'll be easy to access and retain values. 因此,使用这种方式可以很容易地访问和保留值。 Hope that it'll be helpful. 希望对您有所帮助。
Check this solution https://stackoverflow.com/a/31315985/2610010 检查此解决方案https://stackoverflow.com/a/31315985/2610010
First Create a custom ArrayList and add all your items in custom ArrayList 首先创建一个自定义ArrayList并将所有项目添加到自定义ArrayList中
Set your boolean in your adapter on CheckBox click 在CheckBox上的适配器中设置布尔值,然后单击
holder.checkBox.setChecked(actorList.get(position).isSelected());
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isSelected = ((CheckBox)v).isChecked();
actorList.get(position).setSelected(isSelected);
}
});
and on Click of your button use following code 然后点击按钮,使用以下代码
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Service> actorList = ((ServiceAdapter)listView.getAdapter()).getSelectActorList();
Toast.makeText(MainActivity.this,""+actorList.size(),Toast.LENGTH_LONG).show();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.