非静态方法可以从静态上下文中引用

时间:2015-11-11 03:06:11

标签: android

单击按钮时,我正在尝试删除该行。在最底部,我调用了我所做的删除方法。但它给了我一个错误(在问题中)。我尝试将删除方法设为静态,尝试了所有建议的

public class myAdapter extends RecyclerView.Adapter<myAdapter.ViewHolder> {

private List<numbers> mNumbers;

public myAdapter (List<numbers> numbers) {
    mNumbers = numbers;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v  = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_slot, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.bindNumbers(mNumbers.get(position));
}

@Override
public int getItemCount() {
    return mNumbers.size();
}

public void delete(int position) {
    mNumbers.remove(position);
    notifyItemRemoved(position);
}

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    TextView numberText;
    EditText checklistText;
    Button deleteButton;
    CheckBox checkDisBox;

    public ViewHolder(View itemView) {
        super(itemView);
        numberText = (TextView) itemView.findViewById(R.id.number);
        checklistText = (EditText) itemView.findViewById(R.id.editText);
        deleteButton = (Button) itemView.findViewById(R.id.deleteButton);
        checkDisBox = (CheckBox) itemView.findViewById(R.id.checkBox);

        deleteButton.setOnClickListener(this);
    }

    public void bindNumbers(numbers numbers) {
        numberText.setText(numbers.getInt() + "");
    }

    @Override
    public void onClick(View v) {
        delete(getAdapterPosition());
    }
}
}

以下编辑问题

    @Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.bindNumbers(mNumbers.get(position));
    holder.deleteButton.setOnClickListener(this);
}


public void delete(int position) {
    mNumbers.remove(position);
    notifyItemRemoved(position);
}

@Override
public void onClick(View v) {
    delete(getAdapterPosition());
}

2 个答案:

答案 0 :(得分:2)

修改

我自己去实现了这个,这对我有用。

创建删除项目的界面

public interface DeleteItem {
    void delete(int position);
}

使您的适配器实现此接口

public class myAdapter
    extends RecyclerView.Adapter<myAdapter.ViewHolder>
    implements DeleteItem {

    @Override
    public void delete(int position) {
        mNumbers.remove(position);
        notifyItemRemoved(position);
    }
}

将您的视图设置为参考接口

DeleteItem mDeleteItem;

public ViewHolder(View itemView, DeleteItem deleteItem) {
    super(itemView);
    mDeleteItem = deleteItem;
    ...        
}

在点击处理程序

上修改您的视图持有者
@Override
public void onClick(View v) {
    mDeleteItem.delete(getAdapterPosition());
}

最后,在创建视图持有者时,将参考文献传递给

ViewHolder vh = new ViewHolder(v, this);

原始答案

ViewHolder是为了保护您的应用程序不必查找列表项中的视图。它应该只包含myview = itemView.findViewById()类型的调用,而不包含任何其他内容。

bindViewHolder()方法应包含使用您在视图持有者中找到的那些视图的所有代码。

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.bindNumbers(mNumbers.get(position));
    // add listener here
    holder.deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            delete(position);
        }
    }
}

答案 1 :(得分:0)

代码并没有真正改变我的问题,但删除静态关键字真的摆脱了我的很多挣扎

public class ViewHolder extends RecyclerView.ViewHolder implements     View.OnClickListener{

    TextView numberText;
    EditText checklistText;
    Button deleteButton;
    CheckBox checkDisBox;

    public ViewHolder(View itemView) {
        super(itemView);
        numberText = (TextView) itemView.findViewById(R.id.number);
        checklistText = (EditText) itemView.findViewById(R.id.editText);
        deleteButton = (Button) itemView.findViewById(R.id.deleteButton);
        checkDisBox = (CheckBox) itemView.findViewById(R.id.checkBox);

        deleteButton.setOnClickListener(this);
    }

    public void bindNumbers(numbers numbers) {
        numberText.setText(numbers.getInt() + "");
    }

    @Override
    public void onClick(View v) {
        delete(getAdapterPosition());
    }
}

我的删除方法

public void delete(int position) {
    mNumbers.remove(position);
    notifyItemRemoved(position);
}