How to Put a Linear Layout

length * 5;  
    }  
      
    @Override   
    public View getView(int index, View toReuse, ViewGroup container)  
    {  
        ViewGroup imageView = null;  
          
        if (toReuse != null)  
        {  
            imageView = (ViewGroup)toReuse;  
        }  
        else 
        {  
            LayoutInflater inflater = (LayoutInflater) getContext()  
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
            imageView = (ViewGroup)inflater.inflate(  
                    R.layout.text_and_btton, null, true);  
        }  
          
        Button button = (Button)imageView.getChildAt(0);  
        TextView tv = (TextView)imageView.getChildAt(1);  
          
        Image desiredImage = images[index % images.length];  
          
        button.setBackgroundResource(desiredImage.id);  
        tv.setText(desiredImage.name);  
          
        return imageView;  
    }  
      
    @Override 
    /* 
     * Because I am accessing items from an array, ids will be stable.  If 
     * coming from a DB, maybe this should not be true. 
     * @see android.widget.BaseAdapter#hasStableIds() 
     */ 
    public boolean hasStableIds()  
    {  
        return true;  
    }  
 
    public static class Image  
    {  
        private final String name;  
        private final int id;  
 
        public Image(String n, int i)  
        {  
            this.name = n;  
            this.id = i;  
        }  
    }  

public class ImageStuffAdapter extends ArrayAdapter<ImageStuffAdapter.Image>
{
    private static final Image[] images = new Image[]
    {
            new Image("pizza", R.drawable.pizza),
            new Image("fish", R.drawable.fish),
            new Image("boissoins", R.drawable.boissoins)
    };

    public ImageStuffAdapter(Context context)
    {
        super(context, R.id.label);
    }
   
    @Override
    public int getCount()
    {
        return images.length * 5;
    }
   
    @Override
    public View getView(int index, View toReuse, ViewGroup container)
    {
        ViewGroup imageView = null;
       
        if (toReuse != null)
        {
            imageView = (ViewGroup)toReuse;
        }
        else
        {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imageView = (ViewGroup)inflater.inflate(
                    R.layout.text_and_btton, null, true);
        }
       
        Button button = (Button)imageView.getChildAt(0);
        TextView tv = (TextView)imageView.getChildAt(1);
       
        Image desiredImage = images[index % images.length];
       
        button.setBackgroundResource(desiredImage.id);
        tv.setText(desiredImage.name);
       
        return imageView;
    }
   
    @Override
    /*
     * Because I am accessing items from an array, ids will be stable.  If
     * coming from a DB, maybe this should not be true.
     * @see android.widget.BaseAdapter#hasStableIds()
     */
    public boolean hasStableIds()
    {
        return true;
    }

    public static class Image
    {
        private final String name;
        private final int id;

        public Image(String n, int i)
        {
            this.name = n;
            this.id = i;
        }
    }
}

Then in the activity where you want to display the list, you build the layout with the list view, then get the ListView and set the adapter:
public class MyActivity extends FragmentActivity  
{  
     
    @Override 
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.image_list);  
          
        ListView imageList = (ListView) findViewById(R.id.image_list);  
        ImageStuffAdapter imageAdapter = new ImageStuffAdapter(this);  
        imageList.setAdapter(imageAdapter);  
    }  
 

public class MyActivity extends FragmentActivity
{
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_list);
       
        ListView imageList = (ListView) findViewById(R.id.image_list);
        ImageStuffAdapter imageAdapter = new ImageStuffAdapter(this);
        imageList.setAdapter(imageAdapter);
    }

}

If you link this with your other posts, the ImageStuffAdapter is the correct place to do the database work to get the images and text you want to display.

Pretty similar stuff to the last code. The new outermost layout uses a GridView instead o

Back  [1] [2] [3] Next

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright