2011-04-19 70 views
2

嗨,我在我的數據庫中有三個表。我想能夠在列表視圖中顯示數據庫中的名稱和姓氏。如何在列表視圖中顯示數據庫中的數據?

  1. 首先我打開數據庫並添加一個客戶的名字,姓氏和號碼。然後關閉數據庫。
  2. 從這裏,我感到困惑,因爲我怎麼可以在一個行顯示姓名,但在ListView留下的數量。

這裏是我的數據庫類DBAdapter和clientsActivity類,我希望能夠顯示我添加的客戶端。

我知道它有點簡單,但我做了。沒有線索,並在最終遵循一些教程後,我顯示的數據,這是不是我想要的方式。

好心幫....

我的數據庫類。(將對DBAdapter)

package com.android.ideos; 
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; 

/** 
* @author G Sam 

* 
*/ 



public class DBAdapter { 
    //defining columns(fields) in all 3 tables 
    public static final String KEY_CLIENTID = "_id"; 
    public static final String KEY_TRANSID = "transId"; 
    public static final String KEY_NAME = "name"; 
    public static final String KEY_SURNAME = "surname"; 
    public static final String KEY_MOBILE= "mobile"; 
    public static final String KEY_TYPE = "Type"; 
    public static final String KEY_DATETIME = "DateTime"; 
    public static final String KEY_AMOUNT = "Amount"; 
    public static final String KEY_BALANCE = "Balance"; 

    private static final String TAG = "DBAdapter"; 

    private static final String DATABASE_NAME = "radicalfinance"; 
    private static final String DATABASE_CLIENTSTable = "clientstable"; 
    private static final String DATABASE_TRANSACTIONS = "TransactionsTable"; 
    private static final String DATABASE_CLIENTSBALANCE = "ClientsBalanceTable"; 
    private static final int DATABASE_VERSION = 1; 
//Creating the database radicalfinance 
    //CREATING CLIENTSTable 
    private static final String DATABASE_CREATE_CLIENTSTABLE = 
     "create table clientstable (_id integer primary key autoincrement, " 
     + "name text not null, surname text not null, " 
     + "mobile integer not null);"; 
//CREATING TransactionsTable  
    private static final String DATABASE_CREATE_TRANSACTIONSTABLE = 
     "create table TransactionsTable (_id integer primary key autoincrement, " 
     + "transId integer," 
     + "Type boolean not null, DateTime text not null, " 
     + "Amount long not null);"; 
//CREATING ClientsBalanceTable 
    private static final String DATABASE_CREATE_CLIENTSBALANCETABLE = 
     "create table ClientsBalanceTable (_id integer primary key autoincrement, " 
     + "Balance long not null); "; 


    private final Context context; 
    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 


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

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

     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      db.execSQL(DATABASE_CREATE_CLIENTSTABLE); 
      db.execSQL(DATABASE_CREATE_TRANSACTIONSTABLE); 
      db.execSQL(DATABASE_CREATE_CLIENTSBALANCETABLE); 
     } 

     @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 clientstable"); 
      onCreate(db); 
     } 
    }  

    //methods for opening and closing the database, as well as the methods for adding/editing/deleting rows in the table. 



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

    } 

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

    //---insert a client and his info into the database--- 

    public long insertClient(String name, String surname, String mobile) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_NAME, name); 
     initialValues.put(KEY_SURNAME, surname); 
     initialValues.put(KEY_MOBILE, mobile); 
     return db.insert(DATABASE_CLIENTSTable, null, initialValues); 
    } 
    public long insertClientTransaction(String transId, String Type, String DateTime, String Amount) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_TRANSID, transId); 
     initialValues.put(KEY_TYPE, Type); 
     initialValues.put(KEY_DATETIME, DateTime); 
     initialValues.put(KEY_AMOUNT, Amount); 
     return db.insert(DATABASE_TRANSACTIONS, null, initialValues); 
    } 
    public long insertClientBalance(String Balance) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_BALANCE, Balance); 
     return db.insert(DATABASE_CLIENTSBALANCE, null, initialValues); 
    } 

    //---deletes a particular client--- 
    public boolean deleteClient(long clientId) 
    { 
     return db.delete(DATABASE_CLIENTSTable,KEY_CLIENTID + "=" + clientId, null) > 0; 
    } 

    //---retrieves all the clients--- 
    public Cursor getAllClients() 
    { 
     return db.query(DATABASE_CLIENTSTable, new String[] { 
       KEY_CLIENTID, 
       KEY_NAME, 
       KEY_SURNAME, 
       KEY_MOBILE}, 
       null, 
       null, 
       null, 
       null, 
       null); 

    } 
    //querying TransactionsTable 
    public Cursor getAllTransactionsRecords() { 

     return db.query(DATABASE_TRANSACTIONS, new String[] { 
       KEY_TRANSID, 
       KEY_TYPE, 
       KEY_DATETIME, 
       KEY_AMOUNT}, 
       null, 
       null, 
       null, 
       null, 
       null); 
    } 
    //made comments will be taken care of i.e the clientsbalanceRecords 
     //querying clientsbalancetable 
    //public Cursor getAllBalanceRecords() { 

     //return db.query(DATABASE_CLIENTSBALANCE, new String[] { 
       //KEY_BALANCE}, 
       //null, 
       //null); 
    //} 

    //---retrieves a particular client--- 
    public Cursor getClient(long clientId) throws SQLException 
    { 
     Cursor mCursor = 
       db.query(true, DATABASE_CLIENTSTable, new String[] { 
         KEY_CLIENTID, 
         KEY_NAME, 
         KEY_SURNAME, 
         KEY_MOBILE 
         }, 
         KEY_CLIENTID + "=" + clientId, 
         null, 
         null, 
         null, 
         null, 
         null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 
    } 

    //---updates a client's details--- 
    public boolean updateClient(long clientId, String name, 
    String surname, String mobile) 
    { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_NAME, name); 
     args.put(KEY_SURNAME, surname); 
     args.put(KEY_MOBILE, mobile); 
     return db.update(DATABASE_CLIENTSTable, args, 
         KEY_CLIENTID + "=" + clientId, null) > 0; 
    } 
    public boolean updateTransactions(long clientId, long transId, String Type, 
      String DateTime, long Amount) 
      { 
       ContentValues args = new ContentValues(); 
       args.put(KEY_TRANSID, transId); 
       args.put(KEY_TYPE, Type); 
       args.put(KEY_DATETIME, DateTime); 
       args.put (KEY_AMOUNT, Amount); 
       return db.update(DATABASE_TRANSACTIONS, args, 
           KEY_CLIENTID + "=" + clientId, null) > 0; 
      } 

    public SQLiteDatabase getWritableDatabase() { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

那麼這裏就是我的ClientsActivity類

package com.android.ideos; 
import android.app.ListActivity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.CursorAdapter; 
import android.widget.ListAdapter; 
import android.widget.Toast; 
public class ClientsActivity extends ListActivity { 


    protected DBAdapter db; 
    protected CursorAdapter dataSource; 
    protected ListAdapter adapter; 
    //private static final String columns[] = { "name", "surname"}; 
    //private static final String TAGG = "ClientsActivity"; 



    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     db = new DBAdapter(this); 
     /*DataBaseHelper helper = new DataBaseHelper(this); 
     db = helper.getWritableDatabase(); 
     Cursor data = db.query("clientstable", columns, 
       null, null, null, null, null); 
     dataSource = new SimpleCursorAdapter(this, 
       R.layout.clients, data, columns, 
       new int[] { R.id.name, R.id.surname }); 

     setListAdapter(dataSource);*/ 
     db.open(); 
     Long rowID = db.insertClient("Adera", "Dan", "0727858191"); 
     db.close(); 

     displayclients(rowID); 

    } 

    private void displayclients(long clientId) 
    **{ 
     // TODO Auto-generated method stub 
     db.open(); 
     Cursor results = db.getClient(clientId); 
     if (results.moveToFirst()) 
     { 
      Toast.makeText(this, "Name: "+results.getString(1)+" "+results.getString(2), Toast.LENGTH_LONG).show(); 
     }** 





} 

//calls the content menu layout 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater myMenuInflater = getMenuInflater(); 
    myMenuInflater.inflate(R.menu.menu, menu); 
    return true; 
} 

// the layout of the menu as seen in the menu.xml file 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // TODO Auto-generated method stub 
    switch(item.getItemId()) 
    { 
    // the menu button New Client and the functionality code will be implemented here. 
    case(R.id.menu_new_client): 
    Toast.makeText(this, "New client", Toast.LENGTH_LONG).show(); 


    break; 

    // the menu button: Edit, and the functionality code will be implemented here. 
    case(R.id.menu_edit): 
    Toast.makeText(this, "Edit", Toast.LENGTH_LONG).show(); 
    break; 

    // the menu button: DElete, and the functionality code will be implemented here. 
    case(R.id.menu_delete): 
    Toast.makeText(this, "Delete", Toast.LENGTH_LONG).show(); 


    break; 

    // the menu button: Search, and the functionality code will be implemented here. 
    case(R.id.menu_search): 
     Toast.makeText(this, "Search", Toast.LENGTH_LONG).show(); 
     break; 
    } 
    return true; 
} 
} 

回答

1

我看不到任何地方,你是通過ListAdapter(CursorAdapter)將數據庫遊標分配給ListView。

我認爲你需要做的谷歌記事本教程做的所有3,但即使只是Notepad1教程介紹了有關SimpleCursorAdapter鏈接到ListView。看看這個tutorial,並特別注意的fillData()方法

private void fillData() { 
    // Get all of the notes from the database and create the item list 
    Cursor c = mDbHelper.fetchAllNotes(); 
    startManagingCursor(c); 

    String[] from = new String[] { NotesDbAdapter.KEY_TITLE }; 
    int[] to = new int[] { R.id.text1 }; 

    // Now create an array adapter and set it to display using our row 
    SimpleCursorAdapter notes = 
     new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to); 
    setListAdapter(notes); 
} 

我希望幫助

+0

@ wired00,應該這個數據庫光標是在私人無效displayclients(長的clientId)正如我在這裏做??私人無效displayclients(長的clientId) ** { // TODO自動生成方法存根 db.open(); 遊標結果= db.getClient(clientId);如果(results.moveToFirst()) Toast.makeText(this,「Name:」+ results.getString(1)+「」+ results.getString(2),Toast.LENGTH_LONG).show();如果(results.moveToFirst()) } ** – Gsam 2011-04-19 14:28:45

+0

只要確保看教程。是的,你可以想象你的displayClients()類似於谷歌記事本教程獲取fillData()方法中的數據。我修改了我的回答,代碼 – wired00 2011-04-19 14:32:48

+0

感謝WiredOO,我希望這有幫助。也將看看谷歌記事本教程 – Gsam 2011-04-19 14:37:03

相關問題