检索到的数据不会显示在listview上

时间:2012-01-12 14:53:42

标签: android database sqlite listview android-arrayadapter

这是我的代码,     包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();
    }

}

}

以下显示了我的Array Adapter类

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>

但由于某种原因未填充列表视图。它只显示黑屏。有人可以帮帮我吗?

谢谢......

2 个答案:

答案 0 :(得分: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 :(得分:0)

我认为您的值为空:

String[] values = null;

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