2015-08-15 60 views
0

我是Android新手。 我只写了一个应用程序,它从EditText中提供一个字符串并将其保存在SQLite中。 知道我想在ListView中看到数据库的内容。 但我不知道你怎么能帮我写这个工作的方法。如何在android studio的列表中显示sqlite实体

这是我的DBManager类

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); 
    } 


} 
+0

使用[SimpleCursorAdapter(http://developer.android.com/reference/android/widget/SimpleCursorAdapter。 html) – PPartisan

回答

0

如果你正在写的东西像一个做笔记的应用程序和使用SQLite来存储它们,我已在我的关于此主题的博客上写了一些相当长的文章tutorialsCode | App),可能对您有所帮助。这是它的要点:

我会建议你在getDetails()方法内用query()替换rawQuery。这是因为SQL语法是不可宽恕的,并且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,否则它们可能会导致应用程序冻结或即使崩溃,如果你的表是特别大。

在对于该数据分配到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); 

在总体布局定义您的排layout,并确保有一个ListView

布局 - 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); 
+0

感谢您的关注和您的有益答案 – SARA

1

应该是作为你的表名“从测试选择文本”是考验。一旦你得到游标,遍历它来读取数据。 How to retrieve data from cursor class在ListView中显示数据时使用SimpleCursorAdapter - http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html,如注释中所示。要改进代码,而不是对原始查询进行编码和使用,请尝试使用投影 - http://developer.android.com/training/basics/data-storage/databases.html

+0

感谢您的帮助 – SARA

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; 
    } 

你可以使用列表(在我的情况下,将产品):

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; 
    } 
+0

对于第二个选项,您必须使用自定义适配器! – EnderNicky

+0

感谢您的帮助 – SARA

相关问题