Adding text and images to a ListView?

I would like to add an image and a text to every list item of a ListView. Let me show you my code:

ArrayAdapter<String> arrayadapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayListString);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener){
@override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
...
}
}

How can I solve this?

Thank you for your patience and consideration.

Upvotes: 0

Views: 52

Answers (1)

Dinorah Tovar
Dinorah Tovar

Reputation: 488

Please take in consideration to use RecyclerView, to use recyclerview you need to have a list of your images (or URL's) and a list of your text's, or even better an object that contains both.

On your activity or fragment

RecyclerViewAdapter adapter = new RecyclerViewAdapter();
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);

In which adapter is a RecyclerviewAdapter and RecyclerViewHolder 'cause the method you want to do is only for Strings, without a custom view. Please check this official documentation: https://developer.android.com/guide/topics/ui/layout/recyclerview

Upvotes: 2

Related Questions