1

我的主要活动中有一个游标来查询数据库的两列并使用简单的游标适配器显示它们。

private Cursor doQuery() {
return(db.getReadableDatabase().rawQuery("SELECT RestaurantID AS _id, RestaurantName, StoreNo "
+ "FROM RestaurantList ORDER BY RestaurantName", null));

}

public void onPostExecute() {
SimpleCursorAdapter adapter;

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
adapter=new SimpleCursorAdapter(this, R.layout.activity_all_restaurants,
        doQuery(), new String[] {
    RestaurantDB.colRestaurantName,
    RestaurantDB.colRestaurantStore },
    new int[] { R.id.title, R.id.value },
    0);
}
else {
adapter=new SimpleCursorAdapter(this, R.layout.activity_all_restaurants,
        doQuery(), new String[] {
    RestaurantDB.colRestaurantName,
    RestaurantDB.colRestaurantStore },
    new int[] { R.id.title, R.id.value });
}

setListAdapter(adapter);

}

我想要做的是,当用户单击特定项目时,会弹出另一个活动,为用户提供更多信息,这意味着我还需要显示存储在同一项目的许多其他列中的数据。我的问题是我该怎么做。我以为我应该使用onListItemClick,但我不知道如何将主活动中选择的项目与新活动相关联。

此外,在主要活动中,我在列表中有两个文本视图,分别显示 colRestaurantName 和 colRestaurantStore。当我使用 onListItemClick 时,如何才能检索 colRestaurantName?

我可以执行以下操作来检索位置,但我不知道如何在新活动中使用它:

@Override
public void onListItemClick(ListView parent, View v, int position,
    long id){
Cursor c = ((SimpleCursorAdapter)parent.getAdapter()).getCursor();
selection = String.valueOf(c.getPosition());
Intent i = new Intent(AllRestaurants.this, RestaurantsInfo.class);
i.putExtra("restaurantSelection", selection);
startActivity(i);      
}

谢谢。

4

1 回答 1

0

实现onListItemClicklistview 并获取 textview 文本并将其发送到下一个活动:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
       TextView tv = (TextView)v.findViewById(R.id.title);
      String strcolRestaurantName=tv.getText().toString();

      Intent intent =new Intent(CurrentActivity.this,NextActivity.class);
      Bundle bundle = new Bundle();
      bundle.putString("RestaurantName",strcolRestaurantName);
      intent.putExtras(bundle);
      startActivity(intent);
}}

并在 NextActivity 中获取 Intent ,如 onCreate :

Intent i = getIntent();
Bundle extras = i.getExtras();

String strRestaurantName = extras.getString("RestaurantName");
于 2012-12-15T08:30:10.577 回答