0

I'm new in android. I just write an app that give a string from EditText and save it in SQLite. know I want see the contents of Database in a ListView. But I don't Know how could you please help me to write a method for this work.

That's my DBManager class

package com.sara.app.savetextapp;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBManager extends SQLiteOpenHelper {

    public static final String DB_NAME = "Data";
    public static final String TABLE_NAME = "test";
    public static final String COLUMN_NAME= "text";
    private static final String COLUMN_TEXT_ID = "_id";

    public DBManager(Context context) {
        super(context, DB_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
            + COLUMN_TEXT_ID + " integer primary key autoincrement, "
            + COLUMN_NAME + " varchar(100))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL("DROP TABLE IF EXISTS");
        onCreate(db);
    }

    public Cursor getDetails()
    {
        SQLiteDatabase db = getReadableDatabase();
        return db.rawQuery("select text from Data", null);
    }


}
4

3 回答 3

1

应该是“从测试中选择文本”,因为您的表名是测试。获得光标后,遍历它以读取数据。如何从光标类中检索数据,在 ListView 中使用 SimpleCursorAdapter - http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html,如评论中所建议的那样。为了改进代码,而不是编码和使用原始查询尝试使用投影 - http://developer.android.com/training/basics/data-storage/databases.html

于 2015-08-15T07:52:11.240 回答
0

获取您要显示的列的字符串(将产品替换为您的列)。

public List<String> getAllContacts() {
        MyDBHandler helper = new MyDBHandler(getActivity().getApplicationContext(),null,null,1);
        List<String> contactList = new ArrayList<String>();
        // Select All Query
        String selectQuery = "SELECT  * FROM products WHERE 1";
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {

            do {

                Product contact = new Product();
                String c;
                c=(cursor.getString(1));

                // Adding contact name to list

                contactList.add(c);
            } while (cursor.moveToNext());
        }// return contact list
        cursor.close();
        db.close();
        return contactList;
    }

或者,您可以使用 List 代替 List(在我的情况下,它将是 Product):

public List<Product> getAllContacts() {
        MyDBHandler helper = new MyDBHandler(getActivity().getApplicationContext(),null,null,1);
        List<Product> contactList = new ArrayList<Product>();
        // Select All Query
        String selectQuery = "SELECT  * FROM products WHERE 1";
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {

            do {

                Product contact = new Product();
                String c;

                contact = new Product((cursor.getString(1)),cursor.getString(2) and so on)

                //From here you just fill your product

                contactList.add(contact);
            } while (cursor.moveToNext());
        }// return contact list
        cursor.close();
        db.close();
        return contactList;
    }
于 2015-08-15T08:06:38.667 回答
0

如果您正在编写类似记笔记的应用程序并使用 SQLite 来存储它们,我已经在我的博客上写了一些相当长的教程代码|应用程序),这些教程可能会对您有所帮助。这是它的要点:

我建议您将方法rawQuery内部替换为. 这是因为 SQL 语法是无情的,并且该方法会为您进行微调。getDetails()query()query()

public Cursor getDetails() {
    return db.getReadableDatabase().query(TABLE_NAME, null, null, null, null, null, null); 
    //This will return all data in your table.
}

仅供参考,最好在 UI 线程之外进行查询,例如使用 AsyncTask,否则如果您的表特别大,它们可能会导致应用程序冻结甚至崩溃。

关于将此数据分配给 a ListView,最直接的方法是使用SimpleCursorAdapter

//Where "data" is the Cursor return by getDetails()
ListAdapter listAdapter = new SimpleCursorAdapter(this,
    R.layout.view_notes_row,
    data,
    new String[] {DBManager.COLUMN_NAME},
    new int[] {R.id.text_view},
    0);

为您的行定义 a layout,并确保ListView您的整体布局中有 a:

布局 - list_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_view" />
</FrameLayout>

行 - view_notes_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/text_view" />
</LinearLayout>

在你的里面Activitiy

setContentView(R.layout.list_layout);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(listAdapter);
于 2015-08-15T08:25:32.787 回答