How to populate ListView from ArrayAdapter

浪尽此生 提交于 2020-01-06 07:04:04

问题


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.

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.

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).

This is what I'm using:

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;
    }
}

This code works:

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

And then I reference the ranking_data with my own RankingAdapter;

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

So my question is;

How do I populate ranking_data dynamically?

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);
}

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?

Edit
My RankingAdapter is declared like this:

public class RankingAdapter extends ArrayAdapter<Ranking> if it helps?

Edit 2

Added code for 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;
}

}


回答1:


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.

you can write something like below:

//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));
}

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

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

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




回答2:


initialize the adapter with empty collection, and change the data of the collection dynamically, after you change it call the notifyDatasetChanged method of the Array Adapter



来源:https://stackoverflow.com/questions/15439062/how-to-populate-listview-from-arrayadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!