我想要TextView的值當我點擊RecyclerView內的項目的textview時。我不希望整個項目可點擊,我只需要在下面的代碼中的「countNumber」項目clickable單個文本視圖。當我點擊它我想獲得countNumber顯示的文本數據。在Recycler視圖內獲得TextView的項目點擊值?
回收站適配器
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);
}
}
你能得到你的onClick內的文本():在你的適配器(代碼註釋)首先創建接口:
然後實現在您的活動接口接收文本((TextView的)v)的.getText(); –