Android 列表视图多检查
正在尝试使用不同类型的listview。我现在可以在列表中显示它,实现搜索过滤器。但是,选中复选框将选中多个复选框,这不是我想要的。因此,我面临以下问题 我如何修改它,使它只检查我想要的多个select行 我将把这段代码转换成一个片段,所以每次我想更改“职员”时,如何刷新“listview”,它将再次从数据库检索查询并重新填充列表。我能否从活动中调用方法或片段中的任何内容来触发刷新 检索为选中复选框的对象存储的ID 非常感谢 活动-将很快将其移动到碎片Android 列表视图多检查,android,android-layout,listview,android-listview,android-fragments,Android,Android Layout,Listview,Android Listview,Android Fragments,正在尝试使用不同类型的listview。我现在可以在列表中显示它,实现搜索过滤器。但是,选中复选框将选中多个复选框,这不是我想要的。因此,我面临以下问题 我如何修改它,使它只检查我想要的多个select行 我将把这段代码转换成一个片段,所以每次我想更改“职员”时,如何刷新“listview”,它将再次从数据库检索查询并重新填充列表。我能否从活动中调用方法或片段中的任何内容来触发刷新 检索为选中复选框的对象存储的ID 非常感谢 活动-将很快将其移动到碎片 @Override prote
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_encounter);
encouterlist = new ArrayList<HashMap<String, String>>();
myContext = this;
encounterDB = new eAndPDbAdapter(myContext);
encounterDB.open();
Cursor mCursor = encounterDB.retrieveAllEncounterCursor();
int count = mCursor.getCount();
displaySharedPreferences();
if ((mCursor != null) && (mCursor.getCount() > 0)) {
mCursor.moveToFirst();
do {
// Get the data
// Convert the initial to String
if (clerkShip.equals(mCursor.getString(2)))
{
String eID = mCursor.getString(1);
String encounter_name = mCursor.getString(3);
String encounter_type = mCursor.getString(4);
// ename = (TextView) findViewById(R.id.eid);
// this.ename.setTextColor(Color.RED);
//indicate that its successful
Log.i("Successful retrival of", encounter_name);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_E_ID, eID);
map.put(TAG_E_NAME, encounter_name);
map.put(TAG_E_TYPE, encounter_type);
// Add it to the array such that i can handle the array
// afterwhich
encouterlist.add(map);
}
}
// move to the next row
while (mCursor.moveToNext());
}
filterText = (EditText) findViewById(R.id.encountersSearch);
filterText.addTextChangedListener(filterTextWatcher);
// need to link the layout
encounterList = (ListView) findViewById(R.id.encounterslist);
cBox = (CheckBox) findViewById(R.id.listCheck);
encounterList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
((CheckBox)parent.getChildAt(position).findViewById(R.id.listCheck)).setChecked(true);
}
}
);
this.adapter = new SimpleAdapter(EncounterActivity.this,
encouterlist, R.layout.list_v, new String[] { TAG_E_ID,
TAG_E_NAME, TAG_E_TYPE,
TAG_E_REQUIRED_ATTEMPTS }, new int[] { R.id.eid,
R.id.ename,
R.id.etype });
this.encounterList.setAdapter(adapter);
};
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
((Filterable) adapter).getFilter().filter(s);
}
};
public void displaySharedPreferences() {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(EncounterActivity.this);
String listPrefs = prefs.getString("listpref", "No Clerkship Selected");
StringBuilder builder2 = new StringBuilder();
builder2.append(listPrefs);
clerkShip = builder2.toString();
}
Activity_.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">
<EditText
android:id="@+id/encountersSearch"
android:hint="Enter encounter here"
android:layout_width="fill_parent"
android:windowSoftInputMode="stateHidden"
android:imeOptions="actionSearch"
android:singleLine="true"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/encounterslist"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
list_v.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/eid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/listCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:clickable="false"
android:focusable="false" >
</CheckBox>
<TextView
android:id="@+id/ename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left" />
</LinearLayout>
<TextView
android:id="@+id/etype"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/erequiredattempts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
您可以将检查状态保存在数组中,并在getview中根据位置设置检查/未检查状态。假设10个项目,有一个包含10个项目的bool数组,该数组存储选中状态或未选中状态。然后在getView中,使用位置变量检查此数组并设置UI状态ListView.CHOICE\u MODE\u MULTIPLE阅读更多:我面临ListView回收视图问题。因此,如果我选中项目1并向下滚动。第17项和第33项也将被选中。这就是我说的,如果你有100项,你需要有100项的布尔数组,arr[0]=true选中,在你的getView中做ifarr[position]{//set checkbox checked}//else{//uncheck},位置来自getView参数。我想你会有一个click listener for复选框,每次选中时,将该特定实例设置为true@Walalala您可以使用SparseBooleanArray。再次检查Romain Guy的解决方案@和我一样什么posted@PulkitSethi您最好发布带有代码的解决方案。