gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:38:55 26 4
gpt4 key购买 nike

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

所以我还没有设法找到解决方案的问题是我不知道如何获得实际的 Spinner 或什至获得 ListItem 行单击的 Spinner 已打开。 Activity 扩展了 ListActivity。任何人都知道我可以在不使用 ViewHolder 的情况下执行此操作的方法,还是必须使用 ViewHolder

这是声明和填充 ListView 的代码:

mSsCursor = mDbHelper.fetchAllSsPlaylistSs(mPlId);
startManagingCursor(mSsCursor);

String[] from = new String[]{"pl_selected", BTDbAdapter.KEY_NAME, BTDbAdapter.KEY_NAME2};

int[] to = new int[]{R.id.pl_selected, R.id.name, R.id.name2};

mAllSs = new SimpleCursorAdapter(this, R.layout.pl_edit_ss_row, mSsCursor, from, to);
mAllSs.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
//custom handling of setting the value
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == 3) {
ViewGroup row = (ViewGroup)view.getParent().getParent();
mSId = cursor.getInt(0);
if (cursor.getInt(3) > 0) {
mCheckBox = (CheckBox) row.findViewById(R.id.pl_selected);
mCheckBox.setChecked(true);
mTSpin = (Spinner) row.findViewById(R.id.pl_t_spin);
mMSpin = (Spinner) row.findViewById(R.id.pl_m_spin);
mtvT = (TextView) row.findViewById(R.id.pl_t);
mtvM = (TextView) row.findViewById(R.id.pl_m);
mTSpin.setVisibility(View.VISIBLE);
mtvT.setVisibility(View.VISIBLE);
mMSpin.setVisibility(View.VISIBLE);
mtvM.setVisibility(View.VISIBLE);
//set the values in the t spinner
PopulateTSpinner(cursor.getInt(4));
//set the values in the m spinner
PopulateMSpinner(cursor.getInt(5));
}
else {
mCheckBox = (CheckBox) row.findViewById(R.id.pl_selected);
mCheckBox.setChecked(false);
mTSpin = (Spinner) row.findViewById(R.id.pl_t_spin);
mMSpin = (Spinner) row.findViewById(R.id.pl_m_spin);
mtvT = (TextView) row.findViewById(R.id.pl_t);
mtvM = (TextView) row.findViewById(R.id.pl_m);
mTSpin.setVisibility(View.GONE);
mtvT.setVisibility(View.GONE);
mMSpin.setVisibility(View.GONE);
mtvM.setVisibility(View.GONE);
}
return true;
}
return false;
}
});
setListAdapter(mAllSs);

谢谢。

最佳答案

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

show a list of data(CheckBox + TextView(Spinners hidden)) ->
user clicks a row(the Spinners appear for that row with(individual) data) ->
user selects something in those Spinners-> save that selection in the database

然后我认为您应该使用自定义适配器并自行处理行创建 + 数据绑定(bind)(我不明白您将如何为 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 移出 ListView 行。

关于ListView 中的 Android Spinners,我必须使用 ViewHolder 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10816765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com