2012-01-12 98 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