简体   繁体   中英

Passed data to new Activity is null

I have a problem with passing data to a new activity. The passed data is not null but it does not contain any data when .getExtras() is called from the new activity. When I debug, the data is null at the new activity. Here is my implementation-

FirstActivity.java

Cursor cursor = databaseAccess.getData("SELECT * FROM TYPE");
    imageList.clear();
    while (cursor.moveToNext()) {
        String id = cursor.getString(0);
        String name = cursor.getString(1);
        byte[] image = cursor.getBlob(2);

        imageList.add(new Food(id, name, image));
    }
    adapter.notifyDataSetChanged();

    //button to click to next page
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
            view.setSelected(true);
            Food labels = imageList.get(position);
            String no = labels.getId();
            Intent passIntent = new Intent(FirstActivity.this, SecondActivity.class);
            passIntent.putExtra("keyid", no);//pass id to next activity
            startActivity(passIntent);

        }
    });

SecondActivity.java

//declaration for gridview and implement adapter
    gridView = (GridView) findViewById(R.id.gridView2);
    imageList = new ArrayList<>();
    adapter = new CustomGridAdapter1(this, R.layout.second_list, imageList);
    gridView.setAdapter(adapter);

    Bundle data = getIntent().getExtras();
    rowId = data.getString("keyid");
    cursor = databaseAccess.getImage(rowId);
    while (cursor.moveToNext()) {
        String id = cursor.getString(0);
        String name = cursor.getString(1);
        byte[] image = cursor.getBlob(2);

        imageList.add(new Recipe(id, name, image));
    }
    adapter.notifyDataSetChanged();
}

databaseAcces.java

//display name and image in gridview at General.this
public Cursor getImage(String id) {
    Cursor cursor;
    String selectQuery = "SELECT RECIPE.id, RECIPE.name, RECIPE.image FROM RECIPE INNER JOIN TYPE WHERE RECIPE.type_id=TYPE.id AND RECIPE.type_id=\"" + id + "\" ";
    database = openHelper.getReadableDatabase();
    cursor = database.rawQuery(selectQuery, null);
    return cursor;
}

It happened cause you put your data in extra directly but in receive you want get it with bundle!

replace

Bundle data = getIntent().getExtras();
rowId = data.getString("keyid");

with

 rowId = getIntent().getStringExtra("keyid");

erfan's answer is also correct but it is not a very good practice to pass just the key and data to other activities, use Bundle instead.

The main purpose of Bundle is to pass data between activities. The values that are to be passed are mapped to String keys which are later used in the next activity to retrieve the values.

In Sending data between activities section of Android Developer site says:

We recommend that you use the Bundle class to set primitives known to the OS on Intent objects. The Bundle class is highly optimized for marshalling and unmarshalling using parcels.

To solve this issue, use Bundle to put data from calling activity and also use Bundle to get from the called activity.

So replace

String no = labels.getId();
Intent passIntent = new Intent(FirstActivity.this, SecondActivity.class);
passIntent.putExtra("keyid", no);//pass id to next activity

with

String no = labels.getId();
Bundle bundle = new Bundle();
bundle.putString("keyid", no);
Intent passIntent = new Intent(FirstActivity.this, SecondActivity.class);
passIntent.putExtras(bundle);//pass id to next activity

and keep the second activity as it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM