ListView中的Android Spinners,我是否必须使用ViewHolder?

时间:2012-05-30 12:55:36

标签: android android-listview android-spinner

我在Spinners中遇到ListView问题。我有一个ListView CheckBox,一个标签和两个SpinnersSpinner是从SQLite填充的,并且工作正常。我没有使用ViewHolder方法,因为到目前为止,当单击ListView行时,CheckBoxes被选中或取消选中,并且更改会立即保存到数据库中。选中该行后,Spinners会变为可见,但在未选中该行时则不可见。

因此,我未能找到解决方案的问题是,我不知道如何获取实际Spinner甚至获得点击ListItem的{​​{1}}行}是的。 Spinner扩展Activity。任何人都知道我可以在不使用ListActivity的情况下执行此操作,还是必须使用ViewHolder

以下是声明和填充ViewHolder

的代码
ListView

感谢。

1 个答案:

答案 0 :(得分:2)

我不知道我是否理解了您的问题:如果您的应用流程是:

  

显示数据列表(CheckBox + TextViewSpinners隐藏)) - >
  用户点击一行({(1}}显示该行包含(个别)数据) - >
  用户选择那些Spinners - >中的内容   将该选择保存在数据库中

然后我认为你应该使用自定义适配器并自己处理行创建+数据绑定(我不知道你将如何为Spinners设置一个监听器)。下面是一个关于如何做到这一点的小例子(尽管可能不是一种很好的方式):

Spinners

此外,所需的public class CustomAdapter extends SimpleCursorAdapter { private LayoutInflater mInflater; public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); mInflater = LayoutInflater.from(context); } @Override public void bindView(View view, Context context, Cursor cursor) { ViewHolder holder = (ViewHolder) view.getTag(); // the holder // pattern // set the text for the TextView in your row holder.name .setText(cursor.getString(cursor.getColumnIndex("name"))); // status of the CheckBox from the database int status = cursor.getInt(cursor.getColumnIndex("pl_selected")); // set the CheckBox status holder.ckb.setChecked((status > 0) ? true : false); // get the id of this particular row, we'll use this later in the // Spinner's listeners long theId = cursor.getLong(cursor.getColumnIndex("_id")); // see if it is time to show the Spinners if (status > 0) { // it is time to show the Spinners. Here you would do stuff // like: setting up the Spinner's adapters + setting the // listener // I used a Spinner with entries set in the xml layout(so my // selection result is a String) holder.spin1.setVisibility(View.VISIBLE); holder.spin2.setVisibility(View.VISIBLE); // set theId as a tag so you know which Spinner was acted on holder.spin1.setTag(new Long(theId)); holder.spin1 .setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Long realRowId = (Long) parent.getTag(); // I don't know ContentValues cv = new ContentValues(); // the column where I saved the spinner selected // item is called "saved_item" cv.put("saved_item", (String) parent .getItemAtPosition(position)); // mDb is my SQLiteDatabase instance mDb.update("tbl", cv, "_id = ?", new String[] { String .valueOf(realRowId) }); // I don't know how you saved the data, the // above is just an example } @Override public void onNothingSelected(AdapterView<?> parent) { } }); // also implement the second Spinner like the first one } else { // required to prevent a recycled View from causing damage holder.spin1.setVisibility(View.GONE); holder.spin2.setVisibility(View.GONE); } } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { View v = mInflater.inflate(R.layout.adapters_listspinner_row, parent, false); ViewHolder holder = new ViewHolder(); holder.spin1 = (Spinner) v.findViewById(R.id.spinner1); holder.spin1.setFocusable(false); holder.spin2 = (Spinner) v.findViewById(R.id.spinner2); holder.spin2.setFocusable(false); holder.name = (TextView) v.findViewById(R.id.textView1); holder.ckb = (CheckBox) v.findViewById(R.id.checkBox1); holder.ckb.setFocusable(false); v.setTag(holder); return v; } class ViewHolder { Spinner spin1, spin2; TextView name; CheckBox ckb; } } 方法:

onListItemcClick

作为建议,也许您可​​以修改应用的布局并将@Override protected void onListItemClick(ListView l, View v, int position, long id) { // manage the CheckBox state CheckBox ckb = (CheckBox) v.findViewById(R.id.checkBox1); ckb.setChecked(!ckb.isChecked()); ContentValues cv = new ContentValues(); cv.put("pl_selected", ckb.isChecked() ? 1 : 0); mDb.update("tbl", cv, "_id = ?", new String[] { String.valueOf(id) }); // requery the database so the changes are seen by the adapter, this is horrible! Cursor re = mDb.query("tbl", null, null, null, null, null, null); mAllSs.changeCursor(re); } 移出Spinners行。