2011-09-01 65 views
0

我知道這個問題經常被問到,但我仍然沒有得到它的工作原理。如何輸入數據庫條目到我的ListView

這裏是我的Activity.class

package de.retowaelchli.filterit; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 

import de.retowaelchli.filterit.database.ADFilterDBAdapter; 

public class ADeleteActivity extends Activity { 

    //Variablen deklarieren 
    private ADFilterDBAdapter mDbHelper; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.autodelete); 
     setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListContent)); 
     mDbHelper.open(); 

    } 

    private static String[] mListContent={mDbHelper.getAllADFilter()}; 

    /** Verweise auf die anderen Seiten **/ 
    public void onClickADConfig(View view){ 
     final Intent i = new Intent(this, ADFilterConfigActivity.class); 
     startActivity(i); } 
} 

這裏是我的Layout.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="match_parent" 
    android:weightSum="1"> 

    <ListView android:id="@+id/AutoDeleteFilterListe" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.97"> 
    </ListView> 

     <TableLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/ADOverviewMainsite" 
      android:layout_weight="0.03"> 

       <TableRow 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"   
        android:id="@+id/ADFilterOverview" 
        android:onClick="onClickADConfig" 
        android:layout_weight="0.03"> 
         <TextView 

          android:text="@string/newadfilter" 
          style="@style/NormalFont" /> 
       </TableRow> 
    </TableLayout> 

</LinearLayout> 

而且我的繼承人將對DBAdapter:

package de.retowaelchli.filterit.database; 

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; 

public class ADFilterDBAdapter { 

     public static final String ROW_ID = "_id"; 
     public static final String NAME = "name"; 
     public static final String KEYWORD = "keyword"; 
     public static final String CACHE = "cache"; 

     private static final String DATABASE_TABLE = "adfilter"; 

     private DatabaseHelper mDbHelper; 
     private SQLiteDatabase mDb; 

     private final Context mCtx; 

     private static class DatabaseHelper extends SQLiteOpenHelper { 

      DatabaseHelper(Context context) { 
       super(context, DBAdapter.DATABASE_NAME, null, DBAdapter.DATABASE_VERSION); 
      } 

      @Override 
      public void onCreate(SQLiteDatabase db) { 
      } 

      @Override 
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      } 
     } 

     /** 
     * Constructor - takes the context to allow the database to be 
     * opened/created 
     * 
     * @param ctx 
     *   the Context within which to work 
     */ 
     public ADFilterDBAdapter(Context ctx) { 
      this.mCtx = ctx; 
     } 

     /** 
     * Open the cars database. If it cannot be opened, try to create a new 
     * instance of the database. If it cannot be created, throw an exception to 
     * signal the failure 
     * 
     * @return this (self reference, allowing this to be chained in an 
     *   initialization call) 
     * @throws SQLException 
     *    if the database could be neither opened or created 
     */ 
     public ADFilterDBAdapter open() throws SQLException { 
      this.mDbHelper = new DatabaseHelper(this.mCtx); 
      this.mDb = this.mDbHelper.getWritableDatabase(); 
      return this; 
     } 

     /** 
     * close return type: void 
     */ 
     public void close() { 
      this.mDbHelper.close(); 
     } 

     /** 
     * Create a new car. If the car is successfully created return the new 
     * rowId for that car, otherwise return a -1 to indicate failure. 
     * 
     * @param name 
     * @param model 
     * @param year 
     * @return rowId or -1 if failed 
     */ 
     public long createADFilter(String name, String keyword, String cache){ 
      ContentValues initialValues = new ContentValues(); 
      initialValues.put(NAME, name); 
      initialValues.put(KEYWORD, keyword); 
      initialValues.put(CACHE, cache); 
      return this.mDb.insert(DATABASE_TABLE, null, initialValues); 
     } 

     /** 
     * Delete the car with the given rowId 
     * 
     * @param rowId 
     * @return true if deleted, false otherwise 
     */ 
     public boolean deleteADFilter(long rowId) { 

      return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0; //$NON-NLS-1$ 
     } 

     /** 
     * Return a Cursor over the list of all cars in the database 
     * 
     * @return Cursor over all cars 
     */ 
     public Cursor getAllADFilter() { 

      return this.mDb.query(DATABASE_TABLE, new String[] { ROW_ID, 
        NAME, KEYWORD, CACHE }, null, null, null, null, null); 
     } 

     /** 
     * Return a Cursor positioned at the car that matches the given rowId 
     * @param rowId 
     * @return Cursor positioned to matching car, if found 
     * @throws SQLException if car could not be found/retrieved 
     */ 
     public Cursor getADFilter(long rowId) throws SQLException { 

      Cursor mCursor = 

      this.mDb.query(true, DATABASE_TABLE, new String[] { ROW_ID, NAME, 
        KEYWORD, CACHE}, ROW_ID + "=" + rowId, null, null, null, null, null); 
      if (mCursor != null) { 
       mCursor.moveToFirst(); 
      } 
      return mCursor; 
     } 

     /** 
     * Update the car. 
     * 
     * @param rowId 
     * @param name 
     * @param keyword 
     * @param cache 
     * @return true if the note was successfully updated, false otherwise 
     */ 
     public boolean updateADFilter(long rowId, String name, String keyword, 
       String cache){ 
      ContentValues args = new ContentValues(); 
      args.put(NAME, name); 
      args.put(KEYWORD, keyword); 
      args.put(CACHE, cache); 

      return this.mDb.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) >0; 
     } 

    } 

我想我的代碼寫道,但我得到了錯誤,而且它不起作用。我應該如何改變它,或者我該怎麼做?

+0

添加這些錯誤您的問題 – Jayp

回答

1

你是返回遊標值並將其保存到字符串數組...

private static String[] mListContent={mDbHelper.getAllADFilter()}; 

嘗試在您的列表添加適配器光標值如下:

ListAdapter adapter = new SimpleCursorAdapter(this, // Context. 
       android.R.layout.two_line_list_item, // Specify the row template 
                 // to use (here, two 
                 // columns bound to the 
                 // two retrieved cursor 
                 // rows). 
       mCursor, // Pass in the cursor to bind to. 
       // Array of cursor columns to bind to. 
       new String[] { ContactsContract.Contacts._ID, 
         ContactsContract.Contacts.DISPLAY_NAME }, 
       // Parallel array of which template objects to bind to those 
       // columns. 
       new int[] { android.R.id.text1, android.R.id.text2 }); 

     // Bind to our new adapter. 
     setListAdapter(adapter); 

你也可以參考這個文檔:

http://www.vogella.de/articles/AndroidListView/article.html

+0

thx幫了我很多! – safari

+0

然後你應該接受答案,因爲它會浮動作爲一個未答覆的問題.. –

+0

是的,我知道但是,5分鐘左右沒有結束;) – safari

相關問題