-1

I need to generate some ImageView inside the getView method.

I will generate them inside this layout :

    <LinearLayout
        android:id="@+id/layoutBaby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="horizontal" >

So the images will be added horizontally automatically (because the orientation).

This is how i add those images :

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    for(int childId : dbHelper.getChildrenIdBySchedule(getItem(position).getId()))
    {
        ImageView imgBaby = new ImageView(context);
        imgBaby.setLayoutParams(params);

        String image = dbHelper.getChildImage(childId);
        File imgFile = new File(image);

        if(imgFile.exists())
        {
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            imgBaby.setImageBitmap(myBitmap);
        }
        else
        {
            imgBaby.setImageResource(R.drawable.ic_launcher);
        }
        holder.layoutBaby.addView(imgBaby);
    }

I have debug the code above to make sure its called multiple times (based on dbHelper.getChildrenIdBySchedule).

This code working if theres only 1 ImageView, however if there is more than 1 ImageView, only the lastest added ImageView is vissible.

For example, if theres 2 ImageViews, although both of them has been added (i debug the code), only the 2nd ImageView is visible.

Feel free to ask me anything, and Thanks for your time.

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • why are adding ImgView at runTime ?? can't you design the wow for a list ? – SweetWisher ツ Nov 10 '14 at 05:55
  • @SweetWisherツ i cant specify the amount of images in each row... – Blaze Tama Nov 10 '14 at 05:56
  • @SweetWisherツ i mean the total imageview in each row will be different – Blaze Tama Nov 10 '14 at 05:57
  • [Check this if it helps](http://stackoverflow.com/a/12616146/2591002) only difference is: `holder.layoutBaby.addView(imgBaby,params);` – SweetWisher ツ Nov 10 '14 at 06:01
  • @SweetWisherツ Thanks. Im doing like it, but with a holder. I think the problem is because im doing it in the getView – Blaze Tama Nov 10 '14 at 06:02
  • [Then this one is perfect for you :)](http://stackoverflow.com/a/17364421/2591002) – SweetWisher ツ Nov 10 '14 at 06:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64607/discussion-between-sweetwisher--and-blaze-tama). – SweetWisher ツ Nov 10 '14 at 06:03
  • @BlazeTama : u can use a [`HorizontalListView`](https://github.com/dinocore1/DevsmartLib-Android) instead of `LinearLayout` – Kaushik Nov 10 '14 at 06:08
  • @BlazeTama Just now tried your code, its working fine. I guess imageview size is too big, change `params` to some fix value. P.S. I don't prefer adding view in holder as it may not be as expected because of view recycling. – Pr38y Nov 10 '14 at 06:19
  • @Pr38y Thanks, i will try. Do you have any better suggestion? – Blaze Tama Nov 10 '14 at 06:21
  • @Pr38y YES ITS WORKING!!, but what should i do to make the imageview's size NOT static? – Blaze Tama Nov 10 '14 at 06:23
  • then use `HorizontalListView` instead of `LinearLayout` as suggested by @kaushik , to get enough space. or wrap `linearLayout` in `HorizontalScrollView`. – Pr38y Nov 10 '14 at 06:41
  • Approach may differ based on max number of items needed in listview and max number of items needed to be add dynamically per row. – Pr38y Nov 10 '14 at 06:45
  • @Pr38y Thanks a lot, you can answer my question (just with your comments), i will upvote+accept it – Blaze Tama Nov 10 '14 at 06:52

1 Answers1

1

You can manage it with HorizontalScrollView :

public View getView(int position, View view, ViewGroup viewgroup) {
        ViewHolder holder = new ViewHolder(); // our view holder of the row
        if (view == null) {

            HorizontalScrollView hr = new HorizontalScrollView(con);
            LinearLayout layout = new LinearLayout(con);
            layout.setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));



           for(int childId : dbHelper.getChildrenIdBySchedule(getItem(position).getId()))
                holder.image = new ImageView(con);
                layout.addView(holder.image);
                String image = dbHelper.getChildImage(childId);
                File imgFile = new File(image);

               if(imgFile.exists())
               {
                    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                    holder.image.setImageBitmap(myBitmap);
               }
               else
                    holder.image.setImageResource(R.drawable.ic_launcher);
            }
            hr.addView(layout);
            view = hr;

            view.setTag(holder);

        }
        holder = (ViewHolder) view.getTag();

        return view;

    }

For more info, refer this

Community
  • 1
  • 1
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74