[英]how to set onclicklistener on cardview with data from database?
The app has a recycler view which has card to display the data from the database.该应用程序有一个回收器视图,其中有显示来自数据库的数据的卡片。 i want to let the user be able to click the card and it would show more details.The card has info of name,type and location only and it less pass data of the name so that i can load the data from the database.我想让用户能够点击卡片,它会显示更多详细信息。卡片只有名称、类型和位置的信息,它较少传递名称的数据,以便我可以从数据库中加载数据。
the error that i am getting is我得到的错误是
Cannot resolve method 'get' in 'TextView'
at
intent.putExtra("Name",String.valueOf((holder.Name_id).get(position)));
intent.putExtra("Type",String.valueOf((holder.Type_id).get(position)));
intent.putExtra("Location",String.valueOf((holder.Location_id).get(position)));
RecyclerView Adapter RecyclerView 适配器
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserHolder>{
Context context;
List<Users> usersList;
public UserAdapter(Context context, List<Users> usersList) {
this.context = context;
this.usersList = usersList;
}
@NonNull
@Override
public UserHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View userLayout= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_list,parent,false);
return new UserHolder(userLayout);
}
@Override
public void onBindViewHolder(@NonNull UserHolder holder, int position) {
Users users=usersList.get(position);
holder.Name_id.setText(users.getName());
holder.Type_id.setText(users.getType());
holder.Location_id.setText(users.getLocation());
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, cardviewclick.class);
intent.putExtra("Name",String.valueOf((holder.Name_id).get(position)));
intent.putExtra("Type",String.valueOf((holder.Type_id).get(position)));
intent.putExtra("Location",String.valueOf((holder.Location_id).get(position)));
}
});
}
@Override
public int getItemCount() {
return usersList.size();
}
public class UserHolder extends RecyclerView.ViewHolder{
TextView Name_id,Type_id,Location_id;
CardView cardView;
public UserHolder(@NonNull View itemView){
super(itemView);
Name_id=itemView.findViewById(R.id.textTitle);
Type_id=itemView.findViewById(R.id.textType);
Location_id=itemView.findViewById(R.id.textLocation);
cardView=itemView.findViewById(R.id.card);
}
}
}
card xml卡 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="@+id/card"
app:cardElevation="12dp"
app:cardCornerRadius="16dp"
android:layout_margin="16dp"
android:backgroundTint="#efefef"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title:"
android:textColor="@color/black"
android:textSize="24dp" />
<TextView
android:id="@+id/textTitle"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type:"
android:textColor="@color/black"
android:textSize="24dp" />
<TextView
android:id="@+id/textType"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location:"
android:textColor="@color/black"
android:textSize="24dp" />
<TextView
android:id="@+id/textLocation"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="24dp" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
This error over here:这个错误在这里:
Cannot resolve method 'get' in 'TextView'
at
intent.putExtra("Name",String.valueOf((holder.Name_id).get(position)));
intent.putExtra("Type",String.valueOf((holder.Type_id).get(position)));
intent.putExtra("Location",String.valueOf((holder.Location_id).get(position)));
Tells the whole story, just replace it with:讲述整个故事,只需将其替换为:
intent.putExtra("Name",holder.Name_id.getText());
intent.putExtra("Type",holder.Type_id.getText());
intent.putExtra("Location",holder.Location_id.getText());
This way, you would get the text inside of them.这样,您将获得其中的文本。
All you have to do is to write the correct method since #get()
doesn't exist.您所要做的就是编写正确的方法,因为#get()
不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.