我有一个ListView
,CustomAdapter
最多有50行(屏幕上一次只有11行)。每行包含spinner
和两个EditText
。我为这三列中的每一列传递了3组数据到适配器。当选择spinner
项或更改文本时,我想修改适配器内的相关数据集,以便通过调用activity
检索它。
我可以针对每个OnItemSelectedListener()
注册spinner
,但是,我无法找到方法来了解spinner
所在的行。因此,我无法更新数据集。
这是适配器。
SQLiteDatabase db;
Activity mActivity;
int [] mCategories;
String [] mComments;
String [] allCategories;
int [] mAmounts;
String [] spinnerValues;
TransCatListAdapter(Activity activity, int[] categories, String[] comments, int[] amounts){
super (activity, R.layout.transcat_row, comments);
mActivity = activity;
mCategories = categories;
mComments = comments;
mAmounts = amounts;
db = DatabaseHelper.getInstance(activity);
}
public View getView(int pos, View convertView, ViewGroup parent) {
View row = convertView;
if (row==null) {
LayoutInflater inflater=mActivity.getLayoutInflater();
row = inflater.inflate(R.layout.transcat_row, null);
}
Spinner SPNCategory = (Spinner) row.findViewById(R.id.trncatrow_category);
EditText ETComment = (EditText) row.findViewById(R.id.trncatrow_comment);
EditText ETAmount = (EditText) row.findViewById(R.id.trncatrow_amount);
SPNCategory.setAdapter(new CategorySpinnerAdapter(mActivity, R.layout.categoryspinnerstyle, DatabaseMethods.getCategories(db)));
ETComment.setText(mComments[pos]);
ETAmount.setText(Utils.formatAsMoneyString(mAmounts[pos]));
return (row);
}
答案 0 :(得分:2)
因此,如果我理解正确,您希望能够向您的微调器注册OnItemSelectedListener
,但您希望能够识别哪个旋转器是正确的?试试这个
getView(int pos, View convertView, ViewGroup parent) {
...
spinner SPNCategory = (Spinner) row.findViewById(R.id.trncatrow_category);
spinner.setOnItemSelectedListener(new YourSpinnerListener(pos);
...
private class YourSpinnerListener implements OnItemSelectedListener {
private int mSpinnerPosition;
public YourSpinnerListener(int spinnerPosition) {
mSpinnerPosition = spinnerPosition;
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
这个实现OnItemSelectedListener的类现在引用了微调器的位置。
玩得开心!