英文:
How to retrieve data from an item that was clicked from an onclick listener
问题
你可以通过Intent将特定店铺的信息传递到DetailsActivity。在HomeActivity的点击事件中,你可以在Intent中放入所需的信息,然后在DetailsActivity中提取这些信息。以下是示例代码:
在HomeActivity的点击事件中:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, DetailsActivity.class);
// 将特定店铺的信息放入Intent
intent.putExtra("shopName", model.getName());
intent.putExtra("shopLocation", model.getLocation());
intent.putExtra("shopAddress", model.getAddress());
// 其他信息也可以类似添加
context.startActivity(intent);
}
});
在DetailsActivity中获取这些信息:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
// 从Intent中提取店铺信息
Intent intent = getIntent();
if (intent != null) {
String shopName = intent.getStringExtra("shopName");
String shopLocation = intent.getStringExtra("shopLocation");
String shopAddress = intent.getStringExtra("shopAddress");
// 获取其他信息的方法类似
// 现在你可以在DetailsActivity中使用这些信息了
}
}
这样,当你点击任何商店卡片或图像时,DetailsActivity将获取到特定商店的信息并显示出来。请确保在DetailsActivity中适当处理这些信息以完成你的需求。
英文:
i have a recycler view that has many shops in my app. each app when clicked opens a new activity that should display all the details of that specific shop.
now i created the onclick listener and everything is working fine and i am even going to the next activity which is detailsActivity.
My problem is that how can i pass the information of each individual shop into the detailsActivity after it has been clicked in the HomeActivity.
here is my Shop 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;
}
}
and here is my 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();
}
this is my DetailsActivity:
public class DetailsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
}
}
now when i click on any of the shops cards or images it opens up the DetailsActivity. my problem is with how to get that specific shops info into the new detailsActivity from the DB such as name,location,address etc.
答案1
得分: 0
首先,在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");
}
英文:
first of all, create setOnClickListener in onBindViewHolder
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);
}
});
for retrieving values in detail activity
Bundle extras = intent.getExtras();
if(extras != null)
String data = extras.getString("keyName");
答案2
得分: 0
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());
// 添加这些行
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 {
// ... 其他类内部的内容保持不变
}
然后在DetailsActivity中使用以下代码来获取所选的商店模型:
Shop shopModel = getIntent().getSerializableExtra("shopModel");
英文:
Nidhessh is right to ask you to move the onCLickListener to the onBindViewHolder, but I would do it this way:
@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);
}
});
Make your Shop class implement Serializable, like this:
public class Shop implements Serializable {
...
//all remains the same in that class
}
After in your DetailsActivity write this to get the selected shop model:
Shop shopModel = getIntent().getSerializableExtra("shopModel");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论