Android的列表活动与动态加载的图像形成网Android中 [英] Android List Activity with dynamically loaded images form the web in android

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

问题描述

我给我的项目的说明

在我的项目在第一部分我已经解析了视频类,并从我的网站service.I图像URL链接的XML数据已经分析的数据和接收数据的ArrayList 在我的主要活性。第一个的ArrayList 是视频类的列表,第二个的ArrayList 是名单视频图像的URL的,我必须展示的ArrayList 图片网址为的ImageView 的ListView ,我不知道对于这一点,请给我一些解决方案。

In my project In the first section i have parsed the xml data of Video categories and image url links from My web service.I have parsed that data and receive that data in ArrayList in My main activity.The first ArrayList is the list of video categories and the second ArrayList is the the list of video image urls and i have to display ArrayList of image urls as ImageView in ListView ,i have no idea for that,please give me some solution.

推荐答案

为了处理东西比String数组在ListView显示为TextView的,你需要编写自己的自定义适配器(因此Android知道如何显示这些项目)

In order to handle something else than an Array of String displayed as TextView in a ListView, you need to write your own Custom Adapter (so Android knows how to display those items)

下面是一个例子:

您活动:

public class MyActivity extends Activity{
    private ArrayList<URL> MY_DATA;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Sets the View of the Activity
        setContentView(R.layout.my_activity);
        ListView myList = (ListView) findViewById(R.id.myList);
        MyAdapter adapter = new MyAdapter(this, MY_DATA);
        listView.setAdapter(adapter);
}

您活动的布局(my_activity.xml这里):

Your Activity's Layout (my_activity.xml here):

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myList"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>

和您的自定义适配器:

public class MyAdapter extends BaseAdapter{
    private LayoutInflater inflater;
    private ArrayList<URL> data;

    public EventAdapter(Context context, ArrayList<URL> data){
    // Caches the LayoutInflater for quicker use
    this.inflater = LayoutInflater.from(context);
    // Sets the events data
    this.data= data;
    }

    public int getCount() {
        return this.data.size();
    }

    public URL getItem(int position) throws IndexOutOfBoundsException{
        return this.data.get(position);
    }

    public long getItemId(int position) throws IndexOutOfBoundsException{
        if(position < getCount() && position >= 0 ){
            return position;
        }
    }

    public int getViewTypeCount(){
        return 1;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        URL myUrl = getItem(position);
        ViewHolder holder = new ViewHolder(); // Use a ViewHolder to save your ImageView, in order not to have to do an expensive findViewById for each iteration

        // DO WHAT YOU WANT WITH YOUR URL (Start a new activity to download image?)

        if(convertView == null){ // If the View is not cached
        // Inflates the Common View from XML file
        convertView = this.inflater.inflate(R.id.my_row_layout, null);
            holder.myImageView = (ImageView)findViewById(R.id.myRowImageView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.myImageView.setImageBitmap(MY_BITMAP_I_JUST_DOWNLOADED);

        return convertView;
    }

    static class ViewHolder{
    ImageView myImageView;
    }
}

最后你行的布局(my_row_layout.xml这里):

And finally your row's layout (my_row_layout.xml here):

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myRowImageView"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>

这篇关于Android的列表活动与动态加载的图像形成网Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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