开发者

Passing parameters between activities not working

Passing parameters works with 2 normal activities, but when I try to pass this from the gridview to another activity, the value returns null

First Activity

        gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            //This takes the position id of the image and change
            //it to match category id
            position = position + 1;
            String positionId = String.valueOf(position);

            System.out.println(positionId);

            Toast.makeText(MenuActivity.this, "position =" + positionId + "\nid =" + id, Toast.LENGTH_SHORT).show();
            Intent myIntent = new Intent(v.getContext(), MenuItemActivity.class);
            myIntent.putExtra("hello", "hello");
            startActivityForResult(myIntent, 0);


        }
    });

My secondary activity (MenuItemActivity.java)

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.menuitem);


    try{
        Bundle extras = getIntent().getExtras();
        String category = null;
  开发者_开发百科      extras.getString("Category "+extras.getString("hello"));
        System.out.println("Category = "+category);
    } catch(Exception e){ 
        System.out.println("Error "+e.getMessage());
    }


from here it looks like a master >> detail page. if so why are you calling

startActivityForResult(myIntent, 0);

instead of a simple

startActivity(myIntent);

can you try this and tell me if this works.

getIntent().getStringExtra("hello");


In your second activity, you need to call setResult to actually pass data back to the first activity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜