我知道我被问了很多,但我就是不能让它工作。
我有一个 listView,我正在尝试更新它..
编辑:所有代码,也许我在其他地方出错了。
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();
}
});
}
更新列表()
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();
}
});
}
我尝试了很多不同的方法,但似乎都没有奏效。
提前谢谢。