Android SimpleCursorAdapter结果未显示在AlertDialog中

时间:2011-12-08 16:06:52

标签: android cursor alertdialog simplecursoradapter android-alertdialog

我一直在寻找一个微调器的替代品,因为第一个项目总是被选中(这会导致我的问题),我找到了一些使用带有列表的AlertDialog的例子。

我遇到两个问题:

  1. 列表正在显示并且格式正常,但其中没有值。我知道查询正在返回,并且游标/适配器中包含数据。

  2. 这可能是#1的症状 - 但是当我选择一个空白行时,Cursor cursor2 =(光标)((AdapterView)对话框).getItemAtPosition(which);语句导致崩溃(它是ClassCastException)。

  3. 之前我有类似的代码将适配器设置为微调器对象,数据显示正常。

    我认为适配器设置不正确,到目前为止我无法提出解决方案。

    有什么想法吗?

    谢谢!

    btnDenomination.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View w) 
            { 
                Cursor cursor = coinDB.myDataBase.rawQuery("select _id, denomination_desc from denomination", null); // must select the _id field, but no need to use it
                startManagingCursor(cursor); // required in order to use the cursor in 
    
                String[] from = new String[] {"denomination_desc" }; // This is the database column name I want to display in the spinner
                int[] to = new int[] { R.id.tvDBViewRow }; // This is the TextView object in the spinner
    
                cursor.moveToFirst();
    
                SimpleCursorAdapter adapterDenomination = new SimpleCursorAdapter(CoinsScreen.this, 
                        android.R.layout.simple_spinner_item, cursor, from, to  ); 
    
                adapterDenomination.setDropDownViewResource(R.layout.db_view_row); 
    
                 new AlertDialog.Builder(CoinsScreen.this) 
                  .setTitle("Select Denomination") 
                  .setAdapter(adapterDenomination, new DialogInterface.OnClickListener() 
                  { 
    
                    public void onClick(DialogInterface dialog, int which) 
                    { 
    
                        Cursor cursor2 = (Cursor) ((AdapterView<?>) dialog).getItemAtPosition(which);
                        strDenomination_id = cursor2.getString(0); // Gets column 1 in a zero based index, the first column is the PKID.  this could
                        // be avoided by using a select AS statement.
    
                        Log.d("Item Selected", strDenomination_id );   
    
                        TextView txtDenomination = (TextView) findViewById(R.id.textDenomination); 
                        txtDenomination.setText(cursor2.getString(1));  
    
    
                      dialog.dismiss(); 
                    } 
                  }).create().show(); 
            } 
        });
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <TextView android:text=""
    android:id="@+id/tvDBViewRow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FF0000" />
    
    </LinearLayout> 
    

2 个答案:

答案 0 :(得分:0)

你确定R.id.tvDBViewRow是布局android.R.layout.simple_spinner_item中TextView的id吗?从this开始,TextView的id应为android.R.id.text1。

答案 1 :(得分:0)

第二期的新答案:)

我认为你应该重复使用初始光标,而不是试图获得一个新光标...你可以尝试这样做:

adapterDenomination.moveToPosition(which);
strDenomination_id = adapterDenomination.getString(0);

在onClick()?