如何从 onclick 侦听器单击的项目中检索数据

Posted

技术标签:

【中文标题】如何从 onclick 侦听器单击的项目中检索数据【英文标题】:How to retrieve data from an item that was clicked from an onclick listener 【发布时间】:2020-11-27 04:47:40 【问题描述】:

我的应用中有很多商店的回收站视图。每个应用程序在单击时都会打开一个新活动,该活动应显示该特定商店的所有详细信息。

现在我创建了 onclick 监听器,一切正常,我什至要进入下一个活动,即 detailsActivity。

我的问题是如何在 HomeActivity 中单击后将每个商店的信息传递到 detailsActivity 中。

这是我的商店课程:

private String name;
private String country;
private String address;
private String location;
private String ShopHeaderImg;
private String ShopProfileImg;
private String shopPID;

//constructor
public Shop()



//constructor with parameters
public Shop(String name, String country, String address, String location, String shopHeaderImg, String shopProfileImg, String shopPID) 
    this.name = name;
    this.country = country;
    this.address = address;
    this.location = location;
    this.ShopHeaderImg = shopHeaderImg;
    ShopProfileImg = shopProfileImg;

    this.shopPID = shopPID;



public String getName() 
    return name;


public void setName(String name) 
    this.name = name;


public String getCountry() 
    return country;


public void setCountry(String country) 
    this.country = country;


public String getAddress() 
    return address;


public void setAddress(String address) 
    this.address = address;


public String getLocation() 
    return location;


public void setLocation(String location) 
    this.location = location;



public String getShopHeaderImg() 
    return ShopHeaderImg;


public void setShopHeaderImg(String shopHeaderImg) 
    ShopHeaderImg = shopHeaderImg;



public String getShopProfileImg() 
    return ShopProfileImg;


public void setShopProfileImg(String shopProfileImg) 
    ShopProfileImg = shopProfileImg;



public String getShopPID() 
    return shopPID;


public void setShopPID(String shopPID) 
    this.shopPID = shopPID;

 

这是我的 HomeActivity:

private FirebaseFirestore firebaseFirestore;
    private RecyclerView FirestoreList;
    private FirestoreRecyclerAdapter adapter;

 protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);


        FirestoreList = findViewById(R.id.recycler_view);
        firebaseFirestore = FirebaseFirestore.getInstance();

  Query q = firebaseFirestore.collection("Shops");

    //recycle options
    FirestoreRecyclerOptions<Shop> options = new FirestoreRecyclerOptions.Builder<Shop>()
            .setQuery(q, Shop.class)
            .build();

    adapter = new FirestoreRecyclerAdapter<Shop, ShopViewHolder>(options) 
        @NonNull
        @Override
        public ShopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_item,parent,false);
            //onclick of cardview
            view.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    Context context = v.getContext();
                    Intent intent = new Intent(context, DetailsActivity.class);
                    context.startActivity(intent);

                
            );
            return new ShopViewHolder(view);
        

        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        protected void onBindViewHolder(@NonNull ShopViewHolder holder, int position, @NonNull Shop model) 
            holder.list_name.setText(model.getName());
            holder.list_location.setText(model.getLocation());
            holder.list_uid.setText(model.getShopPID());
            holder.setHeaderImage(getApplicationContext(),model.getShopHeaderImg());
            holder.list_profileImage(getApplicationContext(),model.getShopProfileImg());

    
    ;

    FirestoreList.setHasFixedSize(true);
    FirestoreList.setLayoutManager(new LinearLayoutManager(this));
    FirestoreList.setAdapter(adapter);



private class ShopViewHolder extends RecyclerView.ViewHolder 
    private TextView list_name;
    private TextView list_location;
    private TextView list_uid;
    private ImageView header_img;
    private ImageView list_profileImage;




    public ShopViewHolder(@NonNull View itemView) 
        super(itemView);



        list_name = itemView.findViewById(R.id.list_name);
        list_location = itemView.findViewById(R.id.list_location);
        list_uid = itemView.findViewById(R.id.list_uid);
        header_img = itemView.findViewById(R.id.header_img);
        list_profileImage = itemView.findViewById(R.id.list_profileImage);

    

    public void setHeaderImage(final Context c , final String Image) 
        final ImageView headerImg = (ImageView)itemView.findViewById(R.id.header_img);
        Picasso.get().load(Image).into(headerImg);

    
    public void list_profileImage(final Context c , final String image) 
        final ImageView profileImg = (ImageView)itemView.findViewById(R.id.list_profileImage);
        Picasso.get().load(image).into(profileImg);

    


@Override
public void onStart() 
    super.onStart();
    adapter.startListening();


@Override
public void onStop() 
    super.onStop();
    adapter.stopListening();

这是我的 DetailsActivity:

public class DetailsActivity extends AppCompatActivity 


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);


    

现在,当我单击任何商店卡片或图像时,它会打开 DetailsActivity。我的问题是如何将特定商店信息从数据库中获取到新的 detailsActivity 中,例如名称、位置、地址等。

【问题讨论】:

【参考方案1】:

Nidhessh 要求您将 onCLickListener 移动到 onBindViewHolder 是正确的,但我会这样做:

    @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        protected void onBindViewHolder(@NonNull ShopViewHolder holder, int position, @NonNull Shop model) 
            holder.list_name.setText(model.getName());
            holder.list_location.setText(model.getLocation());
            holder.list_uid.setText(model.getShopPID());
            holder.setHeaderImage(getApplicationContext(),model.getShopHeaderImg());
            holder.list_profileImage(getApplicationContext(),model.getShopProfileImg());

//add this lines
holder.itemView.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                   
                    Intent intent = new Intent(context, DetailsActivity.class);
                    intent.putExtra("shopModel", model);
                    context.startActivity(intent);

                
            );
    

让您的 Shop 类实现 Serializable,如下所示:

    public class Shop implements Serializable  
...
//all remains the same in that class
 

在您的 DetailsActivity 中写入以下内容以获取选定的商店模型:

Shop shopModel = getIntent().getSerializableExtra("shopModel");

【讨论】:

谢谢您,您的代码运行良好,但您能举个例子说明我现在如何从数据库中检索信息吗?还是课程?因为这个detailsactivity不是recyclerview 您要检索的具体信息是什么?您想通过 Shop 模型类中的商店 id 获取额外信息吗?【参考方案2】:

首先,在onBindViewHolder中创建setOnClickListener

 holder.list_name..setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                   
                    Shop shopmodel= shoparray.get( holder.getAdapterPosition());
                    Intent intent = new Intent(context, DetailsActivity.class);
                    intent.putExtra("keyName", shopmodel.getName());
                    context.startActivity(intent);

                
            );

用于检索详细活动中的值

 Bundle extras = intent.getExtras();
    if(extras != null)
    String data = extras.getString("keyName");

【讨论】:

这很好,但是 shoparray 应该是什么?因为它在“无法解析符号 shoparray”这一行给我一个错误 这段代码有错误,但我认为你的方法是正确的,你能修复它吗?

以上是关于如何从 onclick 侦听器单击的项目中检索数据的主要内容,如果未能解决你的问题,请参考以下文章

如何从 fire-base 检索数据到新活动中单击 RecyclerView

ViewHolder 中的 onClick 无权访问适配器中的单击项目

如何从Button的onClick侦听器中删除Firebase DataBase

onclick 使用 ajax laravel 从数据库中获取数据

如何使用 gridlayout 设置 OnClick android cardview 示例

Android:RecyclerView onClick 监听器没有得到