我在ListView
中遇到Spinners
問題。我有一個ListView
與CheckBox
,一個標籤,和兩個Spinners
。從SQLite填充Spinner
並且工作正常。我沒有使用ViewHolder
方法,因爲到目前爲止,當單擊ListView
行時,CheckBoxes
被選中或取消選中,並且更改立即保存到數據庫。當檢查行時,Spinners
變得可見,但在未檢查行時不可見。Android Spinners在ListView中,我必須使用ViewHolder嗎?
所以我還沒有設法找到解決方案的問題是,我不知道如何獲得實際的Spinner
甚至得到點擊Spinner
的ListItem
行。 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);
感謝。
添加適配器的代碼。 – Luksprog
我意識到我可能會通過使用'ViewHolder'來避免在每次調用setViewValue時查看視圖,但我對此並不熟悉,並且還沒有弄清楚它們的工作方式以及if我可以立即將更改提交給數據庫。 – collusionbdbh
你想要什麼樣的紡紗廠? 「CheckBoxes」真的有效嗎?現在你從數據庫中正確地設置了CheckBoxes的值,但是你沒有連接到它們的監聽器,所以可能他們正在工作,但它不應該能夠將新狀態保存到數據庫中。 – Luksprog