简体   繁体   English

如何使用数据库中的数据在cardview上设置onclicklistener?

[英]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.

相关问题 如何区分从 cardview 子项的 onclicklistener 传递到 OnActivityResult 的意图 - How to differentiate between intent passed from an onclicklistener of a cardview child to OnActivityResult 如何在CardView布局中实现OnClickListener - how to implement OnClickListener in CardView layout 如何使用Android Firebase-realtime-database中的CardView在RecyclerView中显示数据? - How to display data in RecyclerView with CardView from Android firebase-realtime-database? 在cardview上添加onclicklistener - Adding onclicklistener on cardview 如何为ListView中的项目设置onClickListener(通过从Firebase检索数据来动态生成Listview的项目) - How to set onClickListener for items in a ListView (items of listview are generated dynamically by retreiving data from firebase) 如何设置OnClickListener(Android) - How To Set OnClickListener (Android) 如何为ScrollView设置OnClickListener? - How to set OnClickListener for ScrollView? 如何在RecyclerView上设置onClickListener? - How to set onClickListener on RecyclerView? 当用户单击卡片视图时,如何从Firebase实时数据库中删除显示在卡片视图上的特定记录。 - How to delete a specific record displayed on a cardview from Firebase Realtime Database when user clicks on a cardview. 如何从Activity为Fragment的开关设置OnClickListener? - How to set OnClickListener from Activity for Fragment's switch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM