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

How to retrieve data from an item that was clicked from an onclick listener

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

现在我创建了 onclick 侦听器,一切正常,我什至要去下一个 activity detailsActivity。

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

这是我的店铺Class:

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 中,例如名称、位置、地址等。

首先,在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);

                }
            });

用于详细检索值activity

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

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);

                }
            });
    

使您的商店 class 实现可序列化,如下所示:

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

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

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