views:

36

answers:

2

Hi,

I'm very new to Android delelopment, searching and reading various tutorials and using different parts of code from around the internet, trying to make sense of it as I go. Forgive me if my explanation isn't too great.

Currently I have a list created with 4 list items, what I'd like to do is assign a different icon to each of these list items, however, I've no idea how to do it, nor do I know what to search for.

Here's the code:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mainListView">
</ListView>

</LinearLayout>

row.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="16sp" >
 </TextView>

FirstList.java

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FirstList extends Activity {

private ListView mainListView;
private ArrayAdapter<String> myListAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mainListView = (ListView) findViewById( R.id.mainListView);

    String[] myList = new String[] {"List 1", "List 2", "List 3", "List 4"};

    ArrayList<String> listObjects = new ArrayList<String>();
    listObjects.addAll( Arrays.asList(myList));

    myListAdapter = new ArrayAdapter<String>(this, R.layout.row, listObjects);

    mainListView.setAdapter(myListAdapter);

    }
}

I'd appreciate any input you have, including any resources that you may recommend or critique of my code, as a newbie I'd rather not pick up bad habits from the start!

Thanks.

A: 

You'll have to add an ImageView to the row.xml file (the images will be rendered in the ImageView) - this shouldn't be too hard.

You will also have to create a custom ArrayAdapter (extending the ArrayAdapter class) and override the getView method - quite a common task, so you should easily find some examples on how this should be done. You can also have a look at the original ArrayAdapter class (ArrayAdapter) to see how the getView is implemented. At last you should add the image loading code in the getView method.

humus
Thanks, could you elaborate please? My searches are coming up with the same recycled internet pages for various sites.
mattFllr
A: 

One solution is to use a SimpleAdapter. The SimpleAdapter allows you to map static data to complex views. You should be able to find a number of examples with a quick search, for example a tutorial.

Mayra
That gave me exactly what I needed to go on from a search perspective and I think I may of cracked it. Thank you.
mattFllr