anaxin
anaxin

Reputation: 710

ImageView in ListView gives extra image

I'm getting an extra icon while adding ImageView in a ListView as shown below. What is this and how can I remove it? enter image description here

In my content it also shows up and when selected it highlights the ImageView shown here enter image description here

The xml code is here:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="anaxin.newshellas.Feed"
    tools:showIn="@layout/activity_feed">

    <ListView
        android:choiceMode="singleChoice"
        android:listSelector="@android:color/darker_gray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/feedView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:clickable="false" />
    <TextView
        android:id="@+id/textTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        />
    <TextView
        android:id="@+id/textBot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:layout_below="@+id/textTop"
        />
    <ImageView
        android:layout_below="@+id/textTop"
        android:layout_alignParentRight="true"
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image"
       />
</RelativeLayout>

I use a SimpleAdapter with two rows (textTop, TextBot) to populate my listView like this:

final SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.content_feed,
                new String[]{"First Line", "Second Line"},
                new int[]{R.id.textTop, R.id.textBot}) {
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                return view;
            }
        };

And in method to add items this part

Map<String, String> datum = new HashMap<>(2);
            datum.put("First Line", article.getTop());
            datum.put("Second Line", article.getBot());
            data.add(datum);

Upvotes: 1

Views: 83

Answers (3)

Shree Krishna
Shree Krishna

Reputation: 8562

First of all Don't mix up ListView and other elements like TextView and ImageView which should be in the single item layout. Because of it you are seeing multiple images in single list.

And another thing to be noticed is your android:layout_below="@+id/textTop" attribute is conflicted in ImageView and TextView as well, so I suggest you to fix that as well.

Rest of the codes are fine enough !!

Upvotes: 1

Helmi
Helmi

Reputation: 90

remove all content in your list view like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="anaxin.newshellas.Feed"
tools:showIn="@layout/activity_feed">
<ListView
    android:choiceMode="singleChoice"
    android:listSelector="@android:color/darker_gray"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/feedView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:clickable="false" />

you don't need them

Upvotes: 0

Deepak Sharma
Deepak Sharma

Reputation: 417

You have an extra ImageView and 2 extra TextViews in your layout file. These should be in the layout file of your list adapter and not in the layout containing listview itself. Try removing these extra views, that might solve your problem

Upvotes: 1

Related Questions