我有一个扩展Activity的A类。从A类开始,我调用了适配器类B,它扩展了BaseAdapter。
我正在覆盖方法中显示ListView数据 public View getView(final int position,View convertView,ViewGroup parent)
我从SQLite获取ListView的数据。现在单击特定列表项的取消按钮,我可以从数据库中删除记录数据,但无法刷新视图。仍在显示数据。但是,如果我再次从主屏幕导航到屏幕,则数据未显示,表示数据库没有问题,只有更新显示才是问题。
骨架代码如下:
class A extends Activity {
B adapter ;
ListView list ;
public void onCreate(Bundle savedInstanceState) {
list = (ListView) findViewById(R.id.textList);
// I am calling the adapter
adapter = new B(this, txtStr);
list.setAdapter(adapter);
}
class B extends BaseAdapter{
public View getView(final int position, View convertView, ViewGroup parent){
// on click of the delete button , record is delated for a specific
// listitem & the view needs to be updated , but I am unable to update the view
holder.deleteImage.setOnClickListener(new OnClickListener() {
private int pos = position;
public void onClick(View v) {// (position)) -- what was this doing here? (dbreslau)
sampleDB.execSQL("DELETE FROM " + Constants.TABLE_NAME + " where id = 1");
adapter.notifyDataSetChanged();
}
});
}
}
}
即使在调用notifyDataSetChanged之后,视图也没有得到更新。 如果有人已经实现了同样的帮助,请提供帮助。
答案 0 :(得分:1)
在活动类中声明你的onClickListener,因为你正在为每个构建的视图创建一个新的监听器,然后在onClickListener onClick方法中执行此操作:
class A extends Activity {
B adapter ;
ListView list ;
public void onCreate(Bundle savedInstanceState) {
list = (ListView) findViewById(R.id.textList);
// I am calling the adapter
adapter = new B(this, txtStr);
list.setAdapter(adapter);
list.setOnItemClickListener (listener);
}
private OnItemClickListener listener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
sampleDB.execSQL("DELETE FROM " + Constants.TABLE_NAME + " where id = 1");
//adapter.notifyDataSetChanged();
((B)adapter).notifyDataSetChanged();
}
};
**EDIT 1**
//with an onCLickListener...
private OnClickListener deleteListener = new OnClickListener() {
@Override
public void onClick(View v) {
int position = (int)v.getTag();
sampleDB.execSQL("DELETE FROM " + Constants.TABLE_NAME + " where id = 1");
((B)adapter).notifyDataSetChanged());
}
};
class B extends BaseAdapter{
public View getView(final int position, View convertView, ViewGroup parent){
// on click of the delete button , record is delated for a specific
// listitem & the view needs to be updated , but I am unable to update the view
holder.deleteImage.setTag(position);
holder.deleteImage.setOnClickListener(deleteListener);
//...
convertView.setTag(holder);
}
}
}