TwoThumbSticks
TwoThumbSticks

Reputation: 1136

How add different Icons per row inside a ListView

I am trying this cool library that uses Ken Burn effect here (his github page) I got it to work but I cant add different Icons on different rows. I tried to use Mkyongs CustomAdaptor example but what he used is a layout with TextView and ImageView. What I am using is ListView. I am already able to change the text font and size of the ListView but lost trying to add icons. How do you do this?

Below is my Layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@drawable/blur4"
    tools:context="com.gio.hia.hiarewired.NoBoringActionBarActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent" />

    <FrameLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="@dimen/header_height">

        <com.gio.hia.hiarewired.KenBurnsView
            android:id="@+id/header_picture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/picture0" />

        <ImageView
            android:id="@+id/header_logo"
            android:layout_width="@dimen/header_logo_size"
            android:layout_height="@dimen/header_logo_size"
            android:layout_gravity="center"
            android:src="@drawable/ic_header_logo" />

    </FrameLayout>

</FrameLayout>

This is my Activity Class

private void setupListView() {
    ArrayList<String> FAKES = new ArrayList<String>();
    //for (int i = 0; i < 1000; i++) {
        FAKES.add("Row 1");
    FAKES.add("Row 2");
    FAKES.add("Row 3");
    FAKES.add("Row 4");
    //}
    mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
    mListView.addHeaderView(mPlaceHolderView);
    //mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FAKES));
    mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_text_config, FAKES));
    mListView.setOnScrollListener(new AbsListView.OnScrollListener() { ...

I used a custom R.layout.list_text_config to change the font

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:paddingTop="10dip"
    android:paddingBottom="10dip"
    android:paddingLeft="15dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="35dp"
    android:textColor="@color/colorWhite"/>

In his example he used a layout inflater How do I use my Custom Adapter with a layout inflator or am I looking at the problem on the wrong direction?
I usually just do this

CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
    list=(ListView)findViewById(R.id.list);
    list.setAdapter(adapter);

but how do I do that when he used a layout inflater like below

mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
    mListView.addHeaderView(mPlaceHolderView);
    mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FAKES));

Upvotes: 1

Views: 115

Answers (1)

royjavelosa
royjavelosa

Reputation: 2068

Like @Arslan said this can be done with a custom adapter. You are actually doing it correct

CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
    list=(ListView)findViewById(R.id.list);
    list.setAdapter(adapter);

That actually creates the list with the icons. What you need to add to that is the header image to the mPlaceHolderView but instead of using mListView use the list that you created with your "custom adapter". I placed the modified line of code from the original line of code so you can easily see the difference.

    CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
    list=(ListView)findViewById(R.id.list);
    mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, list, false);        
    //mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);      
    list.setAdapter(adapter);

Now and since your no longer using the mListView you should probably lookout for instances of mListView and replace it with your list

Upvotes: 1

Related Questions