我想使用SimpleCursorAdaptor显示联系信息,当我从数据库获取信息时它没有显示任何内容。但我已通过cursor.count()检查存在数据。
这是listactivity
public class Contacts extends ListActivity {
private ArrayList<TempInfo> contactsList = null;
// Attributes
private static String name = "";
private static String phone = "";
private static int rowId = 0;
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);
contactsList = new ArrayList<TempInfo>();
CardsDatabase info = new CardsDatabase(this);
info.open();
Cursor c = info.geTContactsData();
Log.d("cursor length",Integer.toString(c.getCount()));
c.moveToFirst();
while (c.isAfterLast() == false) {
name = c.getString(c.getColumnIndex(CardsDatabase.NAME_R));
phone = c.getString(c.getColumnIndex(CardsDatabase.PHONE_R));
rowId = c.getInt(c.getColumnIndex(CardsDatabase.ROW_R));
TempInfo s = new TempInfo(rowId, name, phone);
contactsList.add(s);
c.moveToNext();
}
c.close();
Cursor cursor = info.geTContactsData();
startManagingCursor(cursor);
String[] columns = new String[] { CardsDatabase.ROW_R,
CardsDatabase.NAME_R, CardsDatabase.PHONE_R };
int[] to = new int[] { R.id.row_id, R.id.contact_name,
R.id.contact_number };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(Contacts.this,
R.layout.contact_list_entry, cursor, columns, to);
this.setListAdapter(mAdapter);
cursor.close();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
TempInfo tempInfo = contactsList.get(position);
Intent i = new Intent(Contacts.this, ContactsDetail.class);
Bundle bundle = new Bundle();
bundle.putInt("rowid", tempInfo.rowid);
i.putExtra("personBundle", bundle);
startActivity(i);
}
}
这是contact_list xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#66000000"
android:cacheColorHint="#6E000000"
android:paddingLeft="5dp"
android:text=" Contacts"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:cacheColorHint="#00000000" >
</ListView>
这是contact_list_entry xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<TextView
android:id="@+id/row_id"
android:layout_width="25dp"
android:layout_height="fill_parent"
android:background="#5C5858"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/contact_name"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:ellipsize="marquee"
android:singleLine="true"
android:textStyle="bold" />
<TextView
android:id="@+id/contact_number"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:ellipsize="marquee"
android:singleLine="true" />
</LinearLayout>
</LinearLayout>
这是数据库
public class CardsDatabase {
public static String USERTEMPLATE = "user";
public static String MYCARD = "mycard";
public static String ALERT = "alert";
public static String USER = "user";
public static String NAME = "name";
public static String COMPANY = "company";
public static String TITLE = "title";
public static String ADDRESS = "address";
public static String PHONE = "phone";
public static String CEL = "cel";
public static String EMAIL = "email";
public static String PHOTO = "photo";
public static String ROW_R = "_id";
public static String NAME_R = "rname";
public static String COMPANY_R = "rcompany";
public static String TITLE_R = "rtitle";
public static String ADDRESS_R = "raddress";
public static String PHONE_R = "rphone";
public static String CEL_R = "rcel";
public static String EMAIL_R = "remail";
public static String PHOTO_R = "rphoto";
public static String TEMPLATE_R = "rtemplate";
public static String MAC = "macaddress";
private static String DATABASE_NAME = "CardsDatabase";
public static String DATABASE_TABLE_TEMPLATE = "TemplateTable";
public static String DATABASE_TABLE_MYTABLE = "MYTable";
public static String DATABASE_TABLE_CONTACTS = "ContactsTable";
public static String DATABASE_TABLE_CONTACTS_REMOTE = "rContactsTable";
private static int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// Log.d(Tag,"Inner on create called");
db.execSQL("CREATE TABLE " + DATABASE_TABLE_TEMPLATE + " ("
+ USERTEMPLATE + " TEXT NOT NULL," + MYCARD
+ " TEXT NOT NULL," + ALERT + " TEXT NOT NULL" + ");");
db.execSQL("CREATE TABLE " + DATABASE_TABLE_MYTABLE + " (" + USER
+ " TEXT NOT NULL," + NAME + " TEXT NOT NULL," + COMPANY
+ " TEXT," + TITLE + " TEXT," + ADDRESS + " TEXT," + PHONE
+ " TEXT NOT NULL," + CEL + " TEXT," + EMAIL
+ " TEXT NOT NULL," + PHOTO + " BLOB" + ");");
db.execSQL("CREATE TABLE " + DATABASE_TABLE_CONTACTS_REMOTE + " ("
+ ROW_R + " integer primary key autoincrement," + NAME_R
+ " TEXT NOT NULL," + COMPANY_R + " TEXT," + TITLE_R
+ " TEXT," + ADDRESS_R + " TEXT," + TEMPLATE_R
+ " TEXT NOT NULL," + PHONE_R + " TEXT NOT NULL," + CEL_R
+ " TEXT," + EMAIL_R + " TEXT NOT NULL," + PHOTO_R
+ " BLOB" + ");");
db.execSQL("CREATE TABLE " + DATABASE_TABLE_CONTACTS + " (" + MAC
+ " TEXT NOT NULL" + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE_TEMPLATE);
db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE_MYTABLE);
db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE_CONTACTS);
db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE_CONTACTS_REMOTE);
onCreate(db);
}
}
public CardsDatabase(Context c) {
ourContext = c;
}
public CardsDatabase open() throws SQLException {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public void createEntry(int type, ContentValues cv) {
// TODO Auto-generated method stub
switch (type) {
case 1:
ourDatabase.insert(DATABASE_TABLE_TEMPLATE, null, cv);
break;
case 2:
ourDatabase.insert(DATABASE_TABLE_MYTABLE, null, cv);
break;
case 3:
ourDatabase.insert(DATABASE_TABLE_CONTACTS, null, cv);
break;
case 4:
ourDatabase.insert(DATABASE_TABLE_CONTACTS_REMOTE, null, cv);
break;
}
}
public Cursor getTemplate() throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[] { USERTEMPLATE, MYCARD, ALERT };
Cursor c = ourDatabase.query(DATABASE_TABLE_TEMPLATE, columns, null,
null, null, null, null);
return c;
}
public Cursor getMyData() throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[] { USER, NAME, COMPANY, TITLE, ADDRESS,
PHONE, CEL, EMAIL, PHOTO };
Cursor c = ourDatabase.query(DATABASE_TABLE_MYTABLE, columns, null,
null, null, null, null);
return c;
}
public Cursor geTContactsData() throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[] { ROW_R, NAME_R, PHONE_R};
Cursor c = ourDatabase.query(DATABASE_TABLE_CONTACTS_REMOTE, columns,
null, null, null, null, null);
return c;
}
public Cursor getDetailData(int row_id) {
String WHERE = String.format("%s='%d'", ROW_R, row_id);
String[] columns = new String[] { NAME_R, TITLE_R, ADDRESS_R, PHONE_R,
CEL_R, EMAIL_R, TEMPLATE_R, PHOTO_R };
Cursor c = ourDatabase.query(DATABASE_TABLE_CONTACTS_REMOTE, columns,
WHERE, null, null, null, null);
return c;
}
public boolean contactExist(String mac_adr) throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[] { MAC };
String WHERE = String.format("%s='%s'", MAC, mac_adr);
Cursor c = ourDatabase.query(DATABASE_TABLE_CONTACTS, columns, WHERE,
null, null, null, null);
return c.getCount() > 0 ? true : false;
}
public void updateTemplate(ContentValues cv) {
String m = "me";
String WHERE = String.format("%s='%s'", USERTEMPLATE, m);
ourDatabase.update(DATABASE_TABLE_TEMPLATE, cv, WHERE, null);
}
public void updateMytable(ContentValues cv) {
String m = "me";
String WHERE = String.format("%s='%s'", USERTEMPLATE, m);
ourDatabase.update(DATABASE_TABLE_MYTABLE, cv, WHERE, null);
}
}
Logcat显示此
02-28 09:18:09.702: D/cursor length(5152): 10
答案 0 :(得分:1)
在onCreate()中设置适配器后,从代码中删除cursor.close();
。