当我点击RecyclerView中项目内的TextView时,我想要获取该TextView的值。我不希望整个项目可点击,我只需要项目中的单个TextView可点击,即下面代码中的"countNumber"。当我点击它时,我想获取显示的countNumber文本数据。
Recycler适配器
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);
}
}