请分享如何在doinbackground()或onpostexecute()方法中使用intent Asynctask类.当我尝试使用这些代码时,它会显示错误.
Intent intent = new Intent(asynctask.this, home.class);
startActivity(intent);
finish();
private Class<Home> clazz;
public asynctask(Class<Home> clazz){
this.clazz = clazz;
}
Run Code Online (Sandbox Code Playgroud)
Asynctask doInBackground()方法:
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, clazz);
startActivity(intent);
finish();
Toast.makeText(cxt, "welcome", Toast.LENGTH_SHORT).show();
return null;
}
Run Code Online (Sandbox Code Playgroud) 我希望清除所有哈希映射值,然后将值从db分配给哈希映射.例如,hashmap<string,string> demo我的hashmap是我从db添加了以下值.来自hashmap的结果响应是.After {A=1, B=2, C=3, D=4, E=5}视图加载后我想要在上面的hashmap中插入这个{A=test, B=demo, C=sample, D=empty, E=null}.为此我如何在插入第二组值之前清除第一个值.而且我尝试使用demo.clear()它不起作用.如何在添加第二个值之前清除第一个值列表.提出一些解决方案来解决问题.
这是从db检索数据的代码.
public HashMap<String, String> getdata() {
try {
HashMap<String, String> map = new HashMap<String, String>();
Log.e("getdata", "Started");
String selectQuery = "SELECT * FROM tablename";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
map.put(cursor.getString(cursor.getColumnIndex("colmn1")),
cursor.getString(cursor.getColumnIndex("colmn2")));
} while (cursor.moveToNext());
}
cursor.close();
return map;
} catch (Exception e) {
e.printStackTrace();
Log.e("getdata", "Ended");
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
在这个演示中,只有我上面解释的响应.然后我正在做这个
if(demo.isEmpty())
{ …Run Code Online (Sandbox Code Playgroud)