-2

So what I want to do is have a different image for each list item in my App. I've been attempting to do this for a little while now and have no idea what I'm doing.

Here's what I'm trying to do:

How do I go about this? Here's my current code:

package net.androidbootcamp.gamesandcoffee;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[ ] attraction = {"Games", "Coffee Shops"};
        setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_main, R.id.games, attraction));

    }
    protected void onListItemClick(ListView l, View v, int position, long id) {
        switch (position) {
            case 0:
                startActivity(new Intent(MainActivity.this, games.class));
                break;
            case 1:
                startActivity(new Intent(MainActivity.this, coffee.class));
                break;
        }
    }
}

and my 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/games"
        android:textSize="20sp"
        android:text="@+id/games"
        android:drawableLeft="@drawable/games"/>
</RelativeLayout>
Community
  • 1
  • 1
Chris S
  • 21
  • 7
  • 1
    You have to create a Custom adapter for your Listview. You can google it for further details An example is here, http://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html – Jithin Sunny Dec 01 '15 at 09:14
  • @Sunny I'll take a look here, thanks for the link. – Chris S Dec 01 '15 at 09:22

3 Answers3

2

For that you have to create a custom adapter, where you can customize all the ListView items.

public class ListAdapter extends ArrayAdapter<Item> {

    public ListAdapter(Context context, int imageViewResourceId) {
        super(context, imageViewResourceId);
    }

    public ListAdapter(Context context, int resource, List<Item> items) {
        super(context, resource, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.itemlistrow, null);
        }

        Item p = getItem(position);

        if (p != null) {
            ImageView iv = (ImageView ) v.findViewById(R.id.id);

            if (iv != null) {
                iv .setImageResource(R.id.xyz);
            }
        }

        return v;
    }
}
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
  • 1
    This uh. Seems pretty complicated. Is it the only way to go about this? I'm still rather new to this so I'm not quite understanding everything going on here – Chris S Dec 01 '15 at 09:19
  • Please refer this link for more details http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view – Daud Arfin Dec 01 '15 at 09:22
  • Thank you, I will look at this. – Chris S Dec 01 '15 at 09:23
  • Yes, this looks complicated, but oddly enough it is probably the leanest way to do it. I have done this quite some times and still don't understand why android makes it so complicated – schubakah Dec 01 '15 at 10:17
0

load the image resource id's to the array and pass to the list adapter in this way

int[] images = {R.drawables.firstimg,R.drawables.secondimg};
setListAdapter(new CustomAdapter(this,images, attraction));

Crate CustomAdapter Class

public class CustomAdapter extends BaseAdapter{

private Context context;
int[] images;
String[] textdata;

public CustomAdapter(Context context, int[] images,String[] textdata) {
    this.context = context;
    this.images=images;
    this.textdata=textdata;
}

@Override
public Object getItem(int position) {
    return textdata.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = null;
    if (convertView == null){
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.drawer_listitem,parent,false);
    }else {
        row = convertView;
    }

    TextView name = (TextView) row.findViewById(R.id.draweritemtext);

    ImageView image = (ImageView) row.findViewById(R.id.draweritemimage);

    name.setText(textdata[position]);

    image.setImageResource(images[position]);
    return row;
}

@Override
public int getCount() {
    return textdata.size();
}

}

dileep
  • 347
  • 4
  • 16
0

Following these two video tutorials, geared towards beginners, I was able to arrive to my desired result. Thank you those who contributed, but none of the answers or resources provided were geared towards beginners that lack experience.

These are the videos I watched

Android Studio Tutorial - 18 - ListView with Custom Adapter part-1

Android Studio Tutorial - 19 - ListView with Custom Adapter part-2

Chris S
  • 21
  • 7