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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42058050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:20:30  来源:igfitidea点击:

how can i get the value of text view in recyclerview item?

javaandroidandroid-recyclerviewtextview

提问by golbahar

This is my adapter for RecyclerView. How can I get the value of deckNamein another activity?

这是我的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);


    }
}

I want get this value and compare it with a string here:

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

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();
    }
}

but this code doesn't really work.

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

回答by Srikanth

Try this

试试这个

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

I used like

我用过

 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) { }
    }));

回答by A.R.

You can try this.

你可以试试这个。

Use below code where you want to retrieve the value from textview, so that you can iterate the value of each textview from your RecyclerView.

在要从 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
        }
      }
}

I have not tested this, but hope it works for you somehow.

我没有测试过这个,但希望它以某种方式对你有用。

回答by Aasik

Check out this. It works for me.
Just Paste in Your Activity or Fragment
rvSelectedProductList = Recyclerview
selectedItemAdapter = RecyclerView Adapter

看看这个。这个对我有用。
只需粘贴您的活动或片段
rvSelectedProductList = Recyclerview
selectedItemAdapter = RecyclerView Adapter

      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);
            }
        });

回答by Applicant

Try this.but this code will execute when press any place in recycler view. If you want to action when press title object only you must put event on title

试试这个。但是当按下回收器视图中的任何地方时,这段代码将执行。如果您只想在按下标题对象时进行操作,则必须将事件放在标题上

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) { }
}));