我试图创建一种产品查看器。我已经获得了产品列表,但如果我点击该列表中的某个项目,我需要它来打开一个单独的产品视图。我已经尝试将产品的ID传递给新活动,但由于某种原因它无法正常工作。有人可以帮忙吗?提前谢谢。
这是我的产品清单:
public class inside_category extends Activity {
private static String _category;
List<product> Products = new ArrayList<product>();
ListView productListView;
private static List<product> productCategory = new ArrayList<product>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inside_category);
setTitle(_category);
productListView = (ListView) findViewById(R.id.listView);
addProduct();
populateList();
productListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
product_view productviewer = new product_view();
productviewer.setCurrentProductId(productCategory.get(position).getId());
productviewer.setTitle(productCategory.get(position).getProductName());
Intent intent = new Intent(inside_category.this, product_view.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.inside_category, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static List<product> getProductCategory(){
return productCategory;
}
public static void setCategory(String category){
_category = category;
}
public void addProduct(){
Products.add(new product("Socket AM3+",1, "i7", "Dayum", 430));
Products.add(new product("Socket AM3+",2, "Cool", "yeah", 430));
Products.add(new product("Socket 2011",3, "i5", "Not that dayum", 200));
Products.add(new product("AMD",4, "i7", "Dayum", 430));
Products.add(new product("Nvidia",5, "i5", "Not that dayum", 200));
Products.add(new product("DDR2",6, "i7", "Dayum", 430));
Products.add(new product("ATX",7, "i5", "Not that dayum", 200));
int listLength = Products.size();
for(int i = 0; i < listLength; i++){
if(Products.get(i).getListId().contains(_category)){
productCategory.add(Products.get(i));
}
}
}
private void populateList(){
ArrayAdapter<product> adapter = new ProductListAdapter();
productListView.setAdapter(adapter);
}
public String getCategory(){
return _category;
}
private class ProductListAdapter extends ArrayAdapter<product>{
public ProductListAdapter(){
super (inside_category.this, R.layout.product, productCategory);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getLayoutInflater().inflate(R.layout.product, parent, false);
product prod = productCategory.get(position);
TextView name = (TextView) view.findViewById(R.id.productTitle);
name.setText(prod.getProductName());
TextView price = (TextView) view.findViewById(R.id.productPrice);
price.setText("€"+prod.getProductPrice());
return view;
}
}
}
这是我试图将产品传递给我们的课程:
public class product_view extends Activity {
int currentProductId;
String Name, Description, ListId;
int Price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_view);
populate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.product_view, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void setCurrentProductId(int currentProductId){
this.currentProductId = currentProductId;
}
public int getCurrentProductId(){
return currentProductId;
}
private void populate(){
for(int i = 0; i > prodCat.size(); i++ ){
if(prodCat.get(i).getId() == currentProductId){
Name = prodCat.get(i).getProductName();
Price = prodCat.get(i).getProductPrice();
Description = prodCat.get(i).getProductDescription();
ListId = prodCat.get(i).getListId();
}
}
product currentProduct = new product(ListId, currentProductId, Name, Description, Price);
TextView name = (TextView) findViewById(R.id.productNameTitle);
name.setText(currentproduct.getProductName());
TextView price = (TextView) findViewById(R.id.productPriceView);
price.setText("€"+currentproduct.getProductPrice());
TextView productType = (TextView) findViewById(R.id.productTypeId);
productType.setText(currentproduct.getListId());
TextView description = (TextView) findViewById(R.id.productDescriptionView);
description.setText(currentproduct.getProductDescription());
}
}
答案 0 :(得分:0)
ProductListAdapter
需要覆盖getItemId
方法:
@Override
public long getItemId(int position){
// return the id(), or whatever you use to access the id on your product object
return productCategory.get(position).id();
}
答案 1 :(得分:0)
您无法像这样实例化product_view Activity。
product_view view = new product_view();
然后调用它的方法,
您必须将产品ID或整个产品对象作为Intent extra传递给您的product_view类,如下所示,
Intent intent = new Intent(inside_category.this, product_view.class);
intent.putExtra("EXTRA_ID", productCategory.get(position).getId());
startActivity(intent);
然后在您的product_view活动中执行以下操作以获取ID。
String id = getIntent().getStringExtra("EXTRA_ID");
使用此ID,您可以获取产品并相应地填充product_view活动。
如果您无法仅使用ID获取数据,您还可以通过Intent传递整个产品对象。请参阅this for reference。