-1

I want to link the activity from main activity in list view to other activity using intent.

When i linked it using intent and direct the result to the frame layout that has id (R.id.main) in mainactivity ,it happens a problem.

Any one can help me?

The problem StartActivity (intent, R.id.main)

  String [] menus=getResources().getStringArray(R.array.menus);

  //currently selected river
  data=menus[pos];
  if (data.equals("One")) {
        Intent intent = new Intent(MainActivity.this, Home.class);
        startActivity(intent,R.id.main);
  }
  else if (data.equals("Two")) {
        Intent in = new Intent(MainActivity.this, Page1.class);
        startActivity(in,R.id.main);
  }                                             
  else if(data.equals("Three")){
        Intent inte = new Intent(MainActivity.this, Page2.class);
        startActivity(inte,R.id.main);                 
  }
random
  • 10,238
  • 8
  • 57
  • 101

3 Answers3

0

The startActivity() method takes the second parameter as a Bundle. But you are passing an int value. Change your code from this

Intent intent = new Intent(MainActivity.this, Home.class);
startActivity(intent,R.id.main);

into this

 Intent in = new Intent(MainActivity.this, Page1.class);
 startActivity(in);
Emil
  • 2,786
  • 20
  • 24
0
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.menus));

 final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
 final ListView navList = (ListView) findViewById(R.id.drawer);

 navList.setAdapter(adapter);

 ImageView burger=(ImageView) findViewById(R.id.btnback) ;
 burger.setOnClickListener(new View.OnClickListener () {

        @Override
        public void onClick (View v) {
            drawer.openDrawer(navList);

        }   

        });



 navList.setOnItemClickListener(new OnItemClickListener(){
     @Override
     public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){

         String [] menus=getResources().getStringArray(R.array.menus);

            //currently selected river
            data=menus[pos];
                     if (data.equals("One")) {
                        Intent intent = new Intent(MainActivity.this, Home.class);
                        startActivity(intent,R.id.main);
                    }
                    else if (data.equals("Two")) {
                        Intent in = new Intent(MainActivity.this, Page1.class);
                        startActivity(in);
                    }


                    else if(data.equals("Three")){
                        Intent inte = new Intent(MainActivity.this, Page2.class);
                        startActivity(inte);
0

Try like the below,

        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        Bundle mBundle = new Bundle();
        mBundle.putInt("key", R.id.main);
        intent.putExtras(mBundle);
        startActivity(intent);

So your Program will look like after change

String[] menus = getResources().getStringArray(R.array.menus);

    // currently selected river
    data = menus[pos];
    if (data.equals("One")) {

        Intent intent = new Intent(MainActivity.this, Home.class);
        Bundle mBundle = new Bundle();
        mBundle.putInt("key", R.id.main);
        intent.putExtras(mBundle);
        startActivity(intent);
    } else if (data.equals("Two")) {
        Intent intent = new Intent(MainActivity.this, Page1.class);
        Bundle mBundle = new Bundle();
        mBundle.putInt("key", R.id.main);
        intent.putExtras(mBundle);
        startActivity(intent);
    }

    else if (data.equals("Three")) {
        Intent intent = new Intent(MainActivity.this, Page2.class);
        Bundle mBundle = new Bundle();
        mBundle.putInt("key", R.id.main);
        intent.putExtras(mBundle);
        startActivity(intent);

    }

Get the value on the target Activity

myVariable = getIntent().getExtras().getInt("key");

Hope this will be helpful...thanks

sahu
  • 1,188
  • 10
  • 19