How to call same activity (same layout) with diffe

2019-05-24 12:31发布

I am new to android field. i am developing restaurant menu application, i have created list view which shows names of items in restaurant. when i select any one item in list view it will launch another activity, it contains text view field showing detail description of selected item. similarly when i select other items, it should launch same activity but data should be of that selected item, how to achieve this ? if i use sqlite database for data source.

4条回答
贼婆χ
2楼-- · 2019-05-24 12:44

You'll want to use the extras Bundle of an Intent, e.g.

Let's assume you have a MenuListActivity which shows the menu as list, and an MenuItemActivity which shows a single menu item in more detail.

In your MenuListActivity.onListItemClick() you want to pass the clicked menu item ID to your MenuItemActivity:

public void onListItemClick(ListView list, final View view, int position, long id) {
    final Intent intent = new Intent(this, MenuItemActivity.class); // from -> to
    intent.putExtra(MenuItemActivity.ITEM_ID, id); // extra arguments: the menu item ID
    startActivity(intent);
}

In your MenuItemActivity you'll have a constant to identify the menu item (used above):

public static final String ITEM_ID = "item_id";

...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    long menuItemId = extras.getLong(ITEM_ID);

    // fetch your menu item from database, using ID
查看更多
成全新的幸福
3楼-- · 2019-05-24 12:47

You need to pass the values onItemClick:

Intent intent = new Intent(context, CalledActivity.class);
        intent.putExtra(key, value);
        startActivity(intent);

If you want some data back from called Activity then you can use startActivityForResult() as:

Intent intent = new Intent(context, CalledActivity.class);
        intent.putExtra(key, value);
        startActivityForResult(intent, requestCode);

In called activity you can set data as:

setResult(RESULT_OK, intent);

Note: Here you set the value in intent and pass it to setResult().

On returning back to calling Activity you can get data by overriding:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            //Get data from Intent "data" and do your task here....
        }
    }

Note: You can pass primitive data type values thru Intent and if you want to pass other types then you have to use Bundle like this.

Bundle data = new Bundle();
        data.putIntArray(key, value);

//same way you can set other values.......
//Now set this Bundle value to Intent as you do for primitive type....

Intent intent = new Intent(context, CalledActivity.class);
        intent.putExtra(data);
        startActivity(intent);

Receiving data in Activity:

//For primitive values:
DataType var_name = getIntent().getExtras().get(key);

//For Bundle values:
Bundle var_name = getIntent().getExtras().getBundle(key);
查看更多
你好瞎i
4楼-- · 2019-05-24 12:52

Use intent extras to achieve this. You can transfer data to your activity by using intent.putExtra("KEY", VALUE). In the receiving activity you get the extras by calling getIntent().getExtras(); which returns a Bundle with the extras you've added in the calling activity.

查看更多
倾城 Initia
5楼-- · 2019-05-24 12:58

Depends on what you are trying to achieve.Suppose you want to send the position of the list view item which is clicked then on click method send id to the activity with the intent

  public void onItemClick(AdapterView<?> arg0, View view, int position,
                long id) {
            //listView.getItemIdAtPosition(position);
            // TODO Auto-generated method stub
          Intent i=new Intent(MyActivity.this,NewActivity.class);
          i.putExtras("position",""+position);
          startActivity(i);


        }

Similarly if you want to send data,have to work accordingly

查看更多
登录 后发表回答