如何在recyclerview项目中获取文本视图的值?

时间:2017-02-05 22:27:35

标签: java android android-recyclerview textview

这是RecyclerView的适配器。如何在另一项活动中获得deckName的值?

public class ViewHolder extends RecyclerView.ViewHolder {
    public RelativeLayout deckLayout;
    public LinearLayout countsLayout;
    public ImageButton deckExpander;
    public ImageButton indentView;
    public  TextView deckName;
    public TextView deckNew, deckLearn, deckRev;

    public ViewHolder(View v) {
        super(v);
        deckLayout = (RelativeLayout) v.findViewById(R.id.DeckPickerHoriz);
        countsLayout = (LinearLayout) v.findViewById(R.id.counts_layout);
        deckExpander = (ImageButton) v.findViewById(R.id.deckpicker_expander);
        indentView = (ImageButton) v.findViewById(R.id.deckpicker_indent);
        deckName = (TextView) v.findViewById(R.id.deckpicker_name);
        deckNew = (TextView) v.findViewById(R.id.deckpicker_new);
        deckLearn = (TextView) v.findViewById(R.id.deckpicker_lrn);
        deckRev = (TextView) v.findViewById(R.id.deckpicker_rev);


    }
}

我想获取此值并将其与字符串进行比较:

final int itemCount = mDeckListAdapter.getItemCount();
DeckAdapter a = new DeckAdapter(getLayoutInflater(), getApplicationContext());
for(int i=0;i<itemCount;i++){
    //  final CharSequence text = DeckAdapter.ViewHolder.deckName.getText();
    if (text.equals(di)){
        mRecyclerView.findViewHolderForAdapterPosition(i).itemView.performClick();
    }
}

但是这段代码并没有真正起作用。

4 个答案:

答案 0 :(得分:12)

试试这个

String title = ((TextView) recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.title)).getText().toString();

我喜欢

 recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {
          //  String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
            String title1 = ((TextView) recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.title)).getText().toString();
            Toast.makeText(getApplicationContext(), title1, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onLongClick(View view, int position) { }
    }));

答案 1 :(得分:0)

你可以试试这个。

使用下面的代码从您想要从textview中检索值,以便您可以从RecyclerView迭代每个textview的值。

for(int i=0;i<itemCount;i++)
{
  View view=mRecyclerView.getChildAt(i); // This will give you entire row(child) from RecyclerView
   if(view!=null)
     {
       TextView textView= (TextView) view.findViewById(R.id.deckpicker_name);
        String text=textView.getText().toString();
        if (text.equals(di)){
             // Do your stuff, after comparison
        }
      }
}

我没有对此进行过测试,但希望它能以某种方式为您服务。

答案 2 :(得分:0)

检查一下。这个对我有用。
只需粘贴您的活动或片段
rvSelectedProductList = Recyclerview
selectedItemAdapter = RecyclerView适配器

      rvSelectedProductList.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                final int itemCount = selectedItemAdapter.getItemCount();

                for (int i = 0; i < itemCount; i++) {
                    TextView tvSelling = rvSelectedProductList.getChildAt(i).findViewById(R.id.tvSelling);
                    TextView textViewDrawerTitle = rvSelectedProductList.getChildAt(i).findViewById(R.id.tvCartQty);


                    String totalamount = tvSelling.getText().toString();
                    String qty = textViewDrawerTitle.getText().toString();
                    System.out.println("qty" + qty);
                    System.out.println("total" + totalamount);
                }
                rvSelectedProductList.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });

答案 3 :(得分:0)

尝试执行此操作。但是,如果在回收站视图中按任意位置,此代码将执行。 如果仅在按标题对象时要动作,则必须在标题上放置事件

recyclerView.addOnItemTouchListener(
    new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
    @Override
    public void onClick(View view, int position) {
    String title=view.findViewById(R.id.title)).getText().toString();
        Toast.makeText(getApplicationContext(), 
    title1, 
   Toast.LENGTH_SHORT).show();
     }

    @Override
    public void onLongClick(View view, int position) { }
}));