如何从列表视图重新加载 activity 以更新数据库信息?
How can I reload an activity from a listview to update information of a database?
我正在尝试实现这样的东西:
我有一个主要的 class 布局,从那里我从数据库中获取一些数据并如图所示绘制它(使用另一个布局 complex_list):
public class main_class extends Activity{
DatabaseHelper myDB;
ListView list;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
myDB=new DatabaseHelper(this);
list = (ListView) findViewById(R.id.list);
Cursor res = myDB.getAllData();
if (res.getCount()==0){
Toast.makeText(this,R.string.nothing_found,Toast.LENGTH_LONG).show();
}
else{
...
list.setAdapter(new listAdapter(..., this.getBaseContext()));
}
我有一个 class 来实现 BaseAdapter:
class listAdapter extends BaseAdapter {
public View getView(final int position, View convertView, final ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View row;
row = inflater.inflate(R.layout.complex_list, parent, false);
...
Delete = (Button) row.findViewById(R.id.delete_entry_list);
...
Delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteEntry(id[position]);
Intent refresh = new Intent(context, main_class.class);
refresh.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //I've added this line because otherwise I get an error an the app crashes.
context.startActivity(refresh);
//((Activity)context).finish();
}
});
return (row);
}
public void deleteEntry ( int id){
DatabaseHelper myDB = new DatabaseHelper(context);
myDB.deleteData(Integer.toString(id));
}
我想做的是在按下删除按钮后刷新整个 activity 以查看数据库的更新版本。
我写的确实有效,但是当我按下 phone 中的 return 按钮时,它 return 会转到数据库的旧图像,而不是转到我访问此 activity.
的菜单
我一直在研究类似的问题,我找到的唯一解决方案是添加注释行:
//((Activity)context).finish();
但它使我的应用程序因以下错误而崩溃:
java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity
谁能告诉我我做错了什么,或者是否有更简单的方法?
ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity
可能从
开始
list.setAdapter(new listAdapter(..., this.getBaseContext()));
您应该使用 this
代替 this.getBaseContext()
,因为 Activity extends Context
我正在尝试实现这样的东西:
我有一个主要的 class 布局,从那里我从数据库中获取一些数据并如图所示绘制它(使用另一个布局 complex_list):
public class main_class extends Activity{
DatabaseHelper myDB;
ListView list;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
myDB=new DatabaseHelper(this);
list = (ListView) findViewById(R.id.list);
Cursor res = myDB.getAllData();
if (res.getCount()==0){
Toast.makeText(this,R.string.nothing_found,Toast.LENGTH_LONG).show();
}
else{
...
list.setAdapter(new listAdapter(..., this.getBaseContext()));
}
我有一个 class 来实现 BaseAdapter:
class listAdapter extends BaseAdapter {
public View getView(final int position, View convertView, final ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View row;
row = inflater.inflate(R.layout.complex_list, parent, false);
...
Delete = (Button) row.findViewById(R.id.delete_entry_list);
...
Delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteEntry(id[position]);
Intent refresh = new Intent(context, main_class.class);
refresh.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //I've added this line because otherwise I get an error an the app crashes.
context.startActivity(refresh);
//((Activity)context).finish();
}
});
return (row);
}
public void deleteEntry ( int id){
DatabaseHelper myDB = new DatabaseHelper(context);
myDB.deleteData(Integer.toString(id));
}
我想做的是在按下删除按钮后刷新整个 activity 以查看数据库的更新版本。
我写的确实有效,但是当我按下 phone 中的 return 按钮时,它 return 会转到数据库的旧图像,而不是转到我访问此 activity.
的菜单我一直在研究类似的问题,我找到的唯一解决方案是添加注释行:
//((Activity)context).finish();
但它使我的应用程序因以下错误而崩溃:
java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity
谁能告诉我我做错了什么,或者是否有更简单的方法?
ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity
可能从
开始list.setAdapter(new listAdapter(..., this.getBaseContext()));
您应该使用 this
代替 this.getBaseContext()
,因为 Activity extends Context