在CheckEdTextView的列表中,从编程中访问特定行 - Android
-
30-09-2019 - |
题
是否可以在CheckEdTextView列表中编程访问特定行以更改其文本框的状态?
我的程序具有一个listView,其中有几个checkEdTextViews,用户可以按下切换状态。
用户离开活动时,我想保存复选框的状态,因此我在on propause方法中有:
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方法中,我回想起数据库中的检查项目,但我不知道如何将IDS返回到ListView Elements。我可以做类似的事情:
listView.getElementById(id_from_database).setChecked
?
我知道我不能使用getElementByid,但我在这里表明我的意思
提前致谢
凯文
解决方案 2
这就是我最终做的。.但这似乎是一个完整的黑客。基本上,我必须设置一个循环的双重。保存)
我的外部通过列表元素进行迭代元素,将每个ID与循环中的循环检查,并通过检查的ID列表。如果他们彼此平等,请按照检查设置该项目。
// mAdapter is contains the list of elements I want to display in my list.
ServiceList.this.setListAdapter(mAdapter);
// Getting a list of element Ids that had been previously checked by the user. getState is a function I have defined in my ServicesAdapter file.
Cursor state = ServiceList.this.application.getServicesHelper().getState(visit_id);
int checks = state.getCount();
int check_service;
int c = mAdapter.getCount();
if(checks>0){
state.moveToFirst();
for (int i=0; i<checks; i++) {
// set check_service = the next id to be checked
check_service = state.getInt(0);
for(int p=0;p<c;p++){
if(mAdapter.getItemId(p)==check_service){
// we have found an id that needs to be checked. 'p' corresponds to its position in my listView
listView.setItemChecked(p,true);
break;
}
}
state.moveToNext();
}
}
ServiceList.this.application.getServicesHelper().close();
请告诉我,有一种更有效的方法可以实现这一目标!
谢谢
凯文
其他提示
你可以打电话
listView.setItemChecked(int position, boolean value)
不隶属于 StackOverflow