문제

I'm trying to create a sort of product viewer. I've got the list with products working but I need it to open a separate product view if I click on one of the items in that list. I've tried passing the Id of a product to the new activity but for some reason it won't work. Can anybody help? Thanks in advance.

This is my product list:

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

This is the class I'm trying to pass the product to:

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());
}
 }
도움이 되었습니까?

해결책

You cannot instantiate product_view Activity like this.

 product_view view = new product_view();

and then call its methods,

You will have to pass the product id, or the entire product object as an Intent extra to your product_view class like this,

Intent intent = new Intent(inside_category.this, product_view.class);
intent.putExtra("EXTRA_ID", productCategory.get(position).getId());
startActivity(intent);

And then in your product_view Activity do the below to get the ID.

String id = getIntent().getStringExtra("EXTRA_ID");

Using this id, you can fetch your product and populate in product_view Activity accordingly.

In case you cannot get the data only using the ID, you can also pass the entire product object via the Intent. See this for reference.

다른 팁

The ProductListAdapter needs to override the getItemId method:

@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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top