【发布时间】:2013-06-06 11:04:15
【问题描述】:
我的列表视图有一个问题,当我将任务添加到我的数据库时,我需要用这个新添加的任务更新我的列表视图....
我是 android 和 Eclipse 的新手...
这是我的 Main 代码(显示 ListView)
public class Main extends ListActivity {
Button newCat;
TextView todaytaskId;
ResultDatabase controller5 = new ResultDatabase(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
newCat = (Button) findViewById(R.id.bNewCat);
newCat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent nextScreen = new Intent(getApplicationContext(),
CategoryList.class);
startActivity(nextScreen);
}
});
ArrayList<HashMap<String, String>> todaytaskList = controller5
.getTodayTasks();
if (todaytaskList.size() != 0) {
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
todaytaskId = (TextView) view
.findViewById(R.id.todaytaskId);
String valtaskId = todaytaskId.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),
DelayTask.class);
objIndent.putExtra("todaytaskId", valtaskId);
startActivity(objIndent);
}
});
SimpleAdapter adapter = new SimpleAdapter(Main.this, todaytaskList,
R.layout.view_today_task, new String[] { "taskId",
"taskName", "taskTime" }, new int[] {
R.id.todaytaskId, R.id.todaytasktv,
R.id.todaytasktimetv});
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
这是我从 ResultDatabase 类中选择任务的代码
public ArrayList<HashMap<String, String>> getTodayTasks() {
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM tasks where taskDate = date('now') AND taskDone = 'No'";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("taskId", cursor.getString(0));
map.put("taskName", cursor.getString(1));
map.put("taskTime", cursor.getString(3));
// map.put("taskDate", cursor.getString(4));
wordList.add(map);
} while (cursor.moveToNext());
}
return wordList;
}
【问题讨论】:
-
你为什么评论 NotifyDataSetChanged();?调用更新任务后尝试调用 adapter.NotifyDataSetChanged()
-
我认为 NotifyDataSetChanged();仅与 ArrayAdapter 一起使用
-
当我将 listadapter 与 NotifyDataSetChanged() 一起使用时,我收到错误消息,指出没有使用此名称的方法与列表适配器一起使用
-
将适配器声明为 SimpleAdapter 的一个实例,而不是 ListAdapter。然后你今天调用你的列表taskList.setAdapter(adapter)。
-
你能解释一下什么样的错误吗?运行时,编译时?您不可能调用此方法,因为您可以看到它存在于 SimpleAdapter developer.android.com/reference/android/widget/… 中。
标签: android eclipse android-listview refresh simpleadapter