0

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. ????

loxxy
  • 12,990
  • 2
  • 25
  • 56
  • custom list view in android , Google will return lots of result for this search. so first try to search in google. – Chintan Khetiya Dec 26 '13 at 04:03
  • 1
    Check out [OnItemClickListener](http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html) – takendarkk Dec 26 '13 at 04:04
  • possible duplicate of [Custom ListView?](http://stackoverflow.com/questions/5475389/custom-listview) – Chintan Khetiya Dec 26 '13 at 04:05
  • I looked and searched on google. But all the tutorials teach is to make a Toast message on click. I dont need to Toast anything. I want each list item to open an activity corresponds to the list item button – Pultulintool Dec 26 '13 at 04:16
  • @Pultulintool instead of toast start a new activity with intent. – Raghunandan Dec 26 '13 at 04:19
  • I understand but how do i do that? I am new to Android and I dont know how to start intent activity that will affect each list item. Can someone please explain to me how can I add this intent method into my existing code? Thank you – Pultulintool Dec 26 '13 at 04:21
  • @Pultulintool then you need to read the docs get to know the basics instead of asking this question here. Download the samples lots of samples from the sdk and try it out. – Raghunandan Dec 26 '13 at 04:27
  • @Pultulintool I think the only problem you are facing to start activity is context or anything else. – Bharat Sharma Dec 26 '13 at 04:41

2 Answers2

0

I think you need some modification in your class. There can be several way but I think this one will fine for you.

Change your MyListAdapter to below code.

    public MyListAdapter(Context context, List<Countries> country_list, ListView list_view) {
        super(MainActivity.this, R.layout.item_view, myCountries);

        if (list_view != null) {
            list_view.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {

                    // YOU CAN START YOUR ACTIVITY HERE.
                    Intent my_activity_intent = new Intent(context, my_activity.class);
                    startActivity(my_activity_intent);
                }
            });
        }
    }

Change

private void populateListView() {
    ArrayAdapter<Countries>  adapter = new MyListAdapter(this, myCountries, list);
    ListView list = (ListView) findViewById(R.id.countryList);
    list.setAdapter(adapter);
}

you can create "setOnItemClickListener" in your main activity also. Only change will be while creating activity "Intent my_activity_intent = new Intent(MainActivity.this, my_activity.class);" need to do for activity intent.

If you have any doubt you can ask. Its all about basics and your programming style. Try to learn such things. :)

Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29
0

my solution is only applicable if you are having limited number of list items.

list_view.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                         switch(arg2)
                         {
                           case 0:
                                startActivity(new Intent(context,Activity0.class));
                                break;
                            case 1:
                                startActivity(new Intent(context,Activity1.class));
                                break;
                           //Implement cases for all list items

                         } 



                }
            });

And if you are having more number items then another possible solution is to pass all the data of selected item i.e image,text,description and display it in next activity.

You can ask if you have any further problems :)

Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27
  • I'm sorry for such a silly question but, where exactly in my code to put this code??? If you can please tell me where ti put the code in my MainActivity class. Also in the beginning of this code where it says list_view do i have to change it ??? – Pultulintool Dec 26 '13 at 13:25
  • @Pultulintool : you need to put this code in oncreate method of your MainActivity – Jigar Pandya Dec 27 '13 at 05:45