如何将信息查询放入列表视图?

时间:2011-08-03 02:46:13

标签: android android-listview

我有一个ListView

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

我正在检索来自网址Feed的信息查询。这是我如何检索饲料的一个例子。

            query.setOrderBy(YouTubeQuery.OrderBy.VIEW_COUNT);
            query.setFullTextQuery("basketball");
            query.setSafeSearch(YouTubeQuery.SafeSearch.NONE);
            VideoFeed videoFeed = service.query(query, VideoFeed.class);
            printVideoFeed(videoFeed, true);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
}

public void printVideoFeed(VideoFeed videoFeed, boolean detailed){
    videoFeed.getTitle().toString();
    }
}

正如您在此处所看到的,我检索了信息提要......现在,在此方法中,我想将所有信息设置为包含所有信息的列表。

例如带有行的ListView。 row.xml包含一个imageView和一个TextView。

我如何制作它以便每个Feed在列表视图中设置为一行?

编辑:我正在尝试通过谷歌搜索使用gdata api搜索的视频列表。这是我正在使用的代码..

public void printVideoEntry(VideoEntry videoEntry, boolean detailed){
    if(detailed){

        YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();

        System.out.println("Uploaded by: " + mediaGroup.getUploader());

        //Here i would like to set one of the thumbnails to a imageView.
        mediaGroup.getThumbnails();

        System.out.println("Video ID: " + mediaGroup.getVideoId());
        System.out.println("Description: " + 
          mediaGroup.getDescription().getPlainTextContent());

        MediaPlayer mediaPlayer = mediaGroup.getPlayer();
        System.out.println("Web Player URL: " + mediaPlayer.getUrl());
        MediaKeywords keywords = mediaGroup.getKeywords();
        System.out.print("Keywords: ");
        for(String keyword : keywords.getKeywords()) {
            System.out.print(keyword + ",");
        }
    }
}

我想让它填入列表中,每个视频标题和自己的imageView都是缩略图。

1 个答案:

答案 0 :(得分:1)

您需要创建自己的适配器,它应如下所示:

public class MyCustomAdapter extends ArrayAdapter<YourObject> {

private final Context mContext;
private final List<YourObject> objectList;
private final int layout;
private final LayoutInflater inflater;

static class ViewHolder {
    public ImageView objectImage;
    public TextView objectTitle;
}

public MyCustomAdapter(Context context, int layout, List<YourObject> objects) {
    super(context, layout, objects);

    this.mContext = context;
    this.inflater = LayoutInflater.from(context);
    this.layout = layout;
    this.objectList = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;

    if (convertView == null) {
        convertView = inflater.inflate(layout, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.objectImage = (ImageView)convertView.findViewById(R.id.image_id);
        viewHolder.objectTitle = (TextView)convertView.findViewById(R.id.title_id);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder)convertView.getTag();
    }


    viewHolder.objectImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.your_image))
    viewHolder.objectTitle.setText("Title Here");

    return convertView;
}

@Override 
public int getCount() { 
    return objectList.size(); 
}
}

并按照以下方式实施:

MyCustomAdapter adapter = new MyCustomAdapter(this, R.layout.your_layout, yourObjectArrayList);
ListView list = (ListView)findViewById(R.id.your_list);
list.setAdapter(adapter);

希望这有帮助!