0

I have a ListViewAdapter that displays a game's scoreboard in three columns.

As properties of my MainActivity, I have

ArrayList<HashMap<String,String>> scoreList;

ListViewAdapter scoreboardAdapter;

In OnCreate(), I have

scoreList = new ArrayList<>();
scoreboardAdapter = new ListViewAdapter(this, scoreList);
((ListView)findViewById(R.id.scoreBoard)).setAdapter(scoreboardAdapter);

I have a class to build the scorelist from a Map (player ids and scores) here:

public class ScoreBoard {
public ScoreBoard(HashMap<String,Integer> scores){
    this.scores = scores;
}

private Map<String,Integer> scores;

public ArrayList<HashMap<String,String>> GetAdapterList()
{
    scores = MapUtil.sortByValue(scores);
    ArrayList<HashMap<String,String>> adapterList = new ArrayList<>();

    Integer i = 1;
    Iterator it = scores.entrySet().iterator();
    while (it.hasNext()) {
        HashMap<String, String> columnMap = new HashMap<>();
        Map.Entry pair = (Map.Entry)it.next();
        columnMap.put(Constants.FIRST_COLUMN, i.toString());
        columnMap.put(Constants.SECOND_COLUMN, pair.getKey().toString());
        columnMap.put(Constants.THIRD_COLUMN, pair.getValue().toString());
        adapterList.add(columnMap);
        it.remove();
        i++;
    }
    return adapterList;
}
}

Custom ListViewAdapter class

public class ListViewAdapter extends BaseAdapter{

public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;

public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
    super();
    this.activity=activity;
    this.list=list;
}

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

@Override
public Object getItem(int position) {
    return list.get(position);
}

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



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null){

        convertView=inflater.inflate(R.layout.column_row, null);

        txtFirst=(TextView) convertView.findViewById(R.id.rank);
        txtSecond=(TextView) convertView.findViewById(R.id.name);
        txtThird=(TextView) convertView.findViewById(R.id.score);
    }

    HashMap<String, String> map=list.get(position);
    txtFirst.setText(map.get(Constants.FIRST_COLUMN));
    txtSecond.setText(map.get(Constants.SECOND_COLUMN));
    txtThird.setText(map.get(Constants.THIRD_COLUMN));

    return convertView;
}

}

And finally, I have the Update method (which I'm calling from the ui thread)

void updatePeerScoresDisplay() {
    Log.d(TAG, "updatePeerScoresDisplay entered");
    scoreList.clear();
    scoreList.addAll(new ScoreBoard(mParticipantScore).GetAdapterList());
    scoreboardAdapter.notifyDataSetChanged();
}

Only the first call to updatePeerScoresDisplay is working. After that, no effect. I'm even debugging and seeing scorelist updated correctly before NotifyDataSetChanged is called. What can cause this?

b15
  • 2,101
  • 3
  • 28
  • 46

0 Answers0