[英]Getting Switch state in ListView
in my Application I want the user to make multiple choices and for this i present some Switch in a ListView and a button to confirm. 在我的应用程序中,我希望用户做出多个选择,为此,我在ListView中显示一些Switch和一个确认按钮。
The problem is, when I inserted the values in db in the Button onClickListener() function, i can retrieve from the switches the text, but not the state of them (in the example, always output true) 问题是,当我在Button onClickListener()函数中的db中插入值时,我可以从开关中检索文本,但不能检索它们的状态(在示例中,始终输出true)
btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
ListAdapter adapter = list.getAdapter();
MyDatabase db = new MyDatabase(cntx);
db.open();
for(int n=0;n<adapter.getCount();n++)
{
View rowview = adapter.getView(n, null, null);
Switch swtc = (Switch) rowview.findViewById(R.id.switch1);
String[] args = {(String)swtc.getText()};
if(swtc.isChecked())
{
Log.i("update", args[0]+" going to be true");
}
else
{
Log.i("update", args[0]+" going to be false");
}
int a = db.updateAlert(swtc.isChecked(), MyDatabase.Tab_Cal_Set.Cal+"=?", args);
Toast.makeText(cntx, "affected: "+Integer.toString(a), Toast.LENGTH_LONG).show();
}
db.close();
}
});
PS: adapter is the arrayadapter I've created for the ListView, cntx is the context of the application, and MyDatabase is my custom class that provide to manage an Sqlite Database. PS:适配器是我为ListView创建的arrayadapter,cntx是应用程序的上下文,MyDatabase是提供用于管理Sqlite数据库的自定义类。 Hope you can help me
希望你能帮我
code of arrayadapter: arrayadapter的代码:
public class NotificationElementList extends ArrayAdapter<String>{
private final Activity context;
private final List<String> titles;
private final List<String> imgs;
private final List<Boolean> alert;
public NotificationElementList(Activity context, List<String> titles, List<String> imgs, List<Boolean> alert) {
super(context, R.layout.list_single, titles);
this.context = context;
this.titles = titles;
this.imgs = imgs;
this.alert = alert;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.notification_list_element, null, true);
Switch switch_text = (Switch) rowView.findViewById(R.id.switch1);
switch_text.setChecked(alert.get(position));
switch_text.setText(titles.get(position));
return rowView;
}
} }
As you are not providing the convertView argument to ArrayAdapter.getView method, it will always create a new view hierarchy for you. 由于您没有为ArrayAdapter.getView方法提供convertView参数,因此它将始终为您创建一个新的视图层次结构。 As a result you will always see your switch widgets' state initialized to alert.get(position) .
结果,您将始终看到开关小部件的状态已初始化为alert.get(position) 。
One of the potential solutions could be to set a OnCheckedChangeListener to Switch widgets and update model whenever switch state changes. 一种可能的解决方案是将OnCheckedChangeListener设置为Switch小部件并在开关状态更改时更新模型。 For example like this
像这样
@Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.notification_list_element, null, true);
Switch s = (Switch) rowView.findViewById(R.id.switch1);
s.setChecked(alert.get(position));
s.setText(titles.get(position));
s.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
alert.set(position, isChecked);
}
});
return rowView;
}
Doing so will allow you to obtain the state directly from model as opposed to trying to obtain it from switch widget in your button click event handler. 这样做将允许您直接从模型获取状态,而不是尝试从按钮单击事件处理程序中的开关小部件获取状态。 Instead of doing this
而不是这样做
View rowview = adapter.getView(n, null, null);
Switch swtc = (Switch) rowview.findViewById(R.id.switch1);
db.updateAlert(swtc.isChecked(), MyDatabase.Tab_Cal_Set.Cal+"=?", args);
You would do 你会做
db.updateAlert(alert.get(n), MyDatabase.Tab_Cal_Set.Cal+"=?", args);
In addition, I suggest you update the adapter to utilize the view holder pattern for better performance. 另外,建议您更新适配器以利用视图持有者模式以获得更好的性能。
You should not call adapter getView method. 您不应调用适配器的getView方法。 It is used internally by ListView.
ListView在内部使用它。
Its purpose is say to ListView how to populate its contents. 它的目的是说ListView如何填充其内容。
If the alert list
is used to save the Switch
checked state you can use it. 如果
alert list
用于保存“ Switch
选中状态,则可以使用它。
btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDatabase db = new MyDatabase(cntx);
db.open();
for(int n=0;n<alert.size();n++)
{
String[] args = {titles.get(n)};
if(alert.get(n))
{
Log.i("update", args[0]+" going to be true");
}
else
{
Log.i("update", args[0]+" going to be false");
}
int a = db.updateAlert(alert.get(n), MyDatabase.Tab_Cal_Set.Cal+"=?", args);
Toast.makeText(cntx, "affected: "+Integer.toString(a), Toast.LENGTH_LONG).show();
}
db.close();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.