当我删除所有项目时,我正在尝试刷新ListView。调用delete函数时,将清除sqlite表。这部分工作正常。但我无法刷新listView。我尝试了notifyDataSetChanged()和invalidate(),但没有任何作用。
有什么想法吗?
public class ResultsAdapter
extends BaseAdapter {
private LayoutInflater inflater;
private ResultsTypeOneOpenHelper resultsTypeOne_db;
private ResultsTypeTwoOpenHelper resultsTypeTwo_db;
private ResultsTypeThreeOpenHelper resultsTypeThree_db;
private ArrayList<Object> results;
static class ViewHolder {
TextView text;
}
public ResultsAdapter(Context context) {
this.inflater = LayoutInflater.from(context);
resultsTypeOne_db = new ResultsTypeOneOpenHelper(context);
resultsTypeTwo_db = new ResultsTypeTwoOpenHelper(context);
resultsTypeThree_db = new ResultsTypethreeOpenHelper(context);
results = new ArrayList<Object>();
/* get results from sqlite */
if (resultsTypeOne_db.getRowCount() != 0) {
results.addAll( resultsTypeOne_db.getAllTypeOneResults() );
}
if (resultsTypeTwo_db.getRowCount() != 0) {
results.addAll( resultsTypeTwo_db.getAllTypeTwoResults() );
}
if (resultsTypeThree_db.getRowCount() != 0) {
results.addAll( resultsTypeThree_db.getAllTypeThreeResults() );
}
}
@Override
public int getCount() {
return results.size();
}
@Override
public Object getItem(int position) {
Object item = results.get(position);
return item;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder;
if (rowView == null) {
rowView = inflater.inflate(R.layout.results_list_row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.results_list_textView);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.text.setText( results.get(position).toString() );
return rowView;
}
}
答案 0 :(得分:1)
您可以再次尝试拨打setListAdapter
上的ListActivity
。删除所有项目后,如果您使用setListAdapter
而不是ListActivity
,请在setAdapter
上调用ListView
/ Activity
方法的ListActivity
方法
答案 1 :(得分:0)
删除数据后,首先清除ArrayList,然后再次获取新数据并将其存储在ArrayList中,然后使用ListView方法setListAdapter(adapter)
答案 2 :(得分:0)
在你的情况下。在ResultsAdapter
类中创建方法,如:
public void refreshadapter(Context context){
resultsTypeOne_db = new ResultsTypeOneOpenHelper(context);
resultsTypeTwo_db = new ResultsTypeTwoOpenHelper(context);
resultsTypeThree_db = new ResultsTypethreeOpenHelper(context);
results.clear();
/* get results from sqlite */
if (resultsTypeOne_db.getRowCount() != 0) {
results.addAll( resultsTypeOne_db.getAllTypeOneResults() );
}
if (resultsTypeTwo_db.getRowCount() != 0) {
results.addAll( resultsTypeTwo_db.getAllTypeTwoResults() );
}
if (resultsTypeThree_db.getRowCount() != 0) {
results.addAll( resultsTypeThree_db.getAllTypeThreeResults() );
}
}
从您的活动中调用此方法
你需要调用此方法的对象,当你执行setAdapter()
所以,请使用..
reslutAdapter.refreshadapter(myActivity.this);
reslutAdapter.notifyDataSetChanged();