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


}
SARA
  • 11
  • 1
  • 6
  • Use [SimpleCursorAdapter](http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html) – PPartisan Aug 15 '15 at 07:51

3 Answers3

1

Should be "select text from test" as your table name is test. Once you get cursor , iterate through it to read data .How to retrieve data from cursor class showing data in ListView use SimpleCursorAdapter - http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html as suggested in comment. To improve code, instead of harcoding and using raw query try using projections - http://developer.android.com/training/basics/data-storage/databases.html

Community
  • 1
  • 1
0

Get the string of the column you want to display (replace products with you column).

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

Or instead of List you can use List (in my case it will be 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;
    }
EnderNicky
  • 323
  • 4
  • 14
0

If you are writing something like a Note taking app and using SQLite to store them, I've written some fairly lengthy tutorials (Code | App) on my blog on this topic that may help you. Here's the gist of it:

I would recommend you replace the rawQuery inside your getDetails() method with a query(). This is because SQL syntax is unforgiving, and the query() method takes care of the fine tuning for you.

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

FYI it is better to conduct queries off the UI thread, such as with an AsyncTask, as otherwise they can cause the app to freeze or even if crash if your table is particularly large.

In respect to assigning this data to a ListView, the most straightforward method would be with 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);

Define a layout for your row, and ensure there's a ListView in your overall layout:

Layout - 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>

Row - 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>

And inside your Activitiy:

setContentView(R.layout.list_layout);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(listAdapter);
PPartisan
  • 8,173
  • 4
  • 29
  • 48