recycleview获取项目在位置

时间:2019-03-17 13:13:25

标签: java android android-recyclerview rx-java recycler-adapter

我正在使用room和rxJava来构建带有购买清单的应用程序。我尝试使用

获取item和itemId
final Purchase purchase=PurAdapter.ViewHolder.getPurchaseAt(position);

但是有错误:“无法解析方法getPurchaseAt(position)”

我的适配器代码:

public class PurAdapter extends RecyclerView.Adapter<PurAdapter.ViewHolder> 
 {
 List<Purchase> purchaseList=new ArrayList<>();

 public PurAdapter(List<Purchase> purchaseList) {
   this.purchaseList = purchaseList;

}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
    View itemView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_new,viewGroup,false);
    itemView.setClickable(true);

    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
    Purchase currentPurchase =purchaseList.get(position);

    holder.myName.setText(currentPurchase.getName());
    holder.myPrice.setText(currentPurchase.getName());

    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            setPosition(holder.getAdapterPosition());

            return false;
        }
    });
}
private int position;

public int getPosition() {
    return position;
}

public void setPosition(int position) {
    this.position = position;
}

@Override
public long getItemId(int position) {

    return super.getItemId(position);
}
@Override
public void onViewRecycled(ViewHolder holder) {
    holder.itemView.setOnLongClickListener(null);
    super.onViewRecycled(holder);
}

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



 public Purchase getPurchaseAt(int position){
 return purchaseList.get(position);
 }

public static class ViewHolder extends RecyclerView.ViewHolder
        implements View.OnCreateContextMenuListener {
    private TextView myName;
    private TextView myPrice;
    public ViewHolder(View view) {
        super(view);
        myName = view
                .findViewById(R.id.tvName);
       myPrice = view
                .findViewById(R.id.tvPrice);
        view.setOnCreateContextMenuListener(this);
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, 
    ContextMenu.ContextMenuInfo contextMenuInfo) {
        menu.setHeaderTitle("select action: ");
        menu.add(Menu.NONE,0,Menu.NONE,"Update");
        menu.add(Menu.NONE,1,Menu.NONE,"Delete");
    }
     }

  }

和MainActivity.class.code:

  @Override
public boolean onContextItemSelected(MenuItem item) {
  int position = -1; 




    try {

     position = ((PurAdapter) mListView.getAdapter()).getPosition();

        final Purchase purchase=PurAdapter.ViewHolder.getPurchaseAt(position); //??????
        //purchase= purchaseList.get(position);  //doesn't work

    } catch (Exception e) {

Toast.makeText(MainActivity.this,"Exception",Toast.LENGTH_SHORT).show();
        return super.onContextItemSelected(item);
    }



    switch (item.getItemId()) {
        case 0: {

   //code for updating item

        }
        break;

    }

    return true;
}

我在获得该商品的位置后该如何获取要更新的购买商品?

0 个答案:

没有答案