1

I want to make my class and layout change when I click on specific items. For example, if I click "physical" from the array list I want it to open up the physical java class and the physical xml layout. How do i do this? As you can see down below I have already tried with the View view and switch/case.

    import android.content.Intent;
    import android.support.v4.app.Fragment;        
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    import android.support.v4.app.FragmentActivity;
    import java.util.List;

    public class MainActivity extends AppCompatActivity  implements         AdapterView.OnItemClickListener{

private DrawerLayout drawerLayout;
private Toolbar toolbar;
private ListView listView;
private String[] planets;


@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_appbar);

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);

    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

    drawerLayout=(DrawerLayout)findViewById(R.id.drawerLayout);
    planets=getResources().getStringArray(R.array.planets);
    listView=(ListView) findViewById(R.id.drawerList);
    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, planets));
    listView.setOnItemClickListener(this);


}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement


    return super.onOptionsItemSelected(item);
}


public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


    Toast.makeText(this, planets[position] + " was selected", Toast.LENGTH_LONG).show();
    selectItem(position);
    selectItem2(view);
    }



public void selectItem(int position) {

    listView.setItemChecked(position, true);
    setTitle(planets[position]);}





public void selectItem2 (View view){

    switch (view.getId()) {
        case 0:
            Intent intent = new Intent(view.getContext(), physical_fragment.class);
            startActivityForResult(intent, 0);}}



public void setTitle(String title){
    getSupportActionBar().setTitle(title);
}
    }
user4792835
  • 159
  • 1
  • 1
  • 7

1 Answers1

0

You probably don't want compare the id of the View since all of the Views in your list will use android.R.layout.simple_list_item_1 they will all have the same id.

You may consider passing the position and comparing that.

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Toast.makeText(this, planets[position] + " was selected", Toast.LENGTH_LONG).show();
    selectItem(position);
    selectItem2(position);
}

public void selectItem2 (int position) {

    switch (position) {
        case 0:
            Intent intent = new Intent(MainActivity .this,    physical_fragment.class);
            startActivityForResult(intent, 0);
            break;
    }
}

Something like that should get you started. However, you may eventually want to check the content of the item selected such as the the actual position of the string resource then get that id in case you decide to switch around the list items or modify them in some other way.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Yes, because I didn't change that part of the answer. `view` doesn't exist in that method anymore. See the update – codeMagic Aug 06 '15 at 15:28
  • thank you but the app crashes when i click it. should the case and break be under my NavigationDrawerFragment.class ? – user4792835 Aug 06 '15 at 15:31
  • Look at the stacktrace for the error. You've got to do some debugging on your own. – codeMagic Aug 06 '15 at 15:32
  • lol, but im new to coding.. was i supposed to do something within the android manifest with my other classes that i have? – user4792835 Aug 06 '15 at 15:33
  • I don't know, depends on what you have. What does the stacktrace say?!? http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – codeMagic Aug 06 '15 at 15:34
  • im trying to upload picture of it – user4792835 Aug 06 '15 at 15:39
  • You need to add `physical_fragment` as an Activity in your manifest. FYI: that's a terrible name for an Activity if that is indeed what that class is – codeMagic Aug 06 '15 at 15:42