简体   繁体   English

自定义适配器不刷新android中onclicklistener的listview

[英]custom adapter do not refresh the listview for onclicklistener in android

I delete database entry using onclicklistener but it is not refreshing the listview. 我使用onclicklistener删除了数据库条目,但它没有刷新listview。 how can i refresh this listview? 我如何刷新此列表视图?

This is main class for listview: 这是listview的主要类:

public class AFragment extends Fragment implements OnItemClickListener {

    protected static final String file_name ="user";
    ListView list;
    Database entry;
    View v;
    String values[];
    MySimpleArrayAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        SharedPreferences settings = getActivity().getSharedPreferences(file_name, 0);
        String name =  settings.getString("name", null);


        entry = new Database(getActivity());
        entry.open();
        values=entry.planlist(name);
        entry.close();
        if(values.length>0){
            v = inflater.inflate(R.layout.activity_afragment, container,false);
            adapter = new MySimpleArrayAdapter(getActivity(), values);
            list=(ListView)v.findViewById(R.id.list);

            list.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            list.setOnItemClickListener(this);


        }else{
            v = inflater.inflate(R.layout.activity_my_tabs_listener, container,false);
        }
//      Toast.makeText(getActivity(),String.valueOf(values.length), Toast.LENGTH_LONG).show();
        return v;
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Intent i = new Intent(getActivity(),Details.class);
        i.putExtra("sub", values[arg2]);
        startActivity(i);
        Toast.makeText(getActivity(), "clicked", Toast.LENGTH_SHORT).show();
    }

}


Here i use onclicklistener to delete data from database but it is not refreshing:

public class MySimpleArrayAdapter extends ArrayAdapter<String>  {

      private final Context context;
      private final String[] values;
      public Business aFragment = new Business();
      int mypos =0;
      ViewHolder holder;
      View row;
      public MySimpleArrayAdapter(Context context, String[] values) {
        super(context,R.layout.activity_my_simple_array_adapter, values);
        this.context = context;
        this.values = values;
      }


      @Override
      public View getView(int position, View convertView, ViewGroup parent) {

        mypos = position;
        row = convertView;
        holder = new ViewHolder();
        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(R.layout.activity_my_simple_array_adapter, parent, false);

            TextView textView = (TextView) row.findViewById(R.id.text);
            Button btn = (Button) row.findViewById(R.id.button1);
            holder.tv = textView;
            holder.btn = btn;
            row.setTag(holder);
        }else{
            holder = (ViewHolder)row.getTag();
        }
        holder.tv.setText(values[position]);
        final int id = position;
        holder.btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Database entry = new Database(context);
                entry.open();
                entry.delete(values[id]);
                entry.close();
//              Toast.makeText(getContext(), String.valueOf(id), Toast.LENGTH_LONG).show();
            }
        });

        return row;
      }

    static class ViewHolder{

        TextView tv;
        Button btn;

    }
} 

as anil said, you should put notifyDataSetChanged(); 正如anil所说的那样,您应该放置notifyDataSetChanged();。 inside the onClickListener this basically tells the adapter to render the list again and will call getView() again for every visible item in the list, if your code crashes, you should check two things: first - debug the program and check that the new data fits what you want, in your case, check that the entry was deleted properly. 在onClickListener内部,这基本上告诉适配器重新渲染列表,并将再次为列表中的每个可见项调用getView(),如果代码崩溃,则应检查两件事:首先-调试程序并检查新数据适合您的情况,请检查条目是否已正确删除。

second - debug the getView method, step through each call and see what gives you the crash. 第二个-调试getView方法,逐步完成每个调用,看看是什么使您崩溃。

in your case the problem is that you are only updating the database, but in fact your listview data is taken from the values[] array which is not updated after you delete the database entry, you should create a function for updating it. 在您的情况下,问题在于您仅在更新数据库,但是实际上您的列表视图数据是从values []数组中获取的,在删除数据库条目后该数组不会更新,因此应该创建一个用于更新它的函数。

Put adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged(); on click of ListView 在单击ListView

@Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Database entry = new Database(context);
                entry.open();
                entry.delete(values[id]);
                entry.close();
                adapter.notifyDataSetChanged();

//              Toast.makeText(getContext(), String.valueOf(id), Toast.LENGTH_LONG).show();
            }

If you do lots of adding and deleting to the list I think you should do the things below. 如果您要对列表进行大量添加和删除操作,我认为您应该执行以下操作。

  • You should use ArrayList<String> instead of simple String[] so that you can easily delete. 您应该使用ArrayList<String>而不是简单的String[]以便可以轻松删除。 Database deletion do not effect the list directly unless you use Loaders 除非使用装载程序,否则数据库删除不会直接影响列表
  • After you delete and item from the list, you should call notifyDataSetChanged() to the adapter. 在从列表中删除项目之后,应该将notifyDataSetChanged()调用到适配器。 If you do not call this method, the list wont be updated. 如果您不调用此方法,则不会更新列表。

    adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();

you are removing it from the database but you are not removing it from the dataset that fills up your ListView . 您要从数据库中删除它,但不是要从填充ListView的数据集中删除它。 The simplest thing you can do is to change values from array to ArrayList, and since you are using an ArrayAdapter , you can call remove(int position) . 您可以执行的最简单的操作是将值从array更改为ArrayList,并且由于使用的是ArrayAdapter ,因此可以调用remove(int position) You need a List<T> of objects otherwise remove will throws an exception. 您需要对象的List<T> ,否则remove将引发异常。

You can do one thing. 你可以做一件事。

Firstly create a method called myAdapter(). 首先创建一个名为myAdapter()的方法。 In this put your creation of adapter code,so you can create new adapter for loading new data by simply calling myAdapter() method. 这样就创建了适配器代码,因此您可以通过简单地调用myAdapter()方法来创建用于加载新数据的新适配器。

Whenever there should be modification in your ListView just called the following code, 每当您在ListView中进行修改时,只需调用以下代码,

listview.invalidate();

Then simply call the myAdapter(). 然后只需调用myAdapter()。

That's it.Hope this is useful to you..:) 就是这样,希望这对您有用.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM