编辑:使用其他方法显示数据。
我现在能够显示数据并确保没有内存泄漏或listview的回收。另外,我可以根据另一列的数据更改文本的颜色。
检查复选框的onclick功能也已完成。
然而,我仍然缺乏一部分。按下提交按钮后获取数据。似乎无法逐行获取复选框。
public class EncounterActivity extends Activity {
ListView list;
TextView eid;
TextView eclerkship;
TextView ename;
TextView etype;
TextView erequiredattempts;
// DATABASE ADAPTOR
private eAndPDbAdapter encounterDB;
private Context myContext = EncounterActivity.this;
//arrayAdapter
ListAdapter adapter;
CheckBox cBox = null;
//Button Btngetdata;
ArrayList<HashMap<String, String>> encountersList = new ArrayList<HashMap<String, String>>();
//various layout
ListView encounterList;
static String value;
private EditText filterText = null;
private Cursor mCursor;
String clerkShip = null;
//JSON Node Names
private static final String TAG_ENCOUNTERS = "encounters";
private static final String TAG_E_ID = "e_id";
private static final String TAG_E_CLERKSHIP = "clerkship";
private static final String TAG_E_NAME = "encounter_name";
private static final String TAG_E_TYPE = "type";
private static final String TAG_E_REQUIRED_ATTEMPTS = "required_attempts";
//JSONArray android = null;
private List nameList = new ArrayList();
private List idList = new ArrayList();
private List typeList = new ArrayList();
public List encounterSelected = new ArrayList();
public void displayList()
{
displaySharedPreferences();
encounterDB = new eAndPDbAdapter(myContext);
encounterDB.open();
mCursor = encounterDB.retrieveAllEncounterCursor(clerkShip);
if ((mCursor != null) && (mCursor.getCount() > 0)) {
mCursor.moveToFirst();
{
do {
String eID = mCursor.getString(1);
String encounter_name = mCursor.getString(3);
String encounter_type = mCursor.getString(4);
this.idList.add(eID);
this.nameList.add(encounter_name);
this.typeList.add(encounter_type);
}
// move to the next row
while (mCursor.moveToNext());
}
}
encounterDB.close();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_encounter);
encountersList = new ArrayList<HashMap<String, String>>();
displayList();
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.encounterslist);
for(int i = 0 ; i<nameList.size();i++)
{
View v = getLayoutInflater().inflate(R.layout.list_v, null);
final LinearLayout single = (LinearLayout) v.findViewById(R.id.listV);
single.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout nestedLL = (LinearLayout) single.getChildAt(1);
CheckBox cb = (CheckBox) nestedLL.getChildAt(0);
if(cb.isChecked())
{
cb.setChecked(false);
single.setBackgroundColor(Color.TRANSPARENT);
}
else
{
cb.setChecked(true);
single.setBackgroundColor(Color.DKGRAY);;
}
}
});
TextView ID = (TextView) v.findViewById(R.id.eid);
TextView name = (TextView) v.findViewById(R.id.ename);
TextView type = (TextView) v.findViewById(R.id.etype);
String nameValue = nameList.get(i).toString();
String IDValue = idList.get(i).toString();
String typeValue = typeList.get(i).toString();
if (typeValue.equals("Must see"))
{
name.setTextColor(Color.RED);
}
else
{
name.setTextColor(Color.GREEN);
}
name.setText(nameValue);
ID.setText(IDValue);
parentLayout.addView(v);
}
myContext = this;
};
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();
}
public static class ViewHolder {
public TextView mProductNameTextView;
public TextView mProductIdTextView;
}
protected void onDestroy() {
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.encounter, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_encounter_save:
openEncounterSave();
return true;
case R.id.action_settings:
openSettings();
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("unchecked")
public void openEncounterSave() {
LinearLayout ListV =(LinearLayout) findViewById(R.id.encounterslist);
//Get the number of rows - Correct number of row.
int RowIndex = ListV.getChildCount();
for(int i = 0 ; i<RowIndex;i++)
{
//Go through 1 row by 1 row.
View v = getLayoutInflater().inflate(R.layout.list_v, null);
final LinearLayout single = (LinearLayout) v.findViewById(R.id.listV);
//Open the nested LinearLayout.
LinearLayout nestedLL = (LinearLayout) single.getChildAt(1);
//get the first item in the nested LinearLayout
//In this case it's the CheckBox.
CheckBox cb = (CheckBox) nestedLL.getChildAt(0);
boolean validation = cb.isChecked();
if (validation == true)
{
String nameValue = nameList.get(RowIndex).toString();
Toast.makeText(myContext, nameValue, Toast.LENGTH_SHORT).show();
String IDValue = idList.get(RowIndex).toString();
encounterSelected.add(IDValue);
//Retrieve the ID value and add it into the EncouterSelected ArrayList.
}
}
//Sort the array on order before sending it back to NewCases
Collections.sort(encounterSelected);
StringBuilder full = new StringBuilder();
String value = full.append(encounterSelected).toString();
// Intent encounterIntent = new Intent();
// encounterIntent.putExtra("EncounterArray", value);
// setResult(-1, encounterIntent);
// finish();
}
public void openSettings() {
Intent settingsIntent = new Intent(this, PrefsActivity.class);
startActivity(settingsIntent);
}
}
EncounterActivity.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">
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/encounterslist"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</ScrollView>
</LinearLayout>
List_V.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listV"
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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</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)
Toast.makeText(EncounterActivity.this,encouterlist.get(+position).get("encounter_name") + " Selected",Toast.LENGTH_SHORT).show();
这个 +位置是什么?尝试删除此Toast并检查它是否有效。我认为问题出在这个Toast上。试试这个
CheckBox checkBox = (CheckBox) view.findViewById(R.id.listCheck);
if (null != checkBox)
checkBox.setChecked(true);