1

So i have this listview with my custom XML file that i made for it.

In the file, there is an image with no source set that has a fixed id.

I want to change this image source to one of my resources in drawable folder.

Here is my XML:

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

<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView 
 android:id="@+id/listImage"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:contentDescription="protection image"
 android:layout_marginTop="10dp"
/>
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
>
<TextView android:id="@+id/text1" 
android:textSize="20dp"  
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/text2"
android:textSize="12dp" 
android:layout_marginTop="5dp"
android:layout_width="wrap_content" 
android:layout_height="fill_parent"/>
</LinearLayout>
</LinearLayout>

</LinearLayout>

This is what i have tried so far, and it crashes:

        adapter=new SimpleAdapter(this, listItems, R.layout.custom_row_view,new String[]{"name", "current"},  new int[] {R.id.text1, R.id.text2});
    ImageView img;
    img = (ImageView) findViewById(R.id.listImage);
    Drawable d = getResources().getDrawable(R.drawable.opened);
    img.setImageDrawable(d);
    setListAdapter(adapter);
4
  • do u want to set same images all list items or different images for each list item ? Commented May 10, 2012 at 13:47
  • I have two images, and i need to decide which items gets what image Commented May 10, 2012 at 13:48
  • what is your condition to decide the image for list item? Commented May 10, 2012 at 13:50
  • its not here, the condition is a simple if statement. i haven't done that yet because i want to see if setting the image even works! Commented May 10, 2012 at 13:51

1 Answer 1

1
ImageView img;
    img = (ImageView) findViewById(R.id.listImage);

This means that you are referring to a object with is present in the layout which you could have used for your Activity's setContentView().But your ImageView is not present in that layout.

And here you are trying to refer this with a ListView. So obviously you are getting the null pointer exception.

You have to make use of a Custom Adapter as well as. Try the one from the below link.

http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter

http://www.mkyong.com/android/android-listview-example/

Sign up to request clarification or add additional context in comments.

8 Comments

Im not sure i understand these examples.. Isn't there any simple way just to change the image if i have the id?
No. Not possible with a Custom Listview. You have to make use of Custom Adapter only.
Well, but my listview has two lines. is it possible to adapt this too?
Yes, with custom listview anything is possible. Give it a try. I can't be done this simple.
But i dont know how to do this with two lines. any examples?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.