如何加载这些元素来填充的android列表

如何加载这些元素来填充的android列表

本文介绍了如何加载这些元素来填充的android列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这从一个网页元素的列表。

  //获取所有的游戏元素
元素游戏= doc.select(div.FP_Up_TextWrap B); //创建新的ArrayList
ArrayList的<串GT; gameList =新的ArrayList<串GT;();//迭代器对这些元素
的ListIterator&所述;组件> postIt = games.listIterator();而(postIt.hasNext()){
//本场比赛的文字添加到ArrayList
gameList.add(postIt.next()文本());
}

这将返回所有的变量与这就是我想要的。但它返回的头衔,我只想标题发布日期和图SR完整标记。
我想它返回一个列表视图。
我将如何去这样做呢?我也许这样做完全错了,所以你们可能要检查出HTML页面的源代码。

编辑 - 给我空指针错误

  / **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    outputTextView =(的TextView)findViewById(R.id.outputTextView);   ListView控件列表=(ListView控件)findViewById(R.id.list);
   ArrayList的<串GT; gameList =新的ArrayList<串GT;();
      文档DOC = NULL;
    尝试{
        DOC = Jsoup.connect(http://www.gamespy.com/index/release.html)获得();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
        //获取所有TD的是一排的孩子 - 每个游戏都有这4
        元素游戏= doc.select(TR> td.indexList1,TR> td.indexList2);
        //迭代器对这些元素
        的ListIterator&所述;组件> postIt = games.listIterator();
        而(postIt.hasNext()){
             //本场比赛的文字添加到ArrayList
             gameList.add(postIt.next()文本());
        }        的String []项目=新的String [gameList.size()];
        gameList.toArray(项目);
        ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,
                    android.R.layout.simple_list_item_1,项目);    这里//错误点
             list.setAdapter(适配器);
    }
    }

编辑 - 布局名单

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / outputTextView
/>
< ListView的机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID /列表的android:layout_width =match_parent>< /&的ListView GT;
< / LinearLayout中>


解决方案

您将无法以回归列表视图使用此code,但你可以创建自己的列表视图子类,你可以然后返回,以下同技术:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    ListView控件ListView1的=(的ListView)findViewById(R.id.listView1);    ArrayList的<串GT; gameList =新的ArrayList<串GT;();    //迭代器对这些元素
    的ListIterator&所述;组件> postIt = games.listIterator();
    而(postIt.hasNext()){
         //本场比赛的文字添加到ArrayList
         gameList.add(postIt.next()文本());
    }    的String []项目=新的String [gameList.size()];
    gameList.toArray(项目);
    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,
                android.R.layout.some_list_item_view,项目);    listView1.setAdapter(适配器);
}

最终它归结为是设置列表适配器列表。如果您需要创建自己的列表适配器,你也可以继承并实施ListAdapter数据绑定其他对象类型,但已经有用于处理简单的字符串集的ListView / ListActivity绑定一个predefined适配器(ArrayAdapter)。

更新 - 海报要求的额外实施细节:

假设:

 公共类GameMeta {
    私人字符串m_title;
    私人java.util.Date m_releaseDate;    公共GameMeta(){
    }    公共字符串的getTitle(){
         返回m_title;
    }    公共无效的setTitle(字符串值){
         m_title =价值;
    }    公共java.util.Date getReleaseDate(){
         返回m_releaseDate;
    }    公共无效setReleaseDate(java.util.Date RELEASEDATE){
         m_releaseDate = RELEASEDATE;
    }
}

您可以创建适配器:

 公共类GamesMetaAdapter实现适配器扩展ListAdapter {
     私人的ArrayList< GameMeta> m_list =新的ArrayList< GameMeta>();
     私人语境m_context = NULL;     公共GamesMetaAdapter(上下文的背景下){
         m_context =背景;
     }     @覆盖
     公众诠释的getCount(){
          返回m_list.size();
     }     @覆盖
     公共对象的getItem(INT位置){
        如果(位置< m_list.size()){
          返回m_list.getAt(位置);
        }
        返回null;
     }     @覆盖
     众长getItemId(INT位置){
        返回(长)的位置;
     }     @覆盖
     公众诠释getItemViewType(INT位置){
        返回IGNORE_ITEM_VIEW_TYPE;
     }     @覆盖
     公共查看getView(INT位置,查看convertView,父母的ViewGroup){
             TextView的simple查看=新的TextView(this.m_context);
             。字符串viewText = this.m_items [位置] .getTitle()+,发布+ this.m_items [位置] .getReleaseDate()的toString();             simpleView.setText(this.m_items [位置] .getTitle());             如果(TextView.class.isInstance(convertView)){
                   convertView = simple查看;
             }             返回simple查看;
     }
     @覆盖
     公众诠释getViewTypeCount(){
        返回1;
     }     @覆盖
     公共布尔hasStableIds(){
        返回false;
     }     @覆盖
     公共布尔的isEmpty(){
        返回m_list.size()== 0;
     }     @覆盖
     公共无效registerDataSetObserver(DataSetObserver观察者){     }     @覆盖
     公共无效unregisterDataSetObserver(DataSetObserver观察者){     }     / * LIST ADAPTER会员* /
     @覆盖
     公共布尔areAllItemsEnabled(){
          返回true;
     }     @覆盖
     公共布尔isEnabled(INT位置){
        返回true;
     }
}

我没有一个Java编译器在这里,所以如果这不只是复制/粘贴到您的项目请不要叮我,但它应该是很好的北方90%的在那里。

快乐编码。

I am using this to get a list of elements from a webpage.http://www.gamespy.com/index/release.html

// Get all the game elements
Elements games = doc.select("div.FP_Up_TextWrap b");

 // Create new ArrayList
ArrayList<String> gameList = new ArrayList<String>();

// Iterator over those elements
ListIterator<Element> postIt = games.listIterator();

while (postIt.hasNext()) {
// Add the game text to the ArrayList
gameList.add(postIt.next().text());
}

This returns all the tags with which is what I want. But it returns the full tag with the title I just want the title the release date and a Img sr.I would like to return it to a list view.How would I go about doing this? I maybe doing it totally wrong so you guys may want to check out the HTML page source.

EDIT - Gives me NullPointer error

  /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    outputTextView = (TextView)findViewById(R.id.outputTextView);

   ListView list = (ListView)findViewById(R.id.list);
   ArrayList<String> gameList = new ArrayList<String>();


      Document doc = null;
    try {
        doc = Jsoup.connect("http://www.gamespy.com/index/release.html").get();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        // Get all td's that are a child of a row - each game has 4 of these
        Elements games = doc.select("tr > td.indexList1, tr > td.indexList2");
        // Iterator over those elements
        ListIterator<Element> postIt = games.listIterator();
        while (postIt.hasNext()) {
             // Add the game text to the ArrayList
             gameList.add(postIt.next().text());
        }

        String[] items = new String[gameList.size()];
        gameList.toArray(items);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, items);

    //  error points here
             list.setAdapter(adapter);
    }


    }

EDIT - Layout for the list

<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/outputTextView"
/>
<ListView    android:layout_height="wrap_content" android:id="@+id/list" android:layout_width="match_parent"></ListView>
</LinearLayout>
解决方案

You won't be able to "return a list view" using this code, but you could create your own list view sub class which you could then return, following the same techniques:

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

    ListView listView1 = (ListView) findViewById(R.id.listView1);

    ArrayList<String> gameList = new ArrayList<String>();

    // Iterator over those elements
    ListIterator<Element> postIt = games.listIterator();
    while (postIt.hasNext()) {
         // Add the game text to the ArrayList
         gameList.add(postIt.next().text());
    }

    String[] items = new String[gameList.size()];
    gameList.toArray(items);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.some_list_item_view, items);

    listView1.setAdapter(adapter);
}

Ultimately what it comes down to is setting the list adapter for the list. If you need to create your own list adapter you can subclass and implement ListAdapter to databind other object types, but there's already a predefined adapter (ArrayAdapter) for handling simple string sets for ListView/ListActivity databinding.

UPDATE - Poster requested additional implementation details:

Given:

public class GameMeta {
    private String m_title;
    private java.util.Date m_releaseDate;

    public GameMeta() {
    }

    public String getTitle(){
         return m_title;
    }

    public void setTitle(String value) {
         m_title = value;
    }

    public java.util.Date getReleaseDate(){
         return m_releaseDate;
    }

    public void setReleaseDate(java.util.Date releaseDate){
         m_releaseDate = releaseDate;
    }
}

You might create the adapter:

public class GamesMetaAdapter implements Adapter extends ListAdapter {
     private ArrayList<GameMeta> m_list = new ArrayList<GameMeta>();
     private Context m_context = null;

     public GamesMetaAdapter(Context context) {
         m_context = context;
     }

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

     @Override
     public Object getItem(int position){
        if(position < m_list.size()){
          return m_list.getAt(position);
        }
        return null;
     }

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

     @Override
     public int getItemViewType(int position) {
        return IGNORE_ITEM_VIEW_TYPE;
     }

     @Override
     public View getView (int position, View convertView, ViewGroup parent) {
             TextView simpleView = new TextView(this.m_context);
             String viewText = this.m_items[position].getTitle() + ", Released " + this.m_items[position].getReleaseDate().toString();

             simpleView.setText(this.m_items[position].getTitle());

             if(TextView.class.isInstance(convertView)) {
                   convertView = simpleView;
             }

             return simpleView;
     }


     @Override
     public int getViewTypeCount() {
        return 1;
     }

     @Override
     public boolean hasStableIds(){
        return false;
     }

     @Override
     public boolean isEmpty() {
        return m_list.size() == 0;
     }

     @Override
     public void registerDataSetObserver (DataSetObserver observer) {

     }

     @Override
     public void unregisterDataSetObserver (DataSetObserver observer) {

     }

     /* LIST ADAPTER MEMBERS */
     @Override
     public boolean areAllItemsEnabled() {
          return true;
     }

     @Override
     public boolean isEnabled(int position) {
        return true;
     }
}

I don't have a Java compiler here, so please don't ding me if this doesn't just copy/paste right into your project, but it should be well north of 90% there.

Happy coding.

B

这篇关于如何加载这些元素来填充的android列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:04