如何使用arraylist从arrayadapter设置微调器选择

时间:2016-07-23 16:45:38

标签: android arraylist android-spinner pojo

我的sqlite数据中有一个arraylist。我将这个arraylist设置在我的微调器上。

以下是我的表现方式:

 public ArrayList<AttendantModelNames> getAllAttendantNames() {
        ArrayList<AttendantModelNames> attendantModelArrayList = new ArrayList<>();
        hp = new HashMap();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("SELECT  * FROM " + TABLE_ATTENDANTS, null);
        res.moveToFirst();
        while (!res.isAfterLast()) {
            AttendantModelNames l_att = new AttendantModelNames();
            l_att.setname(res.getString(res
                    .getColumnIndex(KEY_NAME)));
            attendantModelArrayList.add(l_att);
            res.moveToNext();
        }
        Log.d(TAG, attendantModelArrayList.toString());
        res.close();
        return attendantModelArrayList;
    }

然后我的AttendantModelNames POJO:

public class AttendantModelNames {
    String name;

    public String getname() {
        return name;
    }

    public void setname(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

然后在我的微调器上:

private void setSpinner() {
        spinner = (Spinner) findViewById(com.duka.R.id.spinner_toolbar);
        ArrayList<AttendantModelNames> modelArrayList = ah.getAllAttendantNames();
        ArrayAdapter<AttendantModelNames> dataAdapter =
                new ArrayAdapter<AttendantModelNames>(this, android.R.layout.simple_spinner_item, modelArrayList);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


        spinner.setAdapter(dataAdapter);

int selectionPosition= dataAdapter.getPosition(modelArrayList.get(position).getname());
spinner.setSelection(selectionPosition);

我试过设置这个,但似乎没有用,说:

  arraylist中的

get(int)不能应用于(java.lang.String)on   位置

任何建议都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

这就是我解决问题的方法:

我首先返回一个字符串类型列表:

public List<String> getAllAttendantNames() {
        List<String> list = new ArrayList<String>();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_ATTENDANTS;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);//selectQuery,selectedArguments

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                list.add(cursor.getString(2));//adding 2nd column data
            } while (cursor.moveToNext());
        }
        // closing connection
        cursor.close();

        // returning lables
        return list;
    }

然后设置微调器:

private void loadSpinnerData() {
        List<String> labels = ah.getAllAttendantNames();

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, labels);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
        int spinnerPosition = dataAdapter.getPosition(s_attname);
        Log.d("spinner_set_selection", s_attname + spinnerPosition);
        spinner.setSelection(spinnerPosition);
    }

当然onCreate()

spinner = (Spinner) findViewById(R.id.spinner);
             // Loading spinner data from database
        loadSpinnerData();