Yryskul Turduev
Yryskul Turduev

Reputation: 307

Why image in Listview is set double times?

Screenshot of Listview link. I was making a menu display, and wanted to set images into my listview. However, images are set double times. I do not know why? My getView function in MenuAdapter class:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder;
    if(convertView == null) {
        holder = new Holder();
        convertView = inflater.inflate(R.layout.menu_lists, null);
        holder.tv=(TextView) convertView.findViewById(R.id.music_txt);
        holder.imageView=(ImageView) convertView.findViewById(R.id.music_icon);
        convertView.setTag(holder);
    } else {
        holder = (Holder) convertView.getTag();
    }

    holder.tv.setText(result[position]);
    holder.tv.setTextColor(Color.RED);

    holder.imageView.setImageResource(image[position]);


    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
        }
    });
    return convertView;
}

This is layout.xml for my listview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:background="#aaef79"
        android:id="@+id/music">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/music_icon"
            android:background="@mipmap/ic_launcher"
            android:layout_weight="3"
            android:layout_gravity="center" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Music"
            android:id="@+id/music_txt"
            android:layout_weight="1"
            android:gravity="fill_vertical|center_horizontal" />

    </LinearLayout>

</LinearLayout>

Upvotes: 1

Views: 79

Answers (1)

MHP
MHP

Reputation: 2731

because you set android:background="@mipmap/ic_launcher" in XML and set setImageResource in adapter

remove android:background="@mipmap/ic_launcher in XML or change it to android:src="@mipmap/ic_launcher

actually each ImageView can take both background and src in xml. setImageResource means that you programmatically set src for your view and setBackgroundResource means that you programmatically set background for your view

Upvotes: 3

Related Questions