简体   繁体   English

在adapter.notifyDataSetChanged()之后,Listview不会更新

[英]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.

相关问题 数据库更新和适配器后,列表视图未更新。notifyDataSetChanged(); - Listview not updating after database update and adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged 不刷新回收站视图 - adapter.notifyDataSetChanged does not refreshing the recycler view Android-调用Adapter.notifyDataSetChanged和ListView.setSelection后访问ListView中的视图 - Android - Accessing View in ListView after call Adapter.notifyDataSetChanged and ListView.setSelection android listview:adapter.add(item)?或者adapter.notifyDataSetChanged() - android listview: adapter.add(item) ? or adapter.notifyDataSetChanged() android ListView适配器方法getViewTypeCount()在adapter.notifyDataSetChanged()时不触发 - android ListView adapter method getViewTypeCount() not fire when adapter.notifyDataSetChanged() Adapter.notifyDataSetChanged()不起作用 - Adapter.notifyDataSetChanged() is not working fragment中的adapter.notifyDataSetChanged() - adapter.notifyDataSetChanged() in fragment 适配器的 NullPointerException.notifyDataSetChanged? - NullPointerException for adapter.notifyDataSetChanged? Android ListView-adapter.notifyDataSetChanged和使用新适配器之间的区别 - Android ListView - difference between adapter.notifyDataSetChanged and using new adapter adapter.notifyDataSetChanged(); 不工作 - adapter.notifyDataSetChanged(); not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM