0

In my listView I have 3 row types, TXT, IMG, SMS. Each one has different row layout and works fine like this:

... extends BaseAdapter ...

public View getView(int position, View convertView, ViewGroup parent) {

    TextViewHolder textViewHolder = null;
    ImageHolder imageHolder = null;
    SmsHolder smsHolder = null;
    //PlaceHolder placeHolder = null;

    ConversionModel conversion = getItem(position);
    int type = conversion.type;

    if (convertView == null) {
         if(type == ConversionModel.TXT) {
            textViewHolder = new TextViewHolder();

            convertView = mInflater.inflate(R.layout.convers_txt,  null);
            textViewHolder.textView = (TextView)convertView.findViewById(R.id.row_txt);

            convertView.setTag(textViewHolder);
        }

      if(type == ConversionModel.IMG) {
            imageHolder = new ImageHolder();

            convertView = mInflater.inflate(R.layout.convers_img,  null);
            imageHolder.img = (ImageHolder)convertView.findViewById(R.id.row_txt);

            convertView.setTag(textViewHolder);
        }
       ....
    }

   ...

  }

My purpose is this:

-alllist-----------
----txt------------
----img------------
-----(multipledata) //placeholder must has dynamic rows in it
--------button-----
--------button-----
---sms-------------
.....

My placeHolder must has dynamic rows in it because I don't know the numbers of data that comes from server. I tried to do it nested listview but it only shows first data in the list.

Also I put a LinearLayout to placeHolder and added items like this(pseudo):

for data in datas {
   Button btn = new Button(ctx)
   ...
   placeHolder.layout.addView(btn);
} 

But whenever I scroll the listView, getView method is messing and it's rendering same buttons almost 10-20 times, when it should be 2-3.

How can I achieve this? I looked at app sources like Telegram but couldn't found similar approaches.

Solution:

String arr[] = conversion.message.split(Pattern.quote("$$"));

 LinearLayout layHolder = new LinearLayout(ctx);

 for (int i = 0; i < arr.length; i++) {
      Button test_btn = new Button(ctx);
      //PlaceModel pm = new PlaceModel(arr[i]);
      layHolder.addView(test_btn);
 }

  placeHolder.linearLay.removeAllViews(); //<- THIS
  placeHolder.linearLay.addView(layHolder);
1
  • Add linear layout below main content(image or text), and add buttons in that linear layout. Commented Apr 29, 2015 at 8:27

2 Answers 2

1

Add a linear layout in your child layout like this:(for demo I have added one button in odd child and two buttons in even child position)

    ...    
    <!--main linear layout contnet-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <LinearLayout
        android:id="@+id/layout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

and add your buttons in it:

in getView()

 ...        
        LinearLayout layout = (LinearLayout)   view.findViewById(R.id.layout);

   //important line
   layout.removeAllViews();

   if (position % 2 == 0)
        {
            Button b1 = new Button(_context);
            b1.setText("B1");
            layout.addView(b1);
        }
        else
        {
            Button b1 = new Button(_context);
            b1.setText("B1");
            layout.addView(b1);
            Button b2 = new Button(_context);
            b2.setText("B2");
            layout.addView(b2);
        }
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this solution. Instead of 2-3, buttons added 10-20 times when I scroll down. As I said getView method is messing with this solution.
Sorry I was trying expandable list, but I updated for listview and it adds buttons properly in view. If you wish I can post screenshot.
0

Implement two methods in your adapter

 public int getItemViewType(position){
     int type = getItem(position).get type();
     return type ==  ConversionModel.TXT ? 0 : type ==  ConversionModel.IMG ? 1 : 2;
 }

and

public int getViewTypeCount(){
       return 3;
  }

will solve your problem

3 Comments

I'm already checking type. Anyway now I'm trying to switch ExpandableListView.
You are checking view type in your getView(), adapter is responsible to inflate views, thats why you are facing problem once you are adding child views inside any view type. These methods tells adapter that you are using multiple views.
As I said I already check "type" and your solution didn't helped. But thanks Haris.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.