如何在android中添加图片到listview?

时间:2014-02-27 09:47:27

标签: android android-listview

我想将图片加载到listview。 看起来喜欢这个。

this

我不知道如何将此图片添加到listview。 单击此列表项时,会加载另一个活动。 我可以将此图像添加到列表并设置为适配器吗?

4 个答案:

答案 0 :(得分:1)

you need to create custom xml(only with image) for your list view and you need to set custom adapter to your list view.

    check this link this will help you.

        http://www.mkyong.com/android/android-listview-example/

    or try this

    Activity file:

    public class MainActivity extends Activity 
    {

         ImageView selectedImage;  
         private Integer[] mImageIds = {
                    R.drawable.ic_launcher,
                    R.drawable.android3d,
                    R.drawable.ic_launcher,

            };
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

                 ListView gallery = (ListView) findViewById(R.id.gallery1);

            gallery.setAdapter(new GalleryImageAdapter(this,R.layout.img));

             // clicklistener for Gallery
            gallery.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    Toast.makeText(MainActivity.this, "Your selected position = " + position, Toast.LENGTH_SHORT).show();

                }
            });
        }
       class GalleryImageAdapter extends BaseAdapter 
    {
        private Context mContext;

        int layout ;

        public GalleryImageAdapter(Context context, int layout) 
        {
            mContext = context;
            this.layout = layout;
        }

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

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

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


        // Override this method according to your need
        public View getView(int index, View view, ViewGroup viewGroup) 
        {
            // TODO Auto-generated method stub

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

                View rowView = inflater.inflate(layout, viewGroup, false);

                ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
                imageView.setImageResource(mImageIds[index]);


                return rowView;

        }
    }
    }

    activity_mail xml:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:orientation="vertical">


            <ListView
                android:id="@+id/gallery1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

         </LinearLayout>

    img xml :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

答案 1 :(得分:1)

http://www.mkyong.com/android/android-listview-example/

而不是字符串数组你必须使用整数数组&amp;将可绘制的图像放入其中。

像这样:

int images[] = {R.drawable.product1, R.drawable.product2, R.drawable.product3, R.drawable.product4, R.drawable.product5, R.drawable.product6, R.drawable.product7};

&安培;

在ImageView中获取图像,如下所示:

imgview.setImageResource(arr[position]);

你已经完成了。

答案 2 :(得分:0)

对于这种行为,您需要实现自己的适配器。当您使用Arrayadapter时,您应该扩展Arrayadapter并覆盖getView方法,以您希望的方式更改ListItem的视图。有关此主题的更多信息,请查看此ListView教程。 http://www.vogella.com/tutorials/AndroidListView/article.html

答案 3 :(得分:0)

您可以使用自定义adapter项目来执行此操作。每个项目都是ImageView,如下例所示:

<强> MainActivity:

this.adapter = new ItemsAdapter(this, R.layout.items_list_item, ItemManager.getLoadedItems());
setListAdapter(this.adapter);

创建以下adapter类:

<强> ItemsAdapter:

private class ItemsAdapter extends ArrayAdapter<Item> {

private Item[] items;

public ItemsAdapter(Context context, int textViewResourceId, Item[] items) {
    super(context, textViewResourceId, items);
    this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.items_list_item, null);
    }

    Item it = items[position];
    if (it != null) {
        ImageView iv = (ImageView) v.findViewById(R.id.list_item_image);
        if (iv != null) {
            iv.setImageDrawable(it.getImage());
        }
    }

    return v;
}
}

然后在onItemClickListener

中添加您想要的操作