1

I am working on android applications. In my project I need to create Listview. My layout i.e info.xml contains topbar with one image view, footer with other imageview. Also in the center of the layout I kept an imageview and on that I have created the Listview. Also I have created row.xml for the textview to display listitems. Now when I click on each listitem a new intent should be called...i.e when I click on 1st listitem page1 should open. Similarly if I click on 2nd listitem page2 should open and so on. So how could I do that. I am struggling for this since 3 days but didnt find any correct solution. Please hgelp me regarding this.

My Code:

public class Information extends Activity 
{
private String[] Countries;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
Countries = getResources().getStringArray(R.array.countries);
ListView list = (ListView)findViewById(R.id.listnew);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.row, Countries);
list.setAdapter(adapter);
registerForContextMenu(list);  }
} 
user370305
  • 108,599
  • 23
  • 164
  • 151
Madhuri
  • 33
  • 1
  • 5

1 Answers1

5

All you have to add is

list.setOnItemClickListener(this);

Then you let your class implement the OnItemClickListener interface and create this method:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    switch(position){
        case 0:
            Intent firstIntent = new Intent(this, MyClass.class);
            startActivity(firstIntentIntent);
            break;  
        case 1:
            Intent secondIntent = new Intent(this, MySecondClass.class);
            startActivity(secondIntentIntent);
                    break;

            [... etc ...]

   }    

}

In this case, if the first item is clicked, it will launch the MyClass Activity, if the second item is clicked, the MySecondClass Activity will be launched, etc.

Yes, it is a bit tedious but it is the best way.

Mark D
  • 1,309
  • 4
  • 19
  • 38