вот мой код, 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();
}
}
}
Ниже показан мой массив класса 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>
, но в виде списка не заполняется по какой-то причине. он показывает только черный экран. Может ли кто-нибудь помочь мне?
благодаря ...
Я просто пробовал дорогой, все тот же. мой метод getData() правильный? Я хотел добавить данные в строковый массив, а затем получить данные через адаптер массива. Но я не уверен, что это правильный метод – Manoj
Как только я его изменю, я получаю этот журнал ошибок. Затем я добавил 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
Почему я продолжаю получать эту ошибку, даже после Я закрыл курсор? 01-12 20: 43: 36.295: E/Cursor (686): android.database.sqlite.DatabaseObjectNotClosedException: приложение не закрыло объект курсора или базы данных, который был открыт здесь – Manoj