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;
}
}
ArrayListof Type Ranking instead of Array if you don't known how many items you are going to show inListView. if you know number of items then first define the Size ofranking_dataArray before adding items to itArrayListinstead of an Array? Sorry for being a total n00b when it comes to Arrays and stuff. See my Edit on how myRankingAdapteris declared. My RankingAdapter extends ArrayAdapter<Ranking>.RankingAdapteradded.