I am trying to Develop a custom List menu with a unique Icon on the left a title and a small description underneath the title. I tried a few variations but none of them seem to work.
Here is what I did
item_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:paddingLeft="10dp"
android:paddingBottom="15dp" >
<ImageView
android:id="@+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/union_europea" />
<TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/item_icon"
android:layout_toRightOf="@+id/item_icon"
android:text="Small Text"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/countryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/year"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textSize="18dp"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/continent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:paddingRight="10dp"
android:textSize="12dp"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
then I did the main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/countryList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
then I did the Countries.java
public class Countries {
private String country;
private int year;
private int iconID;
private String continent;
public Countries (String country, int year, int iconID, String continent){
super();
this.country = country;
this.year = year;
this.iconID = iconID;
this.continent = continent;
}
public String getCountry() {
return country;
}
public int getYear() {
return year;
}
public int getIconID() {
return iconID;
}
public String getContinent() {
return continent;
}
}
then I did the MainActivity.java
public class MainActivity extends Activity {
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;
}
}
}
I have a listView with icon on the left, a title, and a description .
My question is how do I make it so when a user clicks on 1 of the list items and opens an individual activity that corresponds to that list item. ????