1

I know i has been asked a lot, but i just can't make this to work.

I have a listView and i'm trying to update it..

edit: All the code, maybe i got somewhere else wrong.

public class TestActivity extends Activity {
private TasksDataSource dataSource;
private ArrayAdapter<Task> adapter;
private ListView list;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    list = (ListView) findViewById(R.id.list);

    Button btnAdd = (Button) findViewById(R.id.btnAdd);
    Button btnRefresh = (Button) findViewById(R.id.btnRefresh);
    Button btnClear = (Button) findViewById(R.id.btnClear);

    dataSource = new TasksDataSource(this);
    dataSource.open();
    List<Task> tasks = dataSource.getAllTasks();

    adapter = new ArrayAdapter<Task>(this, android.R.layout.simple_list_item_1, tasks);
    list.setAdapter(adapter);


    btnAdd.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent startAddIntent = new Intent(TestActivity.this, AddActivity.class);
            TestActivity.this.startActivity(startAddIntent);
        }
    });

    btnRefresh.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            updateList();

        }
    });
    btnClear.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            dataSource.deleteAll();
        }
    });
}

the updateList()

public void updateList(){

    runOnUiThread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            adapter.notifyDataSetChanged(); 
            list.refreshDrawableState(); 
            list.invalidate(); 
            Toast.makeText(TestActivity.this, "AHHHH", Toast.LENGTH_SHORT).show();
        }
    });
}

I tried a lot of different approches, but none seems to work.

Tks in advance.

2
  • 1
    What makes you think it didn't work? Did your dataset change? Commented May 7, 2012 at 14:00
  • It should. Do i have to update it somewhere? I edited the post, including the dataSource code. Commented May 7, 2012 at 14:11

2 Answers 2

2

try

public void updateList(){

    runOnUiThread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            adapter.notifyDataSetChanged();
            list.refreshDrawableState();
            list.invalidate();
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Calling requery() on a database or content provider Cursor attached to ListView via a CursorAdapter automatically refreshes the list
1

Both

notifyDataSetChanged();

and

invalidate()

should update your view.

The only time I have seen this not work, is because of a delay in processing the notify/invalidate message. If there are several queued messages running on the UI thread, they have to be processed in a serial fashion. There is only one UI thread that all UI operations have to work on. Make sure you are not waiting for other things to finish on the UI thread and your code should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.