如果项目点击listview,我如何刷新位置变量?

时间:2012-04-02 17:15:19

标签: android listview

我正在开发一个Android应用程序,其中用户选择一个特定的食物类别(即itemlist.java)。如果用户选择鸡,则将显示相应的项目列表。然后,当用户点击并点击另一个类别(即素食快乐)时,它仍然显示与鸡肉相同的列表。位置变量已更改,但不会上载目录资源。 请帮我解决这个问题。

Shoppincarthelper.java


public class ShoppingCartHelper {
    public static final String PRODUCT_INDEX = "PRODUCT_INDEX";

    private static List<Product1> itemlist;
    private static List<Product> catalog;
    private static List<Product> cart;

    public static final String PRODUCT_INDEX = "PRODUCT_INDEX";

    public static List<Product> getCatalog(Resources res) {
        if(catalog == null) {
            catalog = new Vector<Product>();
            catalog.add(new Product("Chicken Kadai", res.getDrawable(R.drawable.new_kadai),"Kadai Chicken is a spicy chicken dish that is a little dry in nature with a rich aroma given by the Ginger and curry leaves",123, 0));
        }
        return catalog;
    }

    public static List<Product1> getCatalog1(Resources res) {
        if(itemlist == null) {
            itemlist = new Vector<Product1>();
            itemlist.add(new Product1("Chicken "));
            itemlist.add(new Product1("Vegetarian Delight"));
            itemlist.add(new Product1("Rice"));
            itemlist.add(new Product1("Drinks"));
        }
        return itemlist;
    }

    public static List<Product> getCatalog2(Resources res) {
        if(catalog == null) {
            catalog = new Vector<Product>();
            catalog.add(new Product("Chana Masala", res
                .getDrawable(R.drawable.chanamasala),
                " Chana masala or chole masala is a popular vegetable dish in Pakistani cuisine & Indian cuisine.The main ingredient is chickpeas.It is fairly dry and spicy with a sour citrus note. ",80));
        }
        return catalog;
    }

    public static List<Product> getCatalog3(Resources res) {
        if(catalog == null) {
            catalog = new Vector<Product>();
            catalog.add(new Product("Jeera Rice   ", res
                .getDrawable(R.drawable.jeerarice),
                "Jeera Rice (Cumin flavored rice), is a simple and tasty variation on plain boiled rice made with cumin seeds.  ",80));
            catalog.add(new Product("Filipino Fried Rice ", res
                .getDrawable(R.drawable.friedrice),
                "Philippine style fried rice is a simple dish of rice, meat/and or vegetables, a little oil, some spice and a garnish. ",95));
            catalog.add(new Product("VEG Biryani   ", res
                .getDrawable(R.drawable.vegbiryani),
                "Vegetable Biryani, is a rich Indian Moghlai pullao layered with curry. Kabuli Chana Biryani, Hyderabadi Biryani and lots more ",60));
    }

Itemlist.java


public class ItemList extends Activity {
    private List<Product1> mProductList1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.itemlist);

        // Obtain a reference to the product catalog
        mProductList1 = ShoppingCartHelper.getCatalog1(getResources());

        // Create the list
        ListView listViewCatalog = (ListView) findViewById(R.id.listView1);
        listViewCatalog.setAdapter(new ProductAdapter1(mProductList1, getLayoutInflater()));
        listViewCatalog.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
                Intent CatalogIntent = new Intent(getBaseContext(),CatalogActivity.class);
                CatalogIntent.putExtra(ShoppingCartHelper.PRODUCT_INDEX,position);
                startActivity(CatalogIntent);
            }
        });
    }
}

CatalogActvity.java


public class CatalogActivity extends Activity {
    private List<Product> mProductList;

    /** Called when the activity is first created. 
    * @param CatalogIntent 
    * @param position */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.catalog);

        int ListIndex =getIntent().getExtras().getInt(ShoppingCartHelper.PRODUCT_INDEX);

        mProductList = ShoppingCartHelper.getCatalog(getResources());

        if(ListIndex==1)
        {
            mProductList = ShoppingCartHelper.getCatalog2(getResources());
        }
        if(ListIndex==2)
        {
            mProductList = ShoppingCartHelper.getCatalog3(getResources());
        }


        // Create the list
        ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);
        listViewCatalog.setAdapter(new ProductAdapter(mProductList, getLayoutInflater(), false));
        listViewCatalog.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
                Intent productDetailsIntent = new  Intent(getBaseContext(),ProductDetailsActivity.class);
                productDetailsIntent.putExtra(ShoppingCartHelper.PRODUCT_INDEX, position);
                startActivity(productDetailsIntent);
            }
         });

        Button viewShoppingCart = (Button) findViewById(R.id.ButtonViewCart);
        viewShoppingCart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent viewShoppingCartIntent = new Intent(getBaseContext(), ShoppingCartActivity.class);
                startActivity(viewShoppingCartIntent);
            }
        });
    }

    public void onBackPressed() {
        Intent ListIntent = new Intent(getBaseContext(),ItemList.class);
        startActivity(ListIntent);
    }
}

ProductAdapter1.java

public class ProductAdapter1 extends BaseAdapter {

 private List<Product1> mProductList1;
 private LayoutInflater mInflater;

 public ProductAdapter1(List<Product1> list, LayoutInflater inflater) {
  mProductList1 = list;
  mInflater = inflater;  
 }

@Override
 public int getCount() {
  return mProductList1.size();
 }

 @Override
 public Object getItem(int position) {
  return mProductList1.get(position);
 }

 @Override
 public long getItemId(int position) {
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  final ViewItem item;

  if (convertView == null) {
   convertView = mInflater.inflate(R.layout.itemlistcon,null);
   item = new ViewItem();

   item.productTitle = (TextView) convertView.findViewById(R.id.textView1);

   convertView.setTag(item);
  } else {
   item = (ViewItem) convertView.getTag();
  }

  Product1 curProduct = mProductList1.get(position);
  item.productTitle.setText(curProduct.title);
  return convertView;
 }


 public class ViewItem 
 {
  TextView productTitle;
 }

}

0 个答案:

没有答案