2013-12-15 17 views
0

我正在學習Android。我想學習SQLite數據庫,但我遇到了問題。我無法讀取Cursor的輸出並在ListView中顯示它。這裏是我的TestAdapter.java代碼 -如何在ListView中顯示兩個表SQLite查詢的輸出(android)

public class TestAdapter 
{ 
    protected static final String TAG = "DataAdapter"; 

    private final Context mContext; 
    private SQLiteDatabase mDb; 
    private DataBaseHelper mDbHelper; 
    public static final String COL_ID = "_id"; 
    public static String F_name = "F_name"; 
    public static String F_price = "F_price"; 
    public static String R_name = "R_name"; 

    public TestAdapter(Context context) 
    { 
     this.mContext = context; 
     mDbHelper = new DataBaseHelper(mContext); 
    } 

    public TestAdapter createDatabase() throws SQLException 
    { 
     try 
     { 
      mDbHelper.createDataBase(); 
     } 
     catch (IOException mIOException) 
     { 
      Log.e(TAG, mIOException.toString() + " UnableToCreateDatabase"); 
      throw new Error("UnableToCreateDatabase"); 
     } 
     return this; 
    } 

    public TestAdapter open() throws SQLException 
    { 
     try 
     { 
      mDbHelper.openDataBase(); 
      mDbHelper.close(); 
      mDb = mDbHelper.getReadableDatabase(); 
     } 
     catch (SQLException mSQLException) 
     { 
      Log.e(TAG, "open >>"+ mSQLException.toString()); 
      throw mSQLException; 
     } 
     return this; 
    } 

    public void close() 
    { 
     mDbHelper.close(); 
    } 

    public Cursor getTestData() 
    { 
     try 
     { 
      String sql ="select Food.F_name,Food.F_price,R.R_name from Food inner join R on Food.r_id=R.R_id"; 

      Cursor mCur = mDb.rawQuery(sql, null); 
      if (mCur!=null) 
      { 
       F_name =mCur.getString(mCur.getColumnIndex("F_name")); 
       F_price =mCur.getString(mCur.getColumnIndex("F_price")); 
       R_name =mCur.getString(mCur.getColumnIndex("F_name")); 
       mCur.moveToNext(); 
      } 
      return mCur; 
     } 
     catch (SQLException mSQLException) 
     { 
      Log.e(TAG, "getTestData >>"+ mSQLException.toString()); 
      throw mSQLException; 
     } 
    } 



    public boolean SaveEmployee(String name, String email) 
    { 
     try 
     { 
      ContentValues cv = new ContentValues(); 
      cv.put("Name", name); 
      cv.put("Email", email); 

      mDb.insert("Employees", null, cv); 

      Log.d("SaveEmployee", "informationsaved"); 
      return true; 

     } 
     catch(Exception ex) 
     { 
      Log.d("SaveEmployee", ex.toString()); 
      return false; 
     } 
    } 


} 

這裏主要活動

public class CustomDataList extends ListActivity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    TestAdapter dbHelper = new TestAdapter(this); 
    dbHelper.open(); 

    // Get a Cursor for the list items 
    Cursor listCursor = dbHelper.getTestData(); 
    startManagingCursor(listCursor); 

    // set the custom list adapter 
    setListAdapter(new MyListAdapter(this, listCursor)); 
} 

private class MyListAdapter extends ResourceCursorAdapter { 

    public MyListAdapter(Context context, Cursor cursor) { 
     super(context, R.layout.list_item_with_description, cursor); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 

     TextView title = (TextView) view.findViewById(R.id.item_title); 
     title.setText(cursor.getString(
        cursor.getColumnIndex(TestAdapter.F_name))); 

     TextView details = (TextView) view.findViewById(R.id.item_details); 
     StringBuffer detailsText = new StringBuffer(); 

     int price = cursor.getInt(cursor.getColumnIndex(TestAdapter.F_price)); 
     if (price > 0){ 
      detailsText.append("$"+price+".00"); 
     } else { 
      detailsText.append("Price Unavailable"); 
     } 
     String description = cursor.getString(cursor.getColumnIndex(
       TestAdapter.R_name)); 
     if (description != null && description.length() > 0){ 
      detailsText.append(", "+description); 
     } 
     details.setText(detailsText.toString()); 

    } 

} 

}

它不工作..任何一個請幫助。 ..我只是想表明一個列表視圖的查詢數據

回答

0

嘗試的CursorAdapter

class MyListAdapter extends CursorAdapter { 


    public MyListAdapter (Context context, Cursor c) { 
     super(context, c); 
    } 

    public void bindView(View view, Context context, Cursor cursor) { 
     String id = cursor.getString(0); 
     String name = cursor.getString(1); 
     // Get all the values 
     // Use it however you need to 

    } 

    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     // Inflate your view here. 

    } 
} 
+0

我不熟悉的光標適配器.. u能GV我這種類型的例子鏈接:) – user3105254

+0

的http:/ /tausiq.wordpress.com/2012/08/22/android-list-view-from-database-with-cursor-adapter/ –

相關問題