列表视图中添加图像 [英] add an image in listview

查看:111
本文介绍了列表视图中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在我的列表视图中添加更多的图像,因为这code仅低于每行中显示的图像1和2持续。我想要做的是为每个不同的行显示不同的图像。这里是我的code以下;

感谢您的帮助。我不擅长的java请更改code必要,然后我可以引用它。

公共类起动器扩展ListActivity
{
    私有静态类EfficientAdapter延伸BaseAdapter {
        私人LayoutInflater mInflater;
        私人位图mIcon1;
        私人位图mIcon2;
        私人位图mIcon3;
        私人位图mIcon4;
        私人位图mIcon5;
        私人位图mIcon6;
        私人位图mIcon7;
        私人位图mIcon8;
        私人位图mIcon9;
        私人位图mIcon10;

 公共EfficientAdapter(上下文的背景下){
        //缓存的LayoutInflate避免要求每次一个新的。
        mInflater = LayoutInflater.from(上下文);        //结合到行图标。
        mIcon1 = BitmapFactory.de codeResource(context.getResources(),R.drawable.starters1);
        mIcon2 = BitmapFactory.de codeResource(context.getResources(),R.drawable.starters2);
        mIcon3 = BitmapFactory.de codeResource(context.getResources(),R.drawable.starters3);
        mIcon4 = BitmapFactory.de codeResource(context.getResources(),R.drawable.starters4);
        mIcon5 = BitmapFactory.de codeResource(context.getResources(),R.drawable.starters5);
        mIcon6 = BitmapFactory.de codeResource(context.getResources(),R.drawable.starters6);
        mIcon7 = BitmapFactory.de codeResource(context.getResources(),R.drawable.starters7);
        mIcon8 = BitmapFactory.de codeResource(context.getResources(),R.drawable.starters8);
        mIcon9 = BitmapFactory.de codeResource(context.getResources(),R.drawable.starters9);
        mIcon10 = BitmapFactory.de codeResource(context.getResources(),R.drawable.starters10);
    }    公众诠释的getCount(){
        返回DATA.length;
    }    公共对象的getItem(INT位置){
        返回的位置;
    }    众长getItemId(INT位置){
        返回的位置;
    }    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        //一个ViewHolder不断提及孩子的意见,以避免不必要的来电
        //在每个行findViewById()。
        ViewHolder持有人;        //当convertView不为空,我们可以直接重用,也没有必要
        //为reinflate它。我们只吹一个新的View提供的convertView时
        //通过ListView控件为空。
        如果(convertView == NULL){
            convertView = mInflater.inflate(R.layout.starters,NULL);            //创建两个孩子意见ViewHolder和存储引用
            //我们需要将数据绑定到。
            持有人=新ViewHolder();            holder.text =(TextView中)convertView.findViewById(R.id.text01);
            holder.text =(TextView中)convertView.findViewById(R.id.secondLine);
            holder.icon =(ImageView的)convertView.findViewById(R.id.icon01);            convertView.setTag(保持器);
        }其他{
            //获取ViewHolder回才能到TextView的快速访问
            //和ImageView的。
            支架=(ViewHolder)convertView.getTag();
        }        //与支架有效地结合的数据。
        holder.text.setText(DATA [位置]);
       holder.icon.setImageBitmap((位置和放大器; 1)== 1 mIcon1:mIcon2);
         返回convertView;
    }    静态类ViewHolder {
        TextView的文本;
        ImageView的图标;
    }
}@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    setListAdapter(新EfficientAdapter(本));
}私有静态最后的String [] DATA = {
    原始玉米片,烤鸡肉和奶酪克萨迪亚斯,鸡,石灰和香菜玉米片
    辣豆和奶酪克萨迪亚斯,金枪鱼和玉米油炸玉米饼,俗气豆和甜玉米玉米片,香酥鸡,鳄梨和石灰沙拉,牛肉和玉米笋tostada
    鸡和虾辣的墨西哥饭,辣椒土豆船};

}


解决方案

  

我想要做的是为每个不同的行显示不同的图像是什么


然后把不同的图像中的每个一行。您可以通过以上每一行中发生的事情控制getView()。两个不同的图像之间的当前实现迭代 - 改变这种逻辑来为您的应用程序的任何作品

下面是从进入更详细的关于这个我的书一个免费的摘录

Hi I would like to add more images in my list view as this code below only displays image 1 and 2 continuously in each row. What I want to do is display a different image for each different row. Here is mycode below;

Thanks for any help. I am not good at java please change the code where necessary and I can then refer to it.

public class starters extends ListActivity { private static class EfficientAdapter extends BaseAdapter { private LayoutInflater mInflater; private Bitmap mIcon1; private Bitmap mIcon2; private Bitmap mIcon3; private Bitmap mIcon4; private Bitmap mIcon5; private Bitmap mIcon6; private Bitmap mIcon7; private Bitmap mIcon8; private Bitmap mIcon9; private Bitmap mIcon10;

    public EfficientAdapter(Context context) {
        // Cache the LayoutInflate to avoid asking for a new one each time.
        mInflater = LayoutInflater.from(context);

        // Icons bound to the rows.
        mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters1);
        mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters2);
        mIcon3 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters3);
        mIcon4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters4);
        mIcon5 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters5);
        mIcon6 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters6);
        mIcon7 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters7);
        mIcon8 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters8);
        mIcon9 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters9);
        mIcon10 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters10);
    }

    public int getCount() {
        return DATA.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid unneccessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is no need
        // to reinflate it. We only inflate a new View when the convertView supplied
        // by ListView is null.
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.starters, null);

            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();

            holder.text = (TextView) convertView.findViewById(R.id.text01);
            holder.text = (TextView) convertView.findViewById(R.id.secondLine);
            holder.icon = (ImageView) convertView.findViewById(R.id.icon01);

            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        holder.text.setText(DATA[position]);
       holder.icon.setImageBitmap((position & 1) ==1 ? mIcon1 : mIcon2);


         return convertView;
    }

    static class ViewHolder {
        TextView text;
        ImageView icon;
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new EfficientAdapter(this));
}

private static final String[] DATA = {
    "Original nachos", "Toasted chicken and cheese quesadillas", "Chicken, lime and coriander nachos",
    "Spicy bean and cheese quesadillas", "Tuna and corn quesadillas", "Cheesy bean and sweetcorn nachos", "Crispy chicken, avocado and lime salad", "Beef and baby corn tostada",
    "Spicy mexican rice with chicken and prawns", "Chilli potato boats"};

}

解决方案

What I want to do is display a different image for each different row

Then put a different image in each row. You have control over what goes in each row via getView(). Your current implementation iterates between two different images -- change that logic to be whatever works for your app.

Here is a free excerpt from one of my books that goes into greater detail on this.

这篇关于列表视图中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆