我有一个项目列表,数据存储在SQLite中。当我想获得项目的详细视图时,问题就出现了,它没有显示任何数据。 这是一个在列表视图中显示数据库中所有行的活动,效果很好。
public class List extends Activity {
public final static String ID_EXTRA="com.example.bazadanych._ID";
DBAdapter myDB;
String passedV=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
passedV=getIntent().getStringExtra(MainActivity.ID_EXTRA);
openDB();
populateListViewDB ();
onclickcallback();
}
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}
private void openDB() {
myDB = new DBAdapter(this);
myDB.open();
}
private void closeDB() {
myDB.close();
}
private void populateListViewDB() {
Cursor cursor = myDB.getAllRows();
startManagingCursor(cursor);
String[] fromFieldNames = new String[]
{DBAdapter.KEY_NAME, DBAdapter.KEY_COUNTRY, DBAdapter.KEY_REGION, DBAdapter.KEY_PHONE};
int [] toViewIDs = new int []
{R.id.list_item_name, R.id.list_item_country, R.id.list_item_region, R.id.list_icon_item};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.list_item,
cursor,
fromFieldNames,
toViewIDs
);
ListView myList = (ListView) findViewById(R.id.listViewDB);
myList.setAdapter(myCursorAdapter);
}
private void onclickcallback() {
ListView myList = (ListView) findViewById(R.id.listViewDB);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
Intent intent = new Intent(List.this, Details.class);
intent.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(intent);
}
});
}
}
从List.class中单击列表中的项后,用户可以移动到details.class,以显示该项的详细信息。不幸的是,它没有显示任何内容,也没有给出任何错误。
我将ID从一个活动传递到另一个活动,它从一个活动转移到另一个活动但后来又没有显示任何数据,
public class Details extends Activity {
DBAdapter myDB;
String passedV=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
passedV=getIntent().getStringExtra(List.ID_EXTRA);
myDB = new DBAdapter(this);
}
private void populatedetailsViewDB() {
Cursor cursor = myDB.getRow(0);
startManagingCursor(cursor);
String[] fromFieldNames = new String[]
{DBAdapter.KEY_NAME, DBAdapter.KEY_COUNTRY, DBAdapter.KEY_REGION, DBAdapter.KEY_ADRESS, DBAdapter.KEY_PHONENUM, DBAdapter.KEY_PHONE};
int [] toViewIDs = new int []
{R.id.item_name, R.id.item_country, R.id.item_region, R.id.item_adress, R.id.item_phonenum, R.id.list_icon_item};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.item_layout,
cursor,
fromFieldNames,
);
ListView myList = (ListView) findViewById(R.id.detailsViewDB);
myList.setAdapter(myCursorAdapter);
}
}
从DBAdapter获得一行。
public class DBAdapter {
...
public DBAdapter(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelper(context);
}
// Open the database connection.
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}
// Close the database connection.
public void close() {
myDBHelper.close();
}
...
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
任何人都可以看到可能出现的问题吗?
答案 0 :(得分:0)
因此,您要在DBAdapter类的getRow方法上查询数据库,该方法返回指向单行的游标。但是,在详细信息活动中,您始终调用getRow(0),而不是项目的实际行。
您可能想要更改此方法:
private void populatedetailsViewDB() {
myDB.open();
Cursor cursor = myDB.getRow(Long.parseLong (passedV));
startManagingCursor(cursor);
String[] fromFieldNames = new String[]
{DBAdapter.KEY_NAME, DBAdapter.KEY_COUNTRY, DBAdapter.KEY_REGION, DBAdapter.KEY_ADRESS, DBAdapter.KEY_PHONENUM, DBAdapter.KEY_PHONE};
int [] toViewIDs = new int []
{R.id.item_name, R.id.item_country, R.id.item_region, R.id.item_adress, R.id.item_phonenum, R.id.list_icon_item};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.item_layout,
cursor,
fromFieldNames,
);
ListView myList = (ListView) findViewById(R.id.detailsViewDB);
myList.setAdapter(myCursorAdapter);
myDB.close();
}