刷新的ListView与BaseAdapter(Refresh ListView with Base

2019-10-29 03:47发布

我有一个删除按钮一个ListView上时按下按钮,从数据删除的项目(包含HashMap的ArrayList),并从ListView的解雇过的每个item.I希望。

这里是我的代码:

BaseAdapter:

public class MyCustomAdapter extends BaseAdapter{

 private ArrayList<HashMap<String, Object>> list;

 private Context context;

 public MyCustomAdapter(ArrayList<HashMap<String, Object>> list,Context  context){

    this.list = list;
    this.context = context;
}


@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

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

    View view = convertView;

    if(view == null){

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view = inflater.inflate(R.layout.addresslist_item, null);



        TextView address = (TextView) view.findViewById(R.id.listtxtaddress);

        String add = String.valueOf(list.get(position).get("address"));

        address.setText(add);

        Button btndelete = (Button) view.findViewById(R.id.listbtndelete);

        btndelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.d("check", "item_positiona:"+position);
                Log.d("check", "lista:"+list.toString());

                AlertDialog.Builder builder = new AlertDialog.Builder(context);

                builder.setTitle("Τιτλος επιχειρησησης");

                builder.setMessage("Θέλετε να διαγάρψετε αυτή τη διεύθυνση;");

                builder.setNegativeButton("NAI", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();

                        Log.d("check", "item_positionb:"+position);


                        list.remove(position);

                        notifyDataSetChanged();


                    }
                });

                builder.setPositiveButton("AKYPO", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();

                    }
                });

                AlertDialog abc = builder.create();

                abc.show();



            }
        });


    }

    return view;


}
}

我的问题是,该项目是从数据删除,但在ListView控件是只更新了最后一个项目被删除,而不是项目,我select.Any想法,为什么出现这种情况? 提前致谢。

Answer 1:

在之后ArrayList已经改变,你必须调用notifyDataSetChanged()来更新您ListView与变化。 这可以内部或外部的适配器来完成。 因此,举例来说:

public class MyCustomBaseAdapter extends BaseAdapter {
    //TIP: Don't make this static, that's just a bad idea
    private ArrayList<Contact> searchArrayList;

    private LayoutInflater mInflater;

    public MyCustomBaseAdapter(Context context, ArrayList<Contact> initialResults) {
        searchArrayList = initialResults;
        mInflater = LayoutInflater.from(context);
    }

    public void updateResults(ArrayList<Contact> results) {
        searchArrayList = results;
        //Triggers the list update
        notifyDataSetChanged();
    }
}


Answer 2:

调用notifyDataSetChanged()适配器对象上,一旦你已经修改了该数据应刷新列表视图。

你可以试试这个 -

创建您的适配器内部的专用类ViewHolder。 这个类应该包含所有的列表项有意见。

private static class ViewHolder {
    TextView address;
    String add;
    Button btndelete;
}

然后里面getView():

        ViewHolder viewHolder;
       if (convertView == null) {     
                viewHolder = new ViewHolder();
                LayoutInflater layoutInflater = LayoutInflater.from(mContext);
                convertView = layoutInflater.inflate(R.layout.addresslist_item, null);
                viewHolder.address =  (TextView) view.findViewById(R.id.listtxtaddress);
                viewHolder.add = String.valueOf(list.get(position).get("address"));
                viewHolder.address.setText(add);

            viewHolder.btndelete = (Button) view.findViewById(R.id.listbtndelete);
                convertView.setTag(viewHolder);
    }
     else 
   {                


    viewHolder = (ViewHolder) convertView.getTag();

   } 
    viewHolder.btndelete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    Log.d("check", "item_positiona:"+position);
                    Log.d("check", "lista:"+list.toString());

                    AlertDialog.Builder builder = new AlertDialog.Builder(context);

                    builder.setTitle("Τιτλος επιχειρησησης");

                    builder.setMessage("Θέλετε να διαγάρψετε αυτή τη διεύθυνση;");

                    builder.setNegativeButton("NAI", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            dialog.dismiss();

                            Log.d("check", "item_positionb:"+position);


                            list.remove(position);

                            notifyDataSetChanged();


                        }
                    });




    builder.setPositiveButton("AKYPO", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();



                    }
                });

                AlertDialog abc = builder.create();

                abc.show();



            }
        });


    }
    }
    return convertView;
    }


Answer 3:

你可以试试这个方法也..创建监听接口

package xyz.listeners;

public interface DeleteClickListener {

    public void onDeleteClick(int position);

}

比你actvity /片段具有listview-那

private DeleteClickListener mDeleteClickListener;
mDeleteClickListener = new DeleteClickListener() {

            @Override
            public void onDeleteClick(int position) {

     list.remove(position);
adapter.notifyDataSetChanged();
            }
        };

在您的适配器类构造器传递监听器。

public MyCustomAdapter(ArrayList<HashMap<String, Object>> list,Context  context,DeleteClickListene mDeleteClickListener){

    this.list = list;
    this.context = context;
    deleteClickListener=mDeleteClickListener;

}

然后删除按钮的onClick

builder.setNegativeButton("NAI", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                              deleteClickListener.onDeleteClick(position);




                            }
                        });


文章来源: Refresh ListView with BaseAdapter