【问题标题】:unable to get values in list view by using simple cursor adapter android?无法通过使用简单的光标适配器 android 在列表视图中获取值?
【发布时间】:2012-04-26 20:02:15
【问题描述】:

在我的 android 应用程序中,我想显示从 DB 中检索值的列表。我正在使用简单的光标适配器来执行此操作,但列表视图是空的。这是我的代码。我的列表视图有两行。我如何在应用程序中使用简单的光标适配器。我做错了什么。请提出建议。 提前致谢。

    //list.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
        <ListView android:layout_width="fill_parent" android:id="@android:id/android:list"
        android:layout_height="fill_parent" android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"></ListView>

    </RelativeLayout>


   // textview.xml-->

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" android:weightSum="1">
        <TextView android:id="@+id/column1" android:background="@drawable/mid"
        android:layout_height="30dp" 
        android:layout_width="fill_parent" android:layout_weight="0.03"></TextView>
        <TextView android:layout_height="wrap_content"
        android:text="TextView" android:id="@+id/column2" android:background="@drawable/mid"
        android:layout_weight="0.03" android:layout_width="fill_parent"></TextView>


    </LinearLayout>




    public class AddFood extends ListActivity{

        public void onCreate(Bundle savedInstanceState)
    {

             super.onCreate(savedInstanceState);
             setContentView(R.layout.listxml);

          DataBaseHelper db = null;
        try {
            db = new DataBaseHelper(this);
        } catch (IOException e) {

            e.printStackTrace();
        }
           SQLiteDatabase database=  db.getReadableDatabase();
          Cursor c=database.query("food_list", new String[]{"_id","food_items"}, null, null, null,null,null);
          startManagingCursor(c);

        String[] displayfield=new String[]{"_id","food_items"};
         int[] displayViews = new int[] {R.id.column1,R.id.column2}; 

       SimpleCursorAdapter mAdapter=new SimpleCursorAdapter(getApplicationContext(),R.layout.textview, c,displayfield , displayViews);
          this.setListAdapter(mAdapter);

          c.close();   

    }

    }

【问题讨论】:

  • 我的情况完全一样。看起来我们在同一条船上溺水了。
  • 我什至将列表视图 ID 指定为->“@android:id/list”。但没有任何效果。

标签: android listview simplecursoradapter


【解决方案1】:

对于 list.xml 使用:

<ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></ListView>

在类文件中:

不要使用 c.close();

并使用:

SimpleCursorAdapter mAdapter=new SimpleCursorAdapter(this,R.layout.textview, c,displayfield , displayViews);
          setListAdapter(mAdapter);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    相关资源
    最近更新 更多