ThalyssonVieira
ThalyssonVieira

Reputation: 11

How refresh a listView inside SherlockFragment?

I have a listView inside a SherlockFragment, but I can not update the listView on screen when I make any changes to the Adapter. I tried: adapter.notifyDataSetChanged(), but without success. Thank's.

Upvotes: 0

Views: 638

Answers (2)

James W
James W

Reputation: 410

You should be updating the underlying dataset that is passed to the adapter before calling notifyDatasetChanged();

EG:

For ArrayAdapter in a ListActivity ("arraylist" is the ArrayList you've used to back your ArrayAdapter)

arraylist.add(data);
arrayadapter = this.getListAdapter();
arrayadapter.notifyDatasetChanged();

Upvotes: 1

Stefano Munarini
Stefano Munarini

Reputation: 2717

Personally, everytime user like press refresh button i repopulate listView initializing Cursor again.

Like this: calling this function...

public void repopulateListView(){

cursor = dbHelper.fetchAll();
        columns = new String[] {                        
                DBAdapter.KEY_NAME,
                DBAdapter.KEY_DATE,
                DBAdapter.KEY_VOTE,
                DBAdapter.KEY_CREDIT
        };
        to = new int[] {
                R.id.one,
                R.id.two,
                R.id.three,
                R.id.four
        };

        dataAdapter = new SimpleCursorAdapter(
                getActivity(), R.layout.YOUR_ID,
                cursor,
                columns,
                to,
                0)
        {
            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                final View row = super.getView(position, convertView, parent);

            }
        }
        }

...from Refresh onClick:

 @Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.refresh:{
            repopulateListView();
            break;
        }
    }
 }

Upvotes: 0

Related Questions