1

我在 Alertdialog 中使用 3 个微调器。当我使用没有 onitemselectedlistener 的微调器时,它会正确填充,而当我使用 onItemSelectedListener 时,它不会填充值,我尝试了stackoverflow Questions的其他一些建议,现在当我单击某个项目时,它第一次填充所有值

我在这里发布了我的警报对话框代码

AlertDialog.Builder myDialog = new AlertDialog.Builder(CameraDemo.this);
    myDialog.setTitle("Save");
    TableRow.LayoutParams rowparams = new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

    LinearLayout layout1 = new LinearLayout(CameraDemo.this);
    layout1.setOrientation(LinearLayout.VERTICAL);
    Spinner spin = new Spinner(CameraDemo.this);
    Cursor cursor = placeData.queueAllDoc();
    startManagingCursor(cursor);
    SimpleCursorAdapter cu = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursor, new String[]{SQLiteoperations.DOC_TYPE}, new int[]{android.R.id.text1});
    cu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(cu);

    spin.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position,
                long id) {
            if(mSpinnerInitializedCount < mSpinnerCount){
                mSpinnerInitializedCount++;
                return;                         
            }
            Cursor incursor1 = (Cursor) parent.getItemAtPosition(position);
            int did = incursor1.getInt(incursor1.getColumnIndex(SQLiteoperations.DOC_ID));
            doc_id = did;
            System.out.println(did);
            incursor1.close();
        }
        public void onNothingSelected(AdapterView<?> arg0) {
        return;
        }
    });

    LayoutParams TxtLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    final EditText saveEdt = new EditText(CameraDemo.this);
    LayoutParams locationEdtLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    saveEdt.setLayoutParams(locationEdtLayoutParams);

    final TextView saveTxt = new TextView(CameraDemo.this);
    saveTxt.setLayoutParams(TxtLayoutParams);
    saveTxt.setText("Name");
    TextView groupTxt = new TextView(CameraDemo.this);
    groupTxt.setLayoutParams(TxtLayoutParams);
    groupTxt.setText("Group");
    TextView clientTxt = new TextView(CameraDemo.this);
    clientTxt.setLayoutParams(TxtLayoutParams);
    clientTxt.setText("Client");
    TextView docTxt = new TextView(CameraDemo.this);
    docTxt.setLayoutParams(TxtLayoutParams);
    docTxt.setText("Document Type");

    TableRow grouprow = new TableRow(CameraDemo.this);
    grouprow.setLayoutParams(rowparams);
    final TableRow clientrow = new TableRow(CameraDemo.this);
    clientrow.setLayoutParams(rowparams);
    final TableRow docrow = new TableRow(CameraDemo.this);
    docrow.setLayoutParams(rowparams);
    final TableRow namerow = new TableRow(CameraDemo.this);
    namerow.setLayoutParams(rowparams);



    Spinner spin2 = new Spinner(CameraDemo.this);
    Cursor cursor2 = placeData.queueAllGroup();
    startManagingCursor(cursor2);
    SimpleCursorAdapter cu2 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursor2, new String[]{SQLiteoperations.GROUP_NAME}, new int[]{android.R.id.text1});
    cu2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin2.setAdapter(cu2);

    spin2.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position,
                long id) {
            if(mSpinnerInitializedCount < mSpinnerCount){
                mSpinnerInitializedCount++;
                return;                         
            }
            Cursor incursor2 = (Cursor) parent.getItemAtPosition(position);
            int gid = incursor2.getInt(incursor2.getColumnIndex(SQLiteoperations.GROUP_ID));
            group_id = gid;
            System.out.println(gid);
            incursor2.close();
        }
        public void onNothingSelected(AdapterView<?> arg0) {
            return;
        }
    });

    Spinner spin3 = new Spinner(CameraDemo.this);
    Cursor cursor3 = placeData.queueAllClient(1);
    startManagingCursor(cursor3);
    SimpleCursorAdapter cu3 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursor3, new String[]{SQLiteoperations.CLIENT_NAME}, new int[]{android.R.id.text1});
    cu3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin3.setAdapter(cu3);

    spin3.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position,
                long id) {
            if(mSpinnerInitializedCount < mSpinnerCount){
                mSpinnerInitializedCount++;
                return;                         
            }
            Cursor incursor3 = (Cursor) parent.getItemAtPosition(position);
            int cid = incursor3.getInt(incursor3.getColumnIndex(SQLiteoperations.CLIENT_ID));
            client_id = cid;
            System.out.println(cid);
            incursor3.close();
        }
        public void onNothingSelected(AdapterView<?> arg0) {
            return;
        }
    });

    grouprow.addView(groupTxt);
    grouprow.addView(spin2);
    clientrow.addView(clientTxt);
    clientrow.addView(spin3);
    docrow.addView(docTxt);
    docrow.addView(spin);
    namerow.addView(saveTxt);
    namerow.addView(saveEdt);

    layout1.addView(grouprow);
    layout1.addView(clientrow);
    layout1.addView(docrow);
    layout1.addView(namerow);

    myDialog.setView(layout1);
    myDialog.show();  

请帮我找到问题

4

1 回答 1

0

我认为您不应该关闭通过调用获得的光标

parent.getItemAtPosition(position);

你没有查询任何东西。它已经由 Activity 管理

于 2012-10-21T11:08:54.947 回答