java - 旋转器无法正常工作

标签 java android android-widget android-alertdialog android-spinner

我在 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();  

请帮我找出问题所在

最佳答案

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

parent.getItemAtPosition(position);

您没有查询任何内容。它已经由 Activity 管理

关于java - 旋转器无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12996682/

相关文章:

java - 小程序中的多线程

java - 使用不必要的条件更快地编码?

Android:自动旋转在 setRequestedOrientation 后不起作用

java - 遍历 GroupView 的所有子项?

android - 下载源代码时显示旋转轮而不是图像

android - 多个 Activity 的 ViewPager

java - 正常实例化和实例化之间的区别,包括方法引用

java - 此处不允许使用 Void-type 错误

Android: java.lang.OutOfMemoryError: 线程创建失败

android - 如何在 AppWidgetProvider 类中使用 OnClickListener 方法