4

I want to display N number of imageview in every row of list view. Number of imageview depends on json parsing value. it can be 2 or 3 or 4 everytime when i fetch json from server.

so I can not use static xml by using inflate.so i decided to create dynamic view in getview method and add N number of imageview into view

I write following code but it still display only one imageview and horizontal scroll wont work at all..

any help is appreciated

public class MyAdapter extends BaseAdapter{
private LayoutInflater inflater;
private ArrayList<String> data;
Context con;

public MyAdapter(Context context, ArrayList<String> data1){
// Caches the LayoutInflater for quicker use
      inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Sets the events data
data= data1;
con=context;
}

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));

           //layout set some properties
           for(int i=1;i<2;i++)
           {
            holder.image =new ImageView(con);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100*i, 100*i);
            holder.image.setLayoutParams(layoutParams);
            layout.addView(holder.image);
           }
           //subtitle set some properties

       //CREATING THE LAYOUT THROUGH CODE
           hr.addView(layout);

       view = hr; //INSTEAD OF INFLATING A LAYOUT FOR THE ROW I JUST BINDED IT TO THE RECENTLY CREATED LAYOUT 
       //bind the views of the holder to the views of the layout
           view.setTag(holder);
           Log.w("myapp", "new view");
    } 
    else 
    {
       holder = (ViewHolder) view.getTag();
       Log.w("myapp", "in reuse");
    }
        //rest of implementation of the View
    for(int i=0;i<2;i++)
    {
    holder.image.setImageResource(R.drawable.ic_launcher);
    }
        return view;
    }

static class ViewHolder {
    ImageView image;
 TextView title;
 TextView type;
 HorizontalScrollView hr;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 4;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 1;
}
 }
Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77

3 Answers3

6

Check this is what you want I guess. Just set random value for each line in images

    public class MyAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private ArrayList<String> data;
    Context con;

    public MyAdapter(Context context, ArrayList<String> data1) {

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        data = data1;
        con = context;
    }

    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 i = 0; i < data.size(); i++) {
                holder.image = new ImageView(con);
                layout.addView(holder.image);
                holder.image.setImageResource(R.drawable.ic_launcher);
            }
            hr.addView(layout);
            view = hr;

            view.setTag(holder);

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

        return view;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 1;
    }
}

class ViewHolder {
    ImageView image;
    TextView title;
    TextView type;
    HorizontalScrollView hr;
}
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Aarun
  • 564
  • 1
  • 6
  • 13
2
for(int i=1;i<2;i++)

this means it runs only once, for i = 1 because on the next loop i == 2 and therefore is i < 2 false.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
0

Well, the same way you inflate view items from your xml layout, you can create those programmatically, as usual. Just need to make sure to specify the layout parameters and add them to the layout so that they are visible:

For instance, take the views linked on your ViewHolder initialization:

//That's a view taken from the xml
EditText editText = (EditText) view.findViewById(R.id.YOUR_EditText1);

Likewise, if you want to dynamically add your views to your layout, in the getView() method:

ImageView image1 = new ImageView();
...
...
view.addView(image1);

For more info adding views programmatically check Android: Add two text views programmatically

Community
  • 1
  • 1
Jose L Ugia
  • 5,960
  • 3
  • 23
  • 26
  • i dont want to add statically ..take a look at question and code i have written that i want to create imageview dynamically and for that i am using for loop for image view..i dont want to add imageview statically – Swap-IOS-Android Jun 28 '13 at 11:57
  • 2
    Well, what you return at the end of the method is what is being shown. By returning the view three times you don't get the 3 images drawn together, instead the 3 of them are drawn sequentially and obviously you end up seeing the last one. On top of that I wouldn't use that viewHolder approach. The viewHolder should be the whole view, and inside of it, you can put the rest of the content. Your current way doesn't help that much on recycling.Check http://sriramramani.wordpress.com/2012/07/25/infamous-viewholder-pattern/ .Finally the imageview is not statically added,it's just an instance per row – Jose L Ugia Jun 28 '13 at 12:06