1

I am using a list view to show some data on it and using it with a custom adapter, now at an instance of time, I want to show an image view as a list item in it. For that case I have made dynamic linear layout and added the image view in it. Now I want that on some condition this image view layout should appear as the list item in the last of the list view as a new item added to the list.

Genarating layout dynamically

LinearLayout linearLayout = new LinearLayout(UserChatActivity.this);

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                     LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            ImageView img = new ImageView(UserChatActivity.this);
            img.setImageBitmap(BitmapFactory.decodeFile(picturePath));


            linearLayout.addView(img, layoutParams);


            mList.addView(linearLayout);

But it is giving an error named : - Views can't be added directly in the adapterViews. I know that we have to inflate views for the adapter but how to add the new view with the previous data coming in the list.???

Thanks

Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143

4 Answers4

4

I think MergeAdapter perfectly solves your problem.

k_shil
  • 2,108
  • 1
  • 20
  • 25
2

you can try this, put the code in to getView() in your adapter.

if (view == null) 
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = vi.inflate(R.layout.list, null);
                    **// list.xlm is your layout**
} 

**lnHolder is in list.xml**
LinearLayout lnHolder = (LinearLayout ) view.findViewById(R.id.lnHolder);

LinearLayout linearLayout = new LinearLayout(UserChatActivity.this);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                         LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);    
ImageView img = new ImageView(UserChatActivity.this);
img.setImageBitmap(BitmapFactory.decodeFile(picturePath));   
linearLayout.addView(img, layoutParams);
 lnHolder.addView(linearLayout);
Talha
  • 12,673
  • 5
  • 49
  • 68
1

Your getView() method will look like this,

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

            if (position == 10) //if you already know the position use static position
                //  else mydatalist.get(position).hasImage()== true and 
                // make one function hasImage in your data structure which checks weather its a image or text 
            {
                convertView = _inflater.inflate(R.layout.simplelinearlayout,
                        null);

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);

                ImageView img = new ImageView(UserChatActivity.this);
                img.setImageBitmap(BitmapFactory.decodeFile(picturePath));

                convertView.addView(img, layoutParams);
            } else {
                if (convertView == null) {
                    convertView = _inflater.inflate(R.layout.missingitem_row,
                            null);
                    viewHolder = new ViewHolder();
                    viewHolder.textTitle = (TextView) convertView
                            .findViewById(R.id.txtTitle);

                    convertView.setTag(viewHolder);

                } else {
                    viewHolder = (ViewHolder) convertView.getTag();
                }

                viewHolder.textTitle.setText(messageText);
            }
            return convertView;
        }
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
-1

notifiyDataSetChange() method is used to update the adapter

listView dynamic add item

Community
  • 1
  • 1
Chuekup
  • 32
  • 2