2012-01-12 101 views
0

這裏是我的代碼, package com.mjzone.project;檢索的數據不顯示在列表視圖

public class MyDatabase extends Activity { 

final protected String DATABASE_TABLE = "details"; 
private MyDatabaseHelper myHelper; 
Context mjContext; 
SQLiteDatabase myDb; 
String[] values = null; 

// ====== Constructor for MyDatabse class ========== 

public MyDatabase(Context c) { 
    mjContext = c; 

} 

private class MyDatabaseHelper extends SQLiteOpenHelper { 

    public MyDatabaseHelper(Context context) { 
     super(context, "MapDatabase", null, 1); 
     // TODO Auto-generated constructor stub 
    } 

    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE IF NOT EXISTS " 
       + DATABASE_TABLE 
       + " (" 
       + BaseColumns._ID 
       + " INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR NULL, description VARCHAR NULL,latitude VARCHAR NULL, longitude VARCHAR NULL,image BLOB NULL, category VARCHAR NULL)"); 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { 
     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
     onCreate(db); 
     // TODO Auto-generated method stub 

    } 

} 

// ========= to open connection ============== 

public MyDatabaseHelper open() throws Exception { 
    myHelper = new MyDatabaseHelper(mjContext); 
    myDb = myHelper.getWritableDatabase(); 

    return myHelper; 

} 

// ======== to close connection =============== 

public void close() throws Exception{ 
    myHelper.close(); 

} 

// ========= to create entry ================== 

public long createEntry(byte[] iimage, String iname, String idescription,String ilatitude, String ilongitude) { 

    ContentValues myBundle = new ContentValues(); 

    myBundle.put("name", iname); 
    myBundle.put("description", idescription); 
    myBundle.put("latitude", ilatitude); 
    myBundle.put("longitude", ilongitude); 
    myBundle.put("image", iimage); 
    myBundle.put("category", "NT"); 

    return myDb.insert(DATABASE_TABLE, null, myBundle); 

} 

// =========== get data ======================== 

public String[] getData() throws Exception { 

    final String[] columns = { BaseColumns._ID, "name", "description","latitude", "longitude", "image", "category" }; 
    Cursor c = myDb.query(DATABASE_TABLE, columns, null, null, null, null,null); 

    int xname = c.getColumnIndex("name"); 
    int xdescription = c.getColumnIndex("description"); 
    int xcategory = c.getColumnIndex("image"); 

    int i = 0; 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 

     values[i] = c.getString(xcategory) + "\t" + c.getString(xname) + "\t" + c.getString(xdescription); 
     i++; 

    } 
      c.close(); 
    return values; 

} 

}

上面是數據庫類的代碼,這裏是列表試聽課

public class MyListDemo extends ListActivity{ 

String[] data; 
@Override 
protected void onCreate(Bundle arg0) { 
    // TODO Auto-generated method stub 
    super.onCreate(arg0); 

    try { 
     MyDatabase dbObject = new MyDatabase(this); 
     dbObject.open(); 
     data = dbObject.getData(); 
     MyArrayAdapter adpter = new MyArrayAdapter(this, data); 
     setListAdapter(adpter); 
     dbObject.close(); 

    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 

}

下面顯示了我的數組適配器類

public class MyArrayAdapter extends ArrayAdapter<String> { 

private String[] values; 
private Context context; 

public MyArrayAdapter(Context context, String[] values) { 
    super(context, R.layout.rowlayout, values); 
    this.context = context; 
    this.values = values; 
    // TODO Auto-generated constructor stub 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false); 
    TextView textView = (TextView) rowView.findViewById(R.id.label); 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); 
    textView.setText(values[position]); 
    // Change the icon for Windows and iPhone 
    String s = values[position]; 

    if (s.startsWith("NT")) { 
     imageView.setImageResource(R.drawable.ic_launcher); 
    } else { 
     imageView.setImageResource(R.drawable.camera); 
    } 

    return rowView; 
} 

這是我的layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="22px" 
    android:layout_height="22px" 
    android:layout_marginLeft="4px" 
    android:layout_marginRight="10px" 
    android:layout_marginTop="4px" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

<TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" 
    android:textSize="30px" > 
</TextView> 

但列表視圖不填充由於某種原因。它只顯示黑屏。任何人都可以幫我嗎?

感謝...

回答

0

嘗試使用SimpleCursorAdapter,而不是這樣的:1周的ImageView和2個TextViews(TV1,TV2)創建一個佈局,讓我們把它叫做listview_item

Cursor c = myDb.query(DATABASE_TABLE, columns, null, null, null, null,null); 

String[] from={"category","name","description"} 
int[] to={R.id.IV1,R.id.TV1,R.id.TV2} 

SimpleCursorAdapter adapter=new SimpleCursorAdapter(this, listview_item, c, from, to); 

adapter.setViewBinder(new ViewBinder() { 

    public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) { 
     if(aColumnIndex==aCursor.getColumnIndex("category")){ 
      String s = aCursor.getString(aCursor.getColumnIndex("category")); 

      if (s.startsWith("NT")) { 
       ImageView IV= (ImageView) aView; 
       IV.setImageResource(R.drawable.ic_launcher); 
      } else { 
       IV.setImageResource(R.drawable.camera); 
      } 
      return true; 
     } 
     return false; 
    } 
}); 
+1

我只是親愛的,仍然是相同的。我的getData()方法是否正確?我想將數據添加到字符串數組,然後通過數組適配器檢索數據。但我不知道如果這是正確的方法 – Manoj 2012-01-12 15:10:45

+1

一旦我改變它,我得到這個錯誤日誌。然後,我添加了c.close(),但仍然顯示相同的錯誤,01-12 20:43:36.295:E/Cursor(686):完成尚未停用或關閉的遊標。 database = /data/data/com.mjzone.project/databases/MapDatabase,table = details,query = SELECT _id,name,description,latitude,longitude,image,category FROM details – Manoj 2012-01-12 15:14:33

+1

爲什麼我不斷收到這個錯誤,我關上了光標? 01-12 20:43:36.295:E/Cursor(686):android.database.sqlite.DatabaseObjectNotClosedException:應用程序未關閉在此處打開的遊標或數據庫對象 – Manoj 2012-01-12 15:20:23

0

我覺得你的價值觀爲空:

String[] values = null; 

values[i] = c.getString(xcategory) + "\t" + c.getString(xname) + "\t" + c.getString(xdescription); 
+0

我把它設置爲「String [] values;」仍然是同樣的問題.. – Manoj 2012-01-12 15:16:54

+1

這就是一樣的。它應該像String [] values = new String [「你在數組中需要的數字或位置」]; – sfratini 2012-01-12 15:20:12

+0

我糾正它。但我得到同樣的錯誤..我懷疑我的getData()。那種方法有什麼錯誤嗎? – Manoj 2012-01-12 15:41:37