0

我一直在寻找一个微调的替代品,因为第一项总是被选中(这会导致我的问题),并且我找到了一些使用AlertDialog和列表的例子。Android SimpleCursorAdapter结果无法显示在AlertDialog

我有两个问题:

  1. 清单显示和格式化好的,但也有它没有价值。我知道查询正在返回,并且游标/适配器中包含数据。这个可能是#1的一个症状 - 但是当我选择一个空行时,游标cursor2 =(Cursor)((AdapterView)对话框).getItemAtPosition(which);语句导致崩溃(这是一个ClassCastException)。

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

我不认为适配器设置正确,我一直未能拿出解决方案到目前为止。

有什么想法?

谢谢!

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> 

回答

0

您确定R.id.tvDBViewRow是布局android.R.layout.simple_spinner_item中的TextView的ID吗?从this,TextView的id应该是android.R.id.text1。对于第二个问题:)

+0

好吧,如果我改变:R.id.tvDBViewRow到android.R.id.text1,我看到框中的选择(仍然有第二个问题)。我在单独的XML中使用TextView时所做的是为了对行的外观和感觉有一些控制,当我这样做时会失去这种感觉。它与Spinner一起工作,而不是在对话框中。 –

0

所以新的答案,我认为你应该重用,而不是试图获得一个新的光标初始...你可以尝试做:

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

在onClick()?

+0

天才!唯一的区别是我必须使用原始的光标,而不是光标适配器 - 它工作的很好,应该是相当明显的。现在,开始创建我的下一个问题。 –