在Recycler View中点击项目获取TextView的值?

时间:2017-01-27 11:12:48

标签: android android-recyclerview

当我点击RecyclerView内部项目的textview时,我想要TextView的值。我不想要整个项目可点击,我只需要点击项目的单一文本视图,即#34; countNumber"在波纹管代码中。当我点击它时,我想得到countNumber显示的文本数据。

Recycler Adapter

 public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

    ArrayList<UserData> dataSet = new ArrayList<>();

    public CustomAdapter(ArrayList<UserData> data) {
        this.dataSet = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                           int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_layout, parent, false);


        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {

        holder.heading.setText(dataSet.get(listPosition).getHeading());
        holder.description.setText(dataSet.get(listPosition).getDescription());
        holder.countNumber.setText(String.valueOf(dataSet.get(listPosition).getCount()));
        holder.time.setText(dataSet.get(listPosition).getTime());
        holder.date.setText(dataSet.get(listPosition).getDate());
        holder.place.setText(dataSet.get(listPosition).getPlace());


    }

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

    public void countIncrease() {

    }

    public List<UserData> getList() {
        return this.dataSet;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {


        TextView heading;
        TextView description;
        TextView countNumber;
        TextView time;
        TextView date;
        TextView place;

        public MyViewHolder(final View itemView) {
            super(itemView);

            this.countNumber = (TextView) itemView.findViewById(R.id.countId);
            this.heading = (TextView) itemView.findViewById(R.id.textViewName);
            this.description = (TextView) itemView.findViewById(R.id.textViewVersion);
            this.place = (TextView) itemView.findViewById(R.id.placeId);
            this.time = (TextView) itemView.findViewById(R.id.timeId);
            this.date = (TextView) itemView.findViewById(R.id.dateId);
            this.countNumber.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                         //i need counterNumber value here
                    Log.v("loo", "Click");
                }
            });
        }
    }
}

MainActivity

 public class MainActivity extends AppCompatActivity {

    DBHelper dbHelper;

     CustomAdapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    private static RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        dbHelper = DBHelper.getInstance(getApplicationContext());

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addButton();
            }
        });



        recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        adapter = new CustomAdapter(dbHelper.getAllIncidents());
        recyclerView.setAdapter(adapter);

    }




    public void addButton(){
        Intent addButtonIndent = new Intent(this,AddData.class);
        startActivity(addButtonIndent);
    }


}

3 个答案:

答案 0 :(得分:4)

如果您希望从适配器的内部获取,正如@ ramesh-r所说,您可以使用((TextView)v).getText();

获取它
this.countNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               ((TextView)v).getText();
            }
        });

但是如果你想从Activity 获取值,你需要为它创建和实现一个接口。首先在适配器中创建接口(带注释的代码):

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

  ArrayList<UserData> dataSet = new ArrayList<>();

  public CustomAdapter(ArrayList<UserData> data) {
    this.dataSet = data;
  }

  // Define listener member variable
  private static OnRecyclerViewItemClickListener mListener;

  // Define the listener interface
  public interface OnRecyclerViewItemClickListener {
    void onItemClicked(String text);
  }

  // Define the method that allows the parent activity or fragment to define the listener.
  public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener listener) {
    this.mListener = listener;
  }

  @Override
  public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                       int viewType) {
    ...
  }

  @Override
  public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {

    ...
  }

  ...

  public static class MyViewHolder extends RecyclerView.ViewHolder {
    ...

    public MyViewHolder(final View itemView) {
      super(itemView);

      ...

      this.countNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              // send the text to the listener, i.e Activity.
              mListener.onItemClicked(((TextView)v).getText());
            }
        });
    }
  }

  ...
}

然后在Activity中实现接口以接收文本:

adapter = new CustomAdapter(dbHelper.getAllIncidents());

// Interface implementation.
adapter.setOnRecyclerViewItemClickListener(new YourAdapter.OnRecyclerViewItemClickListener() {
  @Override 
  public void onItemClicked(String text) {
    Log.d(TAG, "Text is = " + text); 
  }
});

recyclerView.setAdapter(adapter);

答案 1 :(得分:1)

试用此代码

countNumber设为final

final this.countNumber = (TextView) itemView.findViewById(R.id.countId);

并且,从TextView获取countNumber

的值
    this.countNumber.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                 //i need counterNumber value here
            String value = this.countNumber.getText().toString().trim();
            Log.v("loo", "countNumber=="+value);
        }
    });

答案 2 :(得分:0)

请尝试以下代码

  this.countNumber.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           String value=((TextView)v).getText();
                 //i need counterNumber value here
            Log.v("loo", value);
        }
    });