user3695970
user3695970

Reputation: 51

android: listview use custom images

here is my code i have png in my drawable folder drinkcan bike hat shoes mAdapter.addItem(R.drawable.drinkcan, "card", "1");

can someone help me figure out how i can add custom images with differnt list items for example.

mAdapter.addItem(R.drawable.drinkcan, "drinkcan", "1");
mAdapter.addItem(R.drawable.shoes, "shoes", "1");
mAdapter.addItem(R.drawable.hat, "hat", "1");

i also have this in my main xml

<LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip"

            android:src="@drawable/ic_launcher" />
    </LinearLayout>

this image gets added on all the items ? plz help me add my custom icons.

Upvotes: 1

Views: 90

Answers (3)

Agapitz
Agapitz

Reputation: 56

i think it's similar to the project that I've made I'm changing the item fields color depends on their status to do that i created a custom adapter and filtered all data one by one before displaying in that case i can easily set field color depends on their status.

Upvotes: 0

Ankit Khare
Ankit Khare

Reputation: 1385

For this to happen do the following

public class SingleItem {

    private int drawableId;
    private String name;
    private String no;

    public int getDrawableId() {
        return drawableId;
    }

    public void setDrawableId(int drawableId) {
        this.drawableId = drawableId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }
}

Now create a method to add items to this Like

 @Override
        protected void onCreate(Bundle savedInstanceState) {
//OnCreate of Activity you Created
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login_screen);

    ListView listView = (ListView) findViewById(R.id.list);
    Adapter adapter = new Adapter(getAllItems);
    listView.setAdapter();
        }
private List<SingleItem> getAllItems(){
        List<SingleItem> allItems  = new ArrayList<>();
        SingleItem singleItem = new SingleItem();
        singleItem.setName("hat");
        singleItem.setDrawableId(R.drawable.test);
        singleItem.setNo("1");
        allItems.add(singleItem);
        return allItems;
    }

And finally use Base Adapter to set This allItems to ListView. Like this

class Adapter extends BaseAdapter {

        private List<SingleItem> allSingleItems;

        public Adapter(List<SingleItem> items) {
            allSingleItems = items;
        }

        @Override
        public int getCount() {
            return (allSingleItems==null)?0:allSingleItems.size();
        }

        @Override
        public SingleItem getItem(int i) {
            return allSingleItems.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            //Set data in Views here
            return null;
        }
    }

Hope this helps.

Upvotes: 1

SAM
SAM

Reputation: 399

you do this by creating custom adapter check the below link may it will be helpful to you

http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/

Upvotes: 0

Related Questions