Reputation: 11
I am trying to open another activity when clicking the list item. I have created a list view with icon, title, and description. I want to be able to open another activity when i click each list item. I dont know where to put the code. I think it should be a switch statement that opens each position with intents.
Any ideas on how to make that happen ??? Thank you in advance!
here is my MainActivity.java
public class MainActivity extends Activity {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// put a switch statement if you want start different activities for each corresponding list item
//Start other activities
switch(position){
case 0:
Intent toActivity = new Intent(MainActivity.this, EuropaUnion.class);
startActivity(toActivity);
break;
case 1:
Intent toActivity2 = new Intent(MainActivity.this, EspaniaClass.class);
startActivity(toActivity2);
break;
default:
break;
}
}
private List<Countries> myCountries = new ArrayList<Countries>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateCountryList();
populateListView();
}
private void populateCountryList() {
myCountries.add(new Countries("European Union", 2014, R.drawable.union_europea, "Europe"));
myCountries.add(new Countries("Spain", 2015, R.drawable.espania, "Europe"));
myCountries.add(new Countries("Finland", 2016, R.drawable.finlandia, "Europe"));
myCountries.add(new Countries("France ", 2017, R.drawable.francia, "Europe"));
myCountries.add(new Countries("Ireland ", 2018, R.drawable.irlanda, "Europe"));
myCountries.add(new Countries("Italy", 2014, R.drawable.italia, "Europe"));
myCountries.add(new Countries("Monaco ", 2014, R.drawable.monaco, "Europe"));
myCountries.add(new Countries("Portugal", 2014, R.drawable.portugal, "Europe"));
myCountries.add(new Countries("Russia", 2014, R.drawable.rusia, "Europe"));
myCountries.add(new Countries("Malta", 2014, R.drawable.malta, "Europe"));
}
private void populateListView() {
ArrayAdapter<Countries> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.countryList);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Countries>{
public MyListAdapter(){
super(MainActivity.this, R.layout.item_view, myCountries);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View itemView = convertView;
// make sure we have a view to work with
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
// find country
Countries currentCountry = myCountries.get(position);
// fill the view
ImageView imageView = (ImageView) itemView.findViewById(R.id.item_icon);
imageView.setImageResource(currentCountry.getIconID());
TextView countryText = (TextView) itemView.findViewById(R.id.countryName);
countryText.setText(currentCountry.getCountry());
TextView yearText = (TextView) itemView.findViewById(R.id.year);
yearText.setText("" + currentCountry.getYear());
TextView continentText = (TextView) itemView.findViewById(R.id.continent);
continentText.setText(currentCountry.getContinent());
return itemView;
}
}
}
Upvotes: 1
Views: 1783
Reputation: 34360
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, YourActivity.class);
startActivity(intent);
}
});
Upvotes: 0
Reputation: 368
enter code herepublic 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 ...]
}
Upvotes: 2
Reputation: 420
public class CurrentClass extends Activity {
.
.
.
.
.
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
// put a switch statement if you want start different activities for each corresponding list item
//Start other activities
Intent toActivity = new Intent(CurrentClass.this,
ToNewActivity.class)
startActivity(toActivity);
}
// If you would like to pass some data along to the new activity..... 'SomePojoClass' must implement Serializable
//Seek and store Data Object
SomePojoClass somePojoClass = (SomePojoClass) listView
.getItemAtPosition(position);
// you can also pass some object data or just a string data by using 'putExtra', just like a key-value pair
Intent toActivity = new Intent(CurrentClass.this,
ToNewActivity.class).putExtra("passingObjectData",
somePojoClass);
startActivity(toActivity);
}
.
.
.
.
.
}
Upvotes: 2
Reputation: 5011
Another way would be to add an OnClickListener to the itemView itself:
// this code goes inside your getView() method
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, YourActivity.class);
startActivity(intent);
}
});
return itemView;
Upvotes: 2
Reputation: 72533
Set an OnItemClickListener
to your ListView
.
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext, "You've clicked item nr. " + position, Toast.LENGTH_SHORT).show();
// start an activity depending on the item/position etc.
}
});
Upvotes: 3