Android的SimpleCursorAdapter结果不AlertDialog显示 [英] Android SimpleCursorAdapter results not displaying in AlertDialog

查看:150
本文介绍了Android的SimpleCursorAdapter结果不AlertDialog显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找替代旋转器,因为第一个项目始终处于选中状态(这使我的问题),我发现了一些例子使用AlertDialog一个列表,而不是。

我有两个问题:

  1. 清单显示和格式化好的,但目前还没有价值在里面。我知道该查询返回,并将光标/适配器的数据吧。

  2. 这可能是#1的症状 - 但是当我选择一个空行,光标光标2 =(光标)((适配器视图)对话框).getItemAtPosition(所);声明会导致系统崩溃(这是一个ClassCastException)。

我有类似的code previously该适配器设置为一个微调对象,而数据显示正常。

我不认为适配器是越来越设置正确,我一直未能拿出一个解决方案迄今。

有什么想法?

谢谢!

  btnDenomination.setOnClickListener(新View.OnClickListener()
    {
        公共无效的onClick(查看W)
        {
            光标光标= coinDB.myDataBase.rawQuery(选择_id,从面额denomination_desc,NULL); //必须选择_id领域,但没有必要使用它
            startManagingCursor(光标); //需要为了使用光标

            的String []从=新的String [] {denomination_desc}; //这是我想要的微调显示数据库列名
            INT []到=新INT [] {R.id.tvDBViewRow}; //这是在微调TextView的对象

            cursor.moveToFirst();

            SimpleCursorAdapter adapterDenomination =新SimpleCursorAdapter(CoinsScreen.this,
                    android.R.layout.simple_spinner_item,光标,从,到);

            adapterDenomination.setDropDownViewResource(R.layout.db_view_row);

             新AlertDialog.Builder(CoinsScreen.this)
              .setTitle(选择面额)
              .setAdapter(adapterDenomination,新DialogInterface.OnClickListener()
              {

                公共无效的onClick(DialogInterface对话,诠释其)
                {

                    光标光标2 =(光标)((适配器视图<>)对话框).getItemAtPosition(所);
                    strDenomination_id = cursor2.getString(0); //获取塔1在一个零的索引,所述第一列是PKID。这可能
                    //避免使用选择AS语句。

                    Log.d(项目中选择,strDenomination_id);

                    TextView的txtDenomination =(TextView中)findViewById(R.id.textDenomination);
                    txtDenomination.setText(cursor2.getString(1));


                  dialog.dismiss();
                }
              })创建()显示()。
        }
    });

< XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout机器人:ID =@ + ID / LinearLayout01
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

< TextView的Andr​​oid版本:文本=
机器人:ID =@ + ID / tvDBViewRow
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=#FF0000/>

< / LinearLayout中>
 

解决方案

第二个问题,所以新的答案:)

我觉得你应该重​​用,而不是试图得到一个新的初始光标...你可以尝试做:

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

在onclick()?

I was looking for an alternative to a spinner, since the first item is always selected (which causes me issues), and I found some examples for using an AlertDialog with a list instead.

I am having two problems:

  1. The list is displaying and is formatted ok, but there are no values in it. I know the query is returning, and the cursor/adapter has the data in it.

  2. This may be a symptom of #1 - but when I select a blank row, the Cursor cursor2 = (Cursor) ((AdapterView) dialog).getItemAtPosition(which); statement causes a crash (it's a ClassCastException).

I had similar code previously which set the adapter to a spinner object, and the data was displaying fine.

I don't think the adapter is getting set correctly, and I have been unable to come up with a solution thus far.

Any thoughts?

Thanks!

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> 

解决方案

So new answer for the second issue :)

I think you should reuse the initial cursor instead of trying to get a new one... Can you try to do :

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

in the onClick() ?

这篇关于Android的SimpleCursorAdapter结果不AlertDialog显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆