Maxime
Maxime

Reputation: 3165

Update row content in android ListView

I have a ListView and I need to update the content of specifics rows without reload all the ListView content.This ListView can be big.

For example (for do it simple) I need to change the content of a TextView (label3 in the adapter) located in a view (the row) at the 'x' position of the adapter. Actually I work with a TouchListView (Commonsware) adapter.

class IconicAdapter extends ArrayAdapter<FavoriteObject> {
    IconicAdapter() {
        super(FavoritesMainActivity.this, R.layout.favbusrow, array);
    }

    public View getView(int position, View convertView,ViewGroup parent) {
        View row=convertView;
        LayoutInflater inflater=getLayoutInflater();

            ListFav lf = (listFav) array.get(position).getContent();

            row=inflater.inflate(R.layout.favrow,parent, false);
            row.setBackgroundColor(Color.WHITE);

            TextView label=(TextView)row.findViewById(R.id.busNameF);
            label.setText(lf.getName());
            label.setTextColor(Color.BLACK);
            label.setTypeface(FontsManager.getBoldFont());

            TextView label2 =(TextView)row.findViewById(R.id.busNumberF);
            label2.setText(lf.getNumber());
            label2.setTextColor(Color.BLACK);
            label2.setTypeface(FontsManager.getBoldFont());

            TextView label3 =(TextView)row.findViewById(R.id.busDirectionF);
            label3.setText(lf.getQuickName());
            label3.setTextColor(Color.BLACK);
            label3.setTypeface(FontsManager.getRegularFont());

        return(row);
    }
}

Someone have an idea?

Thanks!

Upvotes: 3

Views: 354

Answers (2)

CommonsWare
CommonsWare

Reputation: 1007584

I need to change the content of a TextView (label3 in the adapter) located in a view (the row) at the 'x' position of the adapter.

That position may or may not be in view. You can use getFirstVisiblePosition() and getLastVisiblePosition() to see if it is, then calculate the child index and use getChildAt() to retrieve the row.

You also need to ensure that your data model reflects this change, so as the user scrolls, they do not "lose" whatever direct-to-UI modification you make.

Upvotes: 1

Steve Bergamini
Steve Bergamini

Reputation: 14600

Update the data set within your adapter to reflect your changes and then call notifyDataSetChanged() on your adapter.

Upvotes: 2

Related Questions