我创建了一个ListView,其中包含一行,而该行又包含文本和按钮。 我们的想法是让按钮用作删除按钮以删除行 从列表和数据库。
为了做到这一点,我创建了一个适配器来处理按钮点击。这段代码如下。 删除数据库记录工作正常,但我还没有成功刷新 ListView所以不再显示记录。
公共类FeedArrayAdapter扩展了ArrayAdapter {
private ARssEReaderDBAdapter dba;
private String TAG = "FeedArrayAdapter";
private View v;
private ListView feedList;
private OnClickListener btnDeleteFeedListener = new OnClickListener(){
public void onClick(View pView) {
dba = new ARssEReaderDBAdapter(getContext());
Integer objInt = (Integer) pView.getTag();
dba.open();
dba.deleteFeed(objInt);
dba.close();
Log.w(TAG, "Database row, " + pView.getTag() + " got clicked.");
}
};
public FeedArrayAdapter(Context context, int textViewResourceId, List<Feed> items) {
super(context, textViewResourceId, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
Log.w(TAG, "getView");
v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.feedlistrow, null);
}
Feed feed = getItem(position);
if (feed != null){
v.setTag(feed.getFeedId());
TextView title = (TextView)v.findViewById(R.id.TextView01);
if (title != null){
title.setText(feed.getTitle());
}
Button btnDelete = (Button)v.findViewById(R.id.btnDelete);
btnDelete.setTag(feed.getFeedId());
btnDelete.setOnClickListener(btnDeleteFeedListener); //
}
return v;
}
}
非常感谢任何帮助。
理查德
答案 0 :(得分:0)
也许你正在寻找notifyDataSetChanged()
。确保从UI线程中调用它。
public void notifyDataSetChanged ()
Since: API Level 1
Notifies the attached View that the underlying data has been changed and it should refresh itself.