Saty
Saty

Reputation: 2621

Adding multiple views to row of ListView dynamically

I am trying to make a chat client where my user will be able to send text, image, video, music file etc.

I know how to add dynamically row for list, however it just adds the String to the ArrayList and reflect on the list through ArrayAdapter. For me it's unpredictable, user can add String, video, image or music file. How would I able to add them?

Upvotes: 1

Views: 719

Answers (2)

George Aykara
George Aykara

Reputation: 1

First create an activity so you can get both Java and XML file. To this XML file just drag drop a listview. We wont make any dyamicity here.

Then you will need to design your XML file for a single row. For your purpose you can create a new XML layout and drag drop textview, imageview, videoview etc. It is better to make it as Linear Layout.

Create another java class. This is for adding customising items to the list. Add elements based on whether the the elements contain some value or not.

On the activity's java file, you will set adapter linking the designed row with the listview and our java file for custom list.

Visit the below link. It will give you all the necessary codes. I hope it will help you.

http://www.androidhub4you.com/2013/09/dynamic-list-view-demo-in-android.html

This worked really awesome for me. Now you may also find problem like, videoview or imageview occupying space even when you have nothing to add into it. I recommend deleting the view you don't need for each row. you can add delete view code in the customising java file. Use below code for deleting view.

((ViewGroup) image_view_name.getParent()).removeView(image_view_name);

Here image_view_name is is the object for ImageView, VideoView or any other as you wish.

Upvotes: 0

Shivam Verma
Shivam Verma

Reputation: 8023

  1. Make different layouts for your Audio listitem view, Video listitem view, String listitem View.
  2. Inside getView() method of your adapter check the type of content you want to display. In case its audio, inflate the audio view from the xml, add corresponding content and return the correct inflated view.

The key is to be able to find out the type of content inside the getView() method and inflate a different view accordingly.

Upvotes: 3

Related Questions