1

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?

2 Answers 2

3
  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.

Sign up to request clarification or add additional context in comments.

3 Comments

You need to implement your own custom list view adapter and follow the suggestions provided by Shivam Verma.
okay @Shivam...I was thinking of this procedure too..I asked because I thought there may be any other simple procedure to it. There is another question left which I mentioned earlier too. How would I able to add elements dynamically to this custom adapter?
Add new items to the ArrayList that you use to provide data to the custom adapter and then call adapter.notifyDataSetChanged(). Your views will be added to the listview.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.