1

I know this has been asked here but the answers were quite confusing. I have 3 items in my ListView. They are "Aluminium", "Gold" and "Zinc". Through each one of them, I want to start different activities and for that I have created the 3 activities which i named "Aluminium.java","Gold.java" and "Zinc.java"

I have used this ListView in a drawer layout for the navigation drawer. I implemented navigation drawers through the code given below which i got from a site.This code changes fragments and its not working properly. Instead of fragments, I want to switch activities.

I want to achieve 3 things:

  1. Switch between activities through the listview in the navigation drawer.
  2. To achieve point 1, I want to get the clicked list item and then use intents.
  3. I want all the 3 activities to have this navigation drawer.

Sorry if its too dumb but I am a beginner. Please help me out with the code.

Java code

public class MainActivity extends FragmentActivity {
final String[] data ={"Aluminium","Gold","Zinc"};
final String[] fragments ={
        "com.Chinmay.navigationdrawer.Gold",
        "com.Chinmay.navigationdrawer.Aluminium",
        "com.Chinmay.navigationdrawer.Zinc"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);

    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    final ListView navList = (ListView) findViewById(R.id.left_drawer);
    navList.setAdapter(adapter);
    navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){

            drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                @Override
                public void onDrawerClosed(View drawerView){
                    super.onDrawerClosed(drawerView);
                    android.support.v4.app.FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                    tx.replace(R.id.content_frame, Fragment.instantiate(MainActivity.this, fragments[pos]));
                    tx.commit();
                }
            });
            drawer.closeDrawer(navList);
            android.support.v4.app.FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
            tx.replace(R.id.content_frame,Fragment.instantiate(MainActivity.this, fragments[0]));
            tx.commit();
        }
    });
}

}

Vigbyor
  • 2,568
  • 4
  • 21
  • 35
Chinmay Dabke
  • 5,070
  • 11
  • 40
  • 63

1 Answers1

1

Make a base activity class and put all your drawer code there, and extend this base class for your 3 activity, in that way, you'll have drawer for your all activities.

class Gold extends BaseActivity{
}

For the clicking part, you already set an item click listener, just make a switch case such as

 switch (pos){
    case 0:
       Intent i = new Intent(this,Gold.java);
       startActivity(i);
       break;
    }
 // fill the rest
 }
Orhan Obut
  • 8,756
  • 5
  • 32
  • 42
  • But how do I know if the user has clicked gold or aluminium or zinc? – Chinmay Dabke Jan 20 '14 at 08:29
  • Since it's only 3 item, I think you can just use the position which you already know 0 is Gold, 1 is Aluminium etc, or make a inner class something like MenuItem and put a variable to define that it is aluminium, or check the view text etc etc. There are many way to achieve this, it's up to you – Orhan Obut Jan 20 '14 at 08:31
  • Actually there are 119 items! I am done with 3 and i am also going to add search functionality by adding a search bar at the top of the drawer layout. So, the order of the items will change. what should i do now? – Chinmay Dabke Jan 20 '14 at 08:32
  • You can either compare String since you are using ArrayAdapter, in the onItemClickListener method, adapter.getItem() will return text input, so you can compare it or you can make a class such MenuItem and make a custom Adapter by extending BaseAdapter or ArrayAdapter, and override getItem and return an Id to check. http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view – Orhan Obut Jan 20 '14 at 08:43
  • can u please post the code by editing your answer. It will make it more clear. – Chinmay Dabke Jan 20 '14 at 08:51