1

在此处输入图像描述

在列表项中,有一个“删除”按钮和文本视图。单击列表项中的删除按钮时,我正在删除该特定列表项。但我的问题是:删除按钮在开始时和用户单击时是不可见的在屏幕顶部下方显示的“编辑”按钮上,我想让所有按钮从不可见变为可见..但我不知道如何实现..

ListViewAdapter.java:

public class ListViewAdapter extends BaseAdapter {

    public ArrayList<String> list;
    Context context;
    private LayoutInflater mInflater;

    int count = 0;
    static int list_size;

    public ListViewAdapter(Context context, ArrayList<String> list) {
        super();
        this.context = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
        list_size = list.size();
    }

    public int getCount() {
        if (list == null) {
            return 0;
        } else {
            return list_size;
        }
    }

    @Override
    public Object getItem(int position) {
        Log.i("Calling getItem()", "getItem()");
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        Log.i("Calling getItemId()", "getItemId()");
        return position;
    }

    static class ViewHolder {
        TextView notes_text;

        static Button delete;
    }

    //  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i("ListViewAdapter is calling...", "calling...");
        ViewHolder holder;
        Log.i("View to be converted", "" + convertView);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.simplerow, null);
            Log.i("View to be converted", "" + convertView);
            holder = new ViewHolder();
            holder.notes_text = (TextView) convertView.findViewById(R.id.rowTextView);

            holder.delete = (Button) convertView.findViewById(R.id.btn_minus);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.notes_text.setText((String) list.get(position));

        holder.delete.setTag((Integer) position);

        holder.delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Integer myIntegerObject = new Integer((Integer) view.getTag());
                Log.i("Integer postioton", "" + myIntegerObject.intValue());

                list.remove(list.indexOf(list.get(myIntegerObject.intValue())));

                list_size = list.size();

                notifyDataSetChanged();
            }
        });

        return convertView;
    }
}
4

1 回答 1

4

保留一个布尔标志以指示编辑模式。In将可见性设置为或取决于您的布尔标志ListAdaptergetView()ButtonVISIBLEGONE

现在,要切换所有按钮的可见性,您需要更改布尔标志,并调用notifyDataSetChanged()适配器。这将重新绘制所有可见的列表项。

在适配器类中添加变量:

boolean editmode = false;

getView()

holder.delete.setVisibility( (editmode ? View.VISIBLE : View.GONE ) );

最后为您的适配器类添加一个方法:

public void setEditMode(boolean value){
  editmode = value;
  this.notifyDataSetChanged();
}
于 2012-10-11T13:03:20.117 回答