1

データベースの2 つの列をクエリし、単純なカーソル アダプターを使用してそれらを表示するためのメイン アクティビティにカーソルがあります。

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 を使おうと思ったのですが、メインアクティビティで選択したアイテムを新しいアクティビティに関連付ける方法がわかりません。

また、メイン アクティビティでは、リストに 2 つのテキストビューがあり、それぞれ 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

listviewを実装onListItemClickし、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では、onCreateのようにIntentを取得します。

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

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