以编程方式访问 CheckedTextView 列表中的特定行 - Android
是否可以以编程方式访问 CheckedTextView 列表中的特定行来更改其文本框的状态?
我的程序有一个列表视图,其中有几个 CheckedTextView,用户可以按这些视图来切换状态。
我想在用户离开活动时保存复选框的状态,因此我在 onPause 方法中使用:
public void onPause(){
super.onPause();
SparseBooleanArray positions;
positions = listView.getCheckedItemPositions();
ListAdapter items = listView.getAdapter();
int j = items.getCount();
ArrayList<Long> ids = new ArrayList<Long>();
for (int k =0; k < j;k++){
if(positions.get(k)==true){
ids.add(items.getItemId(k));
}
}
this.application.getServicesHelper().open();
this.application.getServicesHelper().storeServices(ids,visit_id);
this.application.getServicesHelper().close();
}
它非常简单地迭代列表视图,将选中的项目添加到 ArrayList,然后将该 id 列表保存到数据库。
我的问题在于一旦用户返回该活动就尝试重置列表。
到目前为止,在我的 onStart 方法中,我记得从数据库中检查过的项目,但我不知道如何将返回的 id 传递给 listview 元素。我可以做类似的事情:
listView.getElementById(id_from_database).setChecked
吗?
我知道我不能使用 getElementById 但我在这里用它来展示我的意思
提前致
谢凯文
is it possible to programatically access specific rows in a list of CheckedTextViews to change the state of their textboxes?
my program has a listview which has several CheckedTextViews which the user can press to toggle state.
I want to save the state of the checkboxes when the user leaves the activity, so I have in my onPause method:
public void onPause(){
super.onPause();
SparseBooleanArray positions;
positions = listView.getCheckedItemPositions();
ListAdapter items = listView.getAdapter();
int j = items.getCount();
ArrayList<Long> ids = new ArrayList<Long>();
for (int k =0; k < j;k++){
if(positions.get(k)==true){
ids.add(items.getItemId(k));
}
}
this.application.getServicesHelper().open();
this.application.getServicesHelper().storeServices(ids,visit_id);
this.application.getServicesHelper().close();
}
which very simply iterates the list view, adds the checked items to an ArrayList and then saves that list of ids to the database.
My problem lise in trying to reset the list once a user goes back to that activity.
so far in my onStart method, I recall the checked items from the database, but I do not know how to march the ids returned to the listview elements. can I do something like:
listView.getElementById(id_from_database).setChecked
?
I know I cant use getElementById but I have it here to show what I mean
Thanks in advance
Kevin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以致电
You can call
这就是我最终所做的……但这看起来像是一个完整的黑客。
基本上我必须设置一个双for循环..一个循环遍历我的列表元素,一个循环遍历我已检索检查列表状态的光标(状态最后一次检查时检查的元素的id的简单数组)保存)
我的外部 for 迭代列表元素,检查每个 id 与要设置为检查的 id 列表的循环。如果它们彼此相等,则将该项目设置为选中。
请告诉我有更有效的方法来实现这一目标!
谢谢
凯文
This is what Ive ended up doing.. but it seems like a complete hack.
Basically I have to set up a double for loop.. one to iterate through my list elements, and one to iterate through the cursor that I have retreived my check list state (a simply array of ids of elements that were checked when state was last saved)
my outer for iterates through the list elements checking each id against a loop through the list of ids to be set as checked. if they equal each other then set that item as checked.
Please tell me there is a more efficient way of achieving this!!
Thanks
Kevin