Jithin Jithin - 2 months ago 13
Android Question

Android listview image cant change?

I'm having a listview in my Android application. the listview and the listview items are in two xml files. I want to change the listview image by clicking on it.

My Mainactivity Code is..

public class MainActivity extends AppCompatActivity {
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String[] itemname = {
"Song 1",
"Song 3",
"Song 4",
"Song 5",
"Song 6",
"Song 7",
"Song 8",
"Song 9"
};
ImageView img = (ImageView)findViewById(R.id.download);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//CODE on click
Bitmap bImage = BitmapFactory.decodeResource(getResources(), R.drawable.tick);
img.setImageBitmap(bImage);
}
});

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

list.setAdapter(new ArrayAdapter<String>(
this, R.layout.list_view_items,
R.id.Itemname, itemname));

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView aa=(TextView)findViewById(R.id.Itemname);
String ss=aa.getText().toString();

Toast.makeText(MainActivity.this, "Clicked "+ss, Toast.LENGTH_SHORT).show();
}
});
}

}


activity_main xml code..

<?xml version="1.0" encoding="utf-8"?>

<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:padding="0dp"
tools:context=".MainActivity">



<ListView
android:layout_width="match_parent"
android:layout_height="350dp"
android:id="@+id/list"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"

android:layout_alignParentBottom="true"
android:layout_alignParentTop="true">
</ListView>

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/imageView2"

android:layout_marginLeft="32dp"
android:layout_marginStart="32dp" />


</RelativeLayout>


listview_items xml..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_gravity="center"
android:text="asdas"
android:layout_weight="0.9"
android:paddingTop="5dp"/>

<ImageView
android:id="@+id/download"

android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="right"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/download" />

</LinearLayout>


The logcat is....

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jithin.list_image_change/com.example.jithin.list_image_change.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)


Any one help me to find a solltion for this thank you....

Answer

You are referring wrong image id in your main activity:

    ImageView img = (ImageView)findViewById(R.id.download);

it should be:

    ImageView img = (ImageView)findViewById(R.id.imageView2);

the one you have define in activity_main.xml

And to change your imageview in your adapter you have to use Custom Adapter class which will extend the Base Adapter.

For reference: https://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html

Comments