【问题标题】:Android database sql listAndroid数据库sql列表
【发布时间】:2013-01-08 14:51:09
【问题描述】:

您好我正在尝试使用 sql 数据库创建一个 android 应用程序,我想在列表视图中列出我的结果,但似乎无法让它工作,请帮助我到目前为止并收到错误

java.lang.IllegalArgumentException:列“_id”不存在

SimpleCursorAdapter adp;

    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         db.open();


         // from array: keeps the names of the Cursor columns.
         String[] from = {"CURSOR_FIELD1","CURSOR_FIELD2"};
         // to array: keeps the ids of the fields in your list item layout
         int[] to = {R.id.itemLayoutfield1, R.id.itemLayoutfield2};
         // sets the basic layout of the ListActivity (the layout must contain a ListView element with the id called 'list').
         setContentView(R.layout.view_data_layout);

         // gets de cursor with the rows        
         c = db.getAllCourses(); 

         adp = new SimpleCursorAdapter(this, R.layout.row, c, from, to);             
         setListAdapter(adp);

        ListView listView = getListView();
        listView.setTextFilterEnabled(true);


        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),
                ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); 

                displayAllCourses(c);

                db.close();


            }               

        });

还有我的 dbadapter

公共游标 getAllCourses() {

    return db.query(DATABASE_TABLE, new String[] { ID, Time_Amount,
            Seed_Acre, MM_Settings, Acre_amount, Speed_Type, Acre_String, Seed_Type,  Hill_Name }, null, null,
            null, null, null, null);
}

public Cursor getCourse(long rowId) throws SQLException {

    Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] { ID,
            Time_Amount, Seed_Acre, MM_Settings, Acre_amount, Speed_Type, Acre_String, Seed_Type, Hill_Name },
            ID + "=" + rowId, null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;


}

【问题讨论】:

  • 错误信息是什么?请发布日志文件
  • 对不起,压力很大,运行时没有错误,它只是显示数据库中有多少条目。我需要它在列表中列出所有 Hill_Name 条目

标签: android arrays sqlite arraylist android-listview


【解决方案1】:

您的 'hill' 字符串数组只包含一个项目(甚至不是表格的第一行)。

要显示来自 Cursor 的查询结果,最好使用 SimpleCursorAdapter 对象,而不是 ArrayAdapter,此对象可帮助您将查询结果(光标)映射到视图(用户界面)的字段)。应该是这样的:

public class YourListActivity extends ListActiviy{

    private SimpleCursorAdapter adp;
    private Cursor c;

    public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);

             ...
             // from array: keeps the names of the Cursor columns.
             String[] from = {"CURSOR_FIELD1","CURSOR_FIELD2", ...};
             // to array: keeps the ids of the fields in your list item layout
             int[] to = {R.id.itemLayoutfield1, R.id.itemLayoutfield2, ...};
             // sets the basic layout of the ListActivity (the layout must contain a ListView element with the id called 'list').
             setContentView(R.layout.listLayout);

             // gets de cursor with the rows        
             c = db.getAllCourses(); 

             adp = new SimpleCursorAdapter(this, R.layout.itemLayout, c, from, to);             
             setListAdapter(adp);

             ...
     }
}

重要提示:请记住,CursorAdapter 类及其子类(即 SimpleCursorAdapter)需要将表的主键命名为 _id。

listLayout.xml 应该类似于(注意它有一个 ID 为 'list' 的 ListView):

<?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="wrap_content">
     <ListView android:id="@android:id/list"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"/>
 </LinearLayout>

itemlayout.xml 可能是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <TextView
                android:id="@+id/itemLayoutfield1"
                android:text="TextView" />
        </LinearLayout>
        <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent" >
            <TextView
                android:id="@+id/itemLayoutfield2"
                android:text="TextView" />          
        </LinearLayout>
        <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent" >
            <TextView
                android:id="@+id/itemLayoutfield3"
                android:text="TextView" />          
        </LinearLayout>
</LinearLayout>

【讨论】:

  • 感谢您的回复 :) 我是 Android 新手。所以 from 数组是我的 ID、Time_Amount、Seed_Acre、MM_Settings 等,然后我的数组 R.id.itemLayoutfield1 是从游标中获取数据库条目后放置的位置?
  • 还有什么是adp部分?
  • 我在试,adp下变红了
  • 我添加了代码,但现在得到 01-08 17:11:54.703: E/AndroidRuntime(15275): FATAL EXCEPTION: main 01-08 17:11:54.703: E/AndroidRuntime(15275 ):java.lang.RuntimeException:无法启动活动ComponentInfo {co.uk.t.app/co.uk.t.app.ViewDataActivity}:java.lang.IllegalArgumentException:列'_id'不存在
  • 全部整理完毕 :) 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-13
  • 2016-04-03
相关资源
最近更新 更多