Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

android - Getting stored data from database into ListView.

package one.two;


import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.database.Cursor;
import android.inputmethodservice.Keyboard.Row;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class List_View extends ListActivity {

    private ListView lv; 
    private TextView toptext; 
    private TextView bottomtext;

    DBAdapter db = new DBAdapter(this);

    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.list_main);
        DBAdapter db = new DBAdapter(this);
        getData();

        toptext = (TextView) findViewById(R.id.toptext); 
        bottomtext = (TextView) findViewById(R.id.bottomtext); 


    }

    private void getData() {

        db.open();

        List<String> items = new ArrayList<String>();

         Cursor c = db.getAllEntry();
         c.moveToFirst(); 

         toptext.setText("Date: "+c.getString(4));
         bottomtext.setText("Title: "+c.getString(1));



         ArrayAdapter<String> entries = new ArrayAdapter<String>(this, R.layout.view_list, items);
         setListAdapter(entries);

         db.close(); 
    }

}

I'm not able to put the data from my database into my ListView.

Here's my database code:

package one.two;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter 
{   
//values for the login table
public static final String KEY_ROWID = "_id";
public static final String KEY_USER = "user";
public static final String KEY_PASSWORD = "pass";
public static final String KEY_NUMBER ="no";

//Values for the entry table
public static final String KEY_ROWID2 = "_id2";
public static final String KEY_TITLE = "title";
public static final String KEY_ENTRY = "entry";
public static final String KEY_MOOD = "mood";
public static final String KEY_DATE = "date";
public static final String KEY_TIME = "time";

private static final String TAG = "DBAdapter";
//declare Database name, tables names
private static final String DATABASE_NAME = "Database3";
private static final String DATABASE_TABLE = "Login";
private static final String DATABASE_TABLE_2 = "Entry";
private static final int DATABASE_VERSION = 1;

//declares the rules for the database tables
private static final String DATABASE_CREATE =
    "create table login (_id integer primary key autoincrement, "
    + "user text not null, pass text not null," 
    + " no integer not null);";

private static final String DATABASE_CREATE_2 =
    "create table entry (_id2 integer primary key autoincrement, "
    + "title text,entry text, mood text not null, date text not null, "
    + "time text not null);";


private final Context context; 

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    //Create the tables with the rules we set.
    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(DATABASE_CREATE);
        db.execSQL(DATABASE_CREATE_2);
    }

    //OnUpgrade is only for use when u changed the database's version to 2 etc.
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
    int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
                + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS titles");
        onCreate(db);
    }
}    

//---opens the database---
public DBAdapter open() throws SQLException 
{
    db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---    
public void close() 
{
    DBHelper.close();
}

//Method for inserting login details, can be used in other java files when DBAdapter is
//declared in the java file. e.g. DBAdapter db = new DBAdapter(this);
public long insertLogin(String user, String pass, String no) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_USER, user);
    initialValues.put(KEY_PASSWORD, pass);
    initialValues.put(KEY_NUMBER, no);
    return db.insert(DATABASE_TABLE, null, initialValues);
}


public long insertEntry(String title, String entry, String mood, String date, String time) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TITLE, title);
    initialValues.put(KEY_ENTRY, entry);
    initialValues.put(KEY_MOOD, mood);
    initialValues.put(KEY_DATE, date);
    initialValues.put(KEY_TIME, time);
    return db.insert(DATABASE_TABLE_2, null, initialValues);
}


//---deletes a particular title---
public boolean deleteLogin(long rowId) 
{
    return db.delete(DATABASE_TABLE, KEY_ROWID + 
            "=" + rowId, null) > 0;
}

public boolean deleteEntry(long rowId2) 
{
    return db.delete(DATABASE_TABLE_2, KEY_ROWID2 + 
            "=" + rowId2, null) > 0;
}

//method for retrieving all the inputs from database
public Cursor getAllLogin() 
{
    return db.query(DATABASE_TABLE, new String[] {
            KEY_ROWID, 
            KEY_USER,
            KEY_PASSWORD,               
            KEY_NUMBER,},
            null, 
            null, 
            null, 
            null, 
            null, 
            null);
}   



public Cursor getAllEntry() 
{
    return db.query(DATABASE_TABLE_2, new String[] {
            KEY_ROWID2,
            KEY_TITLE,
            KEY_ENTRY,
            KEY_MOOD,
            KEY_DATE,
            KEY_TIME},
            null, 
            null, 
            null, 
            null, 
            null, 
            null);
}


//---retrieves a particular title---
public Cursor getLogin(long rowId) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[] {
                    KEY_ROWID,
                    KEY_USER, 
                    KEY_PASSWORD,
                    KEY_NUMBER}, 
                    KEY_ROWID + "=" + rowId, 
                    null,
                    null, 
                    null, 
                    null, 
                    null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

public Cursor getEntry(long rowId2) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DATABASE_TABLE_2, new String[] {
                    KEY_ROWID2,
                    KEY_TITLE,
                    KEY_ENTRY, 
                    KEY_MOOD,
                    KEY_DATE,
                    KEY_TIME}, 
                    KEY_ROWID2 + "=" + rowId2, 
                    null,
                    null, 
                    null, 
                    null, 
                    null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}


//---updates a title---
public boolean updateLogin(long rowId, String user, 
String pass, String no) 
{
    ContentValues args = new ContentValues();
    args.put(KEY_USER, user);
    args.put(KEY_PASSWORD, pass);
    args.put(KEY_NUMBER, no);
    return db.update(DATABASE_TABLE, args, 
                     KEY_ROWID + "=" + rowId, null) > 0;
}

public boolean updateEntry(long rowId,String title, String entry, 
        String mood, String date, String time) 
        {
            ContentValues args = new ContentValues();
            args.put(KEY_TITLE, title);
            args.put(KEY_ENTRY, entry);
            args.put(KEY_MOOD, mood);
            args.put(KEY_DATE, date);
            args.put(KEY_TIME, time);
            return db.update(DATABASE_TABLE, args, 
                             KEY_ROWID + "=" + rowId, null) > 0;
        }


}

Wasn't able to show. Hope you guys are able to help me!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are not populating the list with the data you retrieved. And, you should use SimpleCursorAdapter in these cases:

private void getData() {

    db.open();

    List<String> items = new ArrayList<String>();

     Cursor c = db.getAllEntry();
     c.moveToFirst(); 

     ListAdapter adapter=new SimpleCursorAdapter(this,
                     R.layout.view_list, c,
                     new String[] {"date", "title"},
                     new int[] {R.id.toptext, R.id.bottomtext});
     setListAdapter(adapter);

     setListAdapter(entries);

     db.close(); 
}

In this case I'm guessing your table has a column named date and another named title. Take a look at this example for further reference: SimpleCursorAdapters and ListViews


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...