如何从ArrayAdapter填充ListView [英] How to populate ListView from ArrayAdapter

查看:77
本文介绍了如何从ArrayAdapter填充ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读HTML页面,并从包含two colums的表中获取值.这些值需要插入到ListView.

I'm reading an HTML page and get values from a table consisting of two colums. These values I need to insert into a ListView.

我很难理解ArraysArrayAdapter的概念,因此我试图通过阅读各种代码示例来介绍一些东西.

I'm having a hard time grasping the concepts of Arrays, and ArrayAdapter, so I've tried to put something by reading various code examples.

当我静态填充ArrayAdapter(在Java代码中)时,我的代码工作完美,但是我需要使用循环(或其他方式?)来填充从HTML页面中获得的内容(所有这些代码)可以正常使用.

My code works perfect when I fill my ArrayAdapter statically (in Java code), but I need to use a loop (or something?) to fill the contents of the array with what I get from the HTML page (all that code works and is in place).

这就是我正在使用的:

Ranking.java

public class Ranking {
    public String month;
    public String rank;

    public Ranking() {
        super();
    }
    public Ranking(String month, String rank) {
        super();
        this.month = month;
        this.rank = rank;
    }
}

此代码有效:

Ranking ranking_data[] = new Ranking[] {
    new Ranking("January 2013", "67"),
    new Ranking("February 2013", "45"),
}

然后我用自己的RankAdapter引用ranking_data

And then I reference the ranking_data with my own RankingAdapter;

RankingAdapter adapter = new RankingAdapter(this, R.layout.listview_item_row, ranking_data);

所以我的问题是

如何动态填充ranking_data?

例如,此不会似乎可以循环填充;

For example, this doesn't seem to work to populate in a loop;

Ranking ranking_data[] = null;
String mVal1, mVal2;
while (myIterator1.hasNext() && myIterator2.hasNext()) {
   mVal1 = myIterator1.next().text();
   mVal2 = myIterator2.next().text();
   new Ranking(mVal1, mVal2);
}

这使我在调用RankingAdapter adapter = new RankingAdapter(this, R.layout.listview_item_row, ranking_data);时得到一个NullPointerException,也许是因为我对ranking_data的初始化将其设置为null,但是我应该如何初始化它呢?

This gives me a NullPointerException on my call to RankingAdapter adapter = new RankingAdapter(this, R.layout.listview_item_row, ranking_data);, perhaps because my initialization of ranking_data sets it to null, but how otherwise should I initialize it?

修改
我的RankingAdapter声明如下:

Edit
My RankingAdapter is declared like this:

public class RankingAdapter extends ArrayAdapter<Ranking>是否有帮助?

编辑2

RankingAdapter.java

public class RankingAdapter extends ArrayAdapter<Ranking> {
  Context context;
  int layoutResourceId;   
  Ranking data[] = null;

public RankingAdapter(Context context, int layoutResourceId, Ranking[] data) {
  super(context, layoutResourceId, data);
  this.layoutResourceId = layoutResourceId;
  this.context = context;
  this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    RankingHolder holder = null;

    if(row == null) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new RankingHolder();
        holder.txtMonth = (TextView)row.findViewById(R.id.item_month);
        holder.txtRank = (TextView)row.findViewById(R.id.item_rank);

        row.setTag(holder);
    } else {
        holder = (RankingHolder)row.getTag();
    }

    Ranking ranking = data[position];

    holder.txtMonth.setText(ranking.month);
    holder.txtRank.setText(ranking.rank);

    return row;
}
static class RankingHolder {
    TextView txtMonth;
    TextView txtRank;
}

}

推荐答案

在您赋予的代码中,您并未分配数据/将数据添加到列表中,并且仅在第一次时将其初始化为null并导致错误.

In the code you have wtitten, you were not assigning/adding data to your list and it was initialized null at first time only and throuwing error.

您可以编写如下内容:

//declare arraylist
ArrayList<Ranking> ItemArray = new ArrayList<Ranking>();
String mVal1, mVal2;
while (myIterator1.hasNext() && myIterator2.hasNext()) {
   mVal1 = myIterator1.next().text();
   mVal2 = myIterator2.next().text();
   //add item in your arrayList
   ItemArray.add(new Ranking(mVal1, mVal2));
}

如果您要更改列表视图的内容,请别忘了写adapter.notifydatasetchanged()

And dont forget to write adapter.notifydatasetchanged() if you are changing content of your listview

请参考以下链接,以使用自定义数组适配器清楚了解Listview:

refer below link to get clear idea of listview with custom array adapter:

http://www.vogella.com/articles/AndroidListView/article.html

这篇关于如何从ArrayAdapter填充ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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