尝试使用不同类型的listview。我现在能够在列表中显示它,实现搜索过滤器。但是,选中该复选框将检查多个复选框,这不是我想要的。因此,我面临着以下问题。
如何修改它以便只检查多次选择所需的行?
我会将此代码转换为片段,因此每当我想要更改'clerkship'时,如何刷新'listview',将再次从数据库中检索查询并重新填充列表。我是否能够从活动中调用方法或位于片段中的任何内容来触发刷新?
检索为已选中复选框的ID存储的ID。
非常感谢!
活动 - 很快就会把它移到碎片上。
@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_Encounter.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>
答案 0 :(得分:0)
您可以将检查状态保存在数组中,并在getview中设置基于位置的已检查/未检查状态。让我们说10个项目,有一个包含10个项目的bool数组,这个商店是否存储状态。然后在getView中,使用位置变量检查此数组并设置UI状态