2010-06-22 19 views
5
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(); 
    } 

} 

Verilerimi veritabanımdan ListView ürününe koyamıyorum.Veritabanındaki veriyi ListView'e depolayın.

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


} 

göstermek mümkün değildi:

İşte benim veritabanı şifresi. Umarım bana yardımcı olabilirsiniz!

+0

Aynı soruyu şu şekilde sordunuz: http://stackoverflow.com/questions/3090680/using-a-listadapter-to-show-database-data – Janusz

+0

I ' Üzgünüm, ben cümlecilikte iyi değilim! – UserA

+0

Bize "res/layout/view_list.xml" dosyanızı gösterebilir misiniz, lütfen? – Cristian

cevap

-1

Listeyi, aldığınız verilerle doldurmuyorsunuz. Ve, bu durumlarda SimpleCursorAdapter kullanmalıdır: Ben senin tablo tahmin ediyorum Bu durumda

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

date adlı bir sütun vardır ve başka title ismini verdi. Daha fazla referans için bu örneğe bir göz atın: SimpleCursorAdapters and ListViews

+0

Çok teşekkür ederim. Yapmaya çalışacağım! :> Ben gerçekten android bir noobie duyuyorum! – UserA

İlgili konular