Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get notifyDatasetChanged() to work with a ListAdapter?

Right now I use setAdapter to update my ListView, but I think the proper way is to use notifiyDatasetChanged() and I can't get that to work in my main class (it's in the adapter). Here is the error:

The method notifyDatasetChanged() is undefined for the type ListAdapter

I'm guessing there is a better way of doing this - can anyone point me in the right direction?

Here's the relevant parts of my code:

public class ScoreList extends SherlockFragmentActivity {

    private ListView listViewScore;

    static List<Score> listScore = new ArrayList<Score>();
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.score_list);
        ctx = this;
        listScore = dbh.getAllScores();

        listViewScore = (ListView) findViewById(R.id.score_list);

        listViewScore.setAdapter(new ScoreListAdapter(ctx,
                R.layout.score_row_item, listScore));
        listViewScore.getAdapter().notifyDatasetChanged(); //this is where I get the error
    }
}

Here's the adapter:

public class ScoreListAdapter extends ArrayAdapter<Score> {
    private int resource;
    private LayoutInflater inflater;

    public ScoreListAdapter(Context ctx, int resourceId, List<Score> objects) {
        super(ctx, resourceId, objects);
        resource = resourceId;
        inflater = LayoutInflater.from(ctx);
        //context = ctx;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        convertView = (LinearLayout) inflater.inflate(resource, null);

        Score score = getItem(position);

        TextView txtName = (TextView) convertView.findViewById(R.id.name);
        txtName.setText(score.getName());

        TextView txtScoreChange = (TextView) convertView
                .findViewById(R.id.scoreChange);
        int scoreChange = Integer.parseInt(score.getScoreChange());
        if (scoreChange > 0)
            txtScoreChange.setText("+" + scoreChange);
        else if (scoreChange < 0)
            txtScoreChange.setText("" + scoreChange);
        else
            txtScoreChange.setText("");

        TextView txtScoreTotal = (TextView) convertView
                .findViewById(R.id.scoreTotal);
        txtScoreTotal.setText(score.getScoreTotal());

        final LinearLayout currentRow = (LinearLayout) convertView
                .findViewById(R.id.scoreRowLayout);                 

        notifyDataSetChanged();
        return convertView;
    }   
}
like image 252
GrilledCheese Avatar asked Oct 05 '22 01:10

GrilledCheese


1 Answers

Create an instance of your custom adapter, so you can use it anywhere you like...

public class ScoreList extends SherlockFragmentActivity {

private ListView listViewScore;

private ScoreListAdapter adapter;

static List<Score> listScore = new ArrayList<Score>();
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.score_list);
    ctx = this;
    listScore = dbh.getAllScores();

    listViewScore = (ListView) findViewById(R.id.score_list);

    adapter = new ScoreListAdapter(ctx, R.layout.score_row_item, listScore);
    listViewScore.setAdapter(adapter);
    adapter.notifyDatasetChanged(); 
}
}

By the way, if your listScore array is already loaded, then you do not need to use

adapter.notifyDatasetChanged(); 
like image 182
yahya Avatar answered Oct 10 '22 11:10

yahya