wasabiman
wasabiman

Reputation: 527

ListView assistance

I'm working on a project at school, and I need help implementing ListViews properly. I have to do the Settings and Help sections on the project and as of now, I can display what's on my list and it displays Toast messages of the selected item when I click it. My question is, how can I create and show the content that is inside that specific item? For example, I have an option "Edit Password" and that when I click it, it should display my "Edit Password" screen. This applies to both my sections.

This is what I have so far. It's basically the android ListView tutorial but I added my options on there. My question is, when I click one of the options on my list, how can I display its specific details? Like what I said before, if I click "Edit Password", I want it to go to a "Edit Password" screen. Or on Help, if I click let's say, "Credits", I want it direct me to the Credits page.

public class Settings extends ListActivity {
        /** Called when the activity is first created. */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          setListAdapter(new ArrayAdapter<String>(this, R.layout.settings, SETTINGS));

          ListView lv = getListView();
          lv.setTextFilterEnabled(true);
          lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) 
            {   
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                      Toast.LENGTH_SHORT).show();
            }
          });
        }

    static final String[] SETTINGS = new String[] 
            {"Set Refresh Rate", "Edit Password", "Delete Account"};

}

Upvotes: 2

Views: 357

Answers (1)

user
user

Reputation: 87064

When you extend ListActivity you already have the OnItemClickListener implemented, you should override the method onListItemClick. In that method you should use an Intent to get to a new Activity where you will display the stuff you want:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Intent i = new Intent(this, SecondActivityName.class);
    i.putExtra("pos", position); //pass the position of the clicked row so we know what to show.
    startActivity(i); // let's go to the other activity
}

SeconActivityName is an Activity that you should create and where you would show the other screen you want(remember to add the activity to the manifest file!):

    public class SecondActivityName extends Activity {

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                Intent i = getIntent(); 
                int rowPosition = i.getIntExtra("pos", -1); //we now have the row's position that was clicked in the other activity.
                // based on that you show the screen you want for example after a switch
                switch (rowPosition) {
                     case -1:
                        //this is the default value, something has gone terrible wrong
                        //finish the activity and hide:
                        finish();
                        break;
                     case 0:
                        //row 0 was clicked so show the "Set Refresh Rate" screen
                        setContentView(R.layout.refresh_rate__screen);
                        break;
                //do the same for the other rows;
//...

This will work if the screens for the various settings are not that different. If they are you'll probably have to implement a different activity for each one and start the appropriate activity in the onListItemClick based on the position parameter.

Upvotes: 1

Related Questions