سؤال

I have a ListView with a CustomAdapter that has up to 50 rows( only 11 on the screen at once). Each row contains a spinner and two EditTexts. I pass 3 sets of data to the adapter for each of the three columns. When a spinner item is selected or text changed I want to amend the relevant data set inside the adapter so that it can be retrieved by the calling activity.

I can get a OnItemSelectedListener() registered against each spinner, however, I can't find a way to know which row the spinner was on. Because of that I can't update the data set.

This is the adapter.

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);
}
هل كانت مفيدة؟

المحلول

So if I understand correctly, you want to be able to register an OnItemSelectedListener to your spinners but you want to be able to identify WHICH spinner it was right? Try this

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

    }
}

This class that implements the OnItemSelectedListener now has a reference to the position of the spinner.

Have fun!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top