0

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

}

kaderud
  • 5,457
  • 2
  • 36
  • 49
  • 3
    use `ArrayList` of Type Ranking instead of Array if you don't known how many items you are going to show in `ListView` . if you know number of items then first define the Size of `ranking_data` Array before adding items to it – ρяσѕρєя K Mar 15 '13 at 17:57
  • @ρяσѕρєяK How would I declare Ranking with an `ArrayList` instead of an Array? Sorry for being a total n00b when it comes to Arrays and stuff. See my Edit on how my `RankingAdapter` is declared. My RankingAdapter extends ArrayAdapter. – kaderud Mar 15 '13 at 18:12
  • http://stackoverflow.com/questions/15109406/how-to-set-json-parsed-data-in-a-listview-and-then-adding-search-functionality-i/15109592#15109592. Check the answer. Instead of NewData have Ranking class. Modify the code accordingly. – Raghunandan Mar 15 '13 at 18:24
  • @ρяσѕρєяK Updated Q with code for `RankingAdapter` added. – kaderud Mar 15 '13 at 18:26
  • @ρяσѕρєяK Perfect! That works! If you post it as an answer you will earn it as the Accepted answer! – kaderud Mar 15 '13 at 18:38

2 Answers2

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

Akbari Dipali
  • 2,173
  • 1
  • 20
  • 26
  • Sort of, it wasn't the full solution to my problem, it didn't solve my problem with the `RankingAdapter extends ArrayAdapter`, but @ρяσѕρєяK solved the problem for me, so if he puts it up as an answer, he will earn the Accepted answer reward. You did however get an upvote for partially solving it just has he also suggested. – kaderud Mar 15 '13 at 22:49
0

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

Chen Kinnrot
  • 20,609
  • 17
  • 79
  • 141