[英]Listview does not update after adapter.notifyDataSetChanged()
I have this onCreateView
in a fragment. 我在片段中有此
onCreateView
。 It contains a listview that is being fed by ListViewAdapterNahrung
. 它包含由
ListViewAdapterNahrung
馈送的listview。 My problem is that this listview does not update after adapter.notifyDataSetChanged();
我的问题是,此列表视图在
adapter.notifyDataSetChanged();
之后不会更新adapter.notifyDataSetChanged();
. 。
public class NahrungFragment extends Fragment {
private ArrayList<HashMap<String, String>> list;
HashMap<String,String> temp;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_nahrung, container, false);
list=new ArrayList<HashMap<String,String>>();
temp=new HashMap<String, String>();
temp.put("Bezeichnung", "Bezeichnung");
list.add(temp);
final ListViewAdapterNahrung adapter=new ListViewAdapterNahrung(getActivity(), list);
listView.setAdapter(adapter);
etBez.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
list=new ArrayList<HashMap<String,String>>();
Cursor c=mydatabase.rawQuery("SELECT * FROM nahrung WHERE [bezeichnung] LIKE '%"+etBez.getText().toString()+"%' ", null);
if(c.getCount()>0)
{
while(c.moveToNext())
{
temp=new HashMap<String, String>();
temp.put("Bezeichnung", c.getString(0));
list.add(temp);
}
adapter.notifyDataSetChanged();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
return v;
}
}
This is ListViewAdapterNahrung
: 这是
ListViewAdapterNahrung
:
public class ListViewAdapterNahrung extends BaseAdapter {
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView tvBezeichnung;
private static final String FIRST_COLUMN="Bezeichnung";
public ListViewAdapterNahrung(Activity activity, ArrayList<HashMap<String, String>> list){
super();
this.activity=activity;
this.list=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=activity.getLayoutInflater();
if(convertView == null){
convertView=inflater.inflate(R.layout.listview_nahrung, null);
tvBezeichnung=(TextView) convertView.findViewById(R.id.tvName);
}
HashMap<String, String> map=list.get(position);
tvBezeichnung.setText(map.get("Bezeichnung"));
return convertView;
}
}
Use list.clear()
instead of reassignment to remove data 使用
list.clear()
而不是重新分配来删除数据
However, you should ideally use a CursorAdapter with a Database 但是,理想情况下,应将CursorAdapter与数据库一起使用
You are not passing the data to your adapter, that's why you do not see the data updated. 您没有将数据传递到适配器,这就是为什么看不到数据更新的原因。 You have to pass the data to your adapter before call
adapter.notifyDataSetChanged();
您必须在调用
adapter.notifyDataSetChanged();
之前将数据传递给适配器adapter.notifyDataSetChanged();
Try this: 尝试这个:
...
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
list=new ArrayList<HashMap<String,String>>();
Cursor c=mydatabase.rawQuery("SELECT * FROM nahrung WHERE [bezeichnung] LIKE '%"+etBez.getText().toString()+"%' ", null);
if(c.getCount()>0)
{
while(c.moveToNext())
{
temp=new HashMap<String, String>();
temp.put("Bezeichnung", c.getString(0));
list.add(temp);
}
adapter.updateData(list);
adapter.notifyDataSetChanged();
}
}
,,,,
And in your adapter: 并在您的适配器中:
...
public void updateData(ArrayList<HashMap<String,String>> data){
this.list.addAll(data);
}
...
Or if you want to set all new data : 或者,如果您想设置所有新数据:
...
public void updateData(ArrayList<HashMap<String,String>> data){
this.list.clear();
this.list.addAll(data);
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.