1

Alertdialog で 3 つのスピナーを使用しています。onitemselectedlistenerなしでスピナーを使用している間は正しく入力され、onItemSelectedListenerを使用しているときは値が入力されません。そして、スタックオーバーフローの質問から他の提案を試してみましたが、アイテムをクリックすると初めて入力されますすべての値が消えます

ここにアラートダイアログコードを投稿しました

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);

あなたは何も照会していません。すでにアクティビティによって管理されています

于 2012-10-21T11:08:54.947 に答える