这是我在该列表视图中显示列表视图的代码当我点击该按钮时,我将按一个按钮将相关数据添加到购物车中,但它不会将项目显示到购物车中..
public class All_Item_Fragment extends android.support.v4.app.ListFragment {
// Declare Variables
SQLiteDatabase sqLite;
JSONArray jsonarray = null;
ListView list;
TextView itemText,itemCount,rs,totalAmount,cartEmpty;
ListViewAdapter adapter;
Context context;
ArrayList<HashMap<String, String>> itemlist;
ArrayList<Product> tempMenu = new ArrayList<>();
static String Array = "AllMenu";
static String NAME = "name";
static String DESCRIPTION = "Description";
static String PRICE = "price";
static String IMAGE = "image_path";
static String ORDER = "order";
int count=0;
int totalCartItemCount =0;
int totalCartValue = 0;
Button checkout_main;
String url="http://mahatiffin.com/web/selectallmenu.php";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.all_item_layout, container, false);
itemlist = new ArrayList<HashMap<String, String>>();
new ReadJSON().execute();
list = (ListView) view.findViewById(android.R.id.list);
checkout_main = (Button) view.findViewById(R.id.checkout_all_main);
tempMenu = new ArrayList<>();
getCartData();
totalCartItemCount = tempMenu.size();
totalCartValue =0;
for (int temp1=0; temp1 < tempMenu.size(); temp1++)
{
totalCartValue = totalCartValue + Integer.parseInt(tempMenu.get(temp1).getFinalValue());
}
itemText = (TextView) view.findViewById(R.id.item_text);
itemCount = (TextView) view.findViewById(R.id.item_count);
rs = (TextView) view.findViewById(R.id.total);
totalAmount = (TextView) view.findViewById(R.id.total_amount);
cartEmpty = (TextView) view.findViewById(R.id.cart_empty);
if (totalCartItemCount == 0)
{
itemText.setVisibility(view.VISIBLE);
itemCount.setVisibility(view.VISIBLE);
rs.setVisibility(view.VISIBLE);
totalAmount.setVisibility(view.VISIBLE);
checkout_main.setVisibility(view.VISIBLE);
list.setVisibility(view.VISIBLE);
cartEmpty.setVisibility(view.INVISIBLE);
}
else
{
itemText.setVisibility(view.VISIBLE);
itemCount.setVisibility(view.VISIBLE);
rs.setVisibility(view.VISIBLE);
totalAmount.setVisibility(view.VISIBLE);
checkout_main.setVisibility(view.VISIBLE);
list.setVisibility(view.VISIBLE);
cartEmpty.setVisibility(view.INVISIBLE);
}
itemCount.setText("("+ totalCartItemCount + ")");
checkout_main.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
startActivity(new Intent(getActivity(),ExtrasItem.class));
}
});
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(getActivity(), itemlist);
// Set the adapter to the ListView
list.setAdapter((ListAdapter) adapter);
return view;
}
public void getCartData()
{
Product tempCartItem = new Product();
tempMenu.clear();
sqLite=context.openOrCreateDatabase("mahatiffin",context.MODE_PRIVATE, null);
Cursor c=sqLite.rawQuery("SELECT FINAL_VALUE FROM CART",null);
count=0;
if(c.moveToFirst())
{
do{
tempCartItem = new Product();
tempCartItem.setFinalValue(c.getString(c.getColumnIndex("FINAL_VALUE")));
tempMenu.add(tempCartItem);
count++;
}while(c.moveToNext());
}
sqLite.close();
}
class ReadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... args) {
Product tempMenu;
try {
JSONObject jsonobject = JSONfunctions.getJSONfromURL(url);
jsonarray = jsonobject.optJSONArray(Array);
//productList = json.getJSONArray("AllMenu");
//parse date for dateList
for (int i = 0; i < jsonarray.length(); i++)
{
tempMenu = new Product();
jsonobject = jsonarray.getJSONObject(i);
tempMenu.setName(jsonobject.getString("name"));
tempMenu.setDescription(jsonobject.getString("Description"));
tempMenu.setPrice(jsonobject.getString("price"));
tempMenu.setImagePath(jsonobject.getString("image_path"));
itemlist.add(tempMenu);
//return ("OK");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
adapter.notifyDataSetChanged();
}
}
}
我的ListViewAdapter就在这里..
public class ListViewAdapter extends BaseAdapter{
// Declare Variables
//Product tempMenu;
Context activity;
LayoutInflater inflater;
//ArrayList<HashMap<String, String>> ExtraMenu = new ArrayList<>();
ArrayList<HashMap<String, String>> AllMenu = new ArrayList<>();
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(FragmentActivity activity, ArrayList<HashMap<String, String>> itemlist) {
this.activity=activity;
AllMenu = itemlist;
imageLoader = new ImageLoader(activity);
}
public int getCount() {
return AllMenu.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, final View convertView, ViewGroup parent) {
// Declare Variables
Product tempMenu = (Product) AllMenu.get(position);
TextView name, Description, price;
ImageView image_path;
ImageButton order;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.all_item_list, parent, false);
// Get the position
price = (TextView) view.findViewById(R.id.price_all_main);
name = (TextView) view.findViewById(R.id.name_all_main);
Description = (TextView) view.findViewById(R.id.description_all_main);
image_path = (ImageView) view.findViewById(R.id.image_all_main);
order = (ImageButton) view.findViewById(R.id.order);
// Capture position and set results to the TextViews
name.setText(tempMenu.getName());
price.setText(tempMenu.getPrice());
Description.setText(tempMenu.getDescription());
order.setOnClickListener(new MyPersonalClickListener("order",tempMenu, (FragmentActivity) activity));
imageLoader.DisplayImage(tempMenu.getImagePath(),image_path);
return view;
}
public class MyPersonalClickListener implements View.OnClickListener
{
String button_name;
Product name;
int tempQty;
int tempValue;
SQLiteDatabase sqLite;
Context context;
//constructor method
public MyPersonalClickListener(String button_name, Product name, Context context)
{
this.name = name;
this.button_name = button_name;
this.context = context;
}
@Override
public void onClick(View v)
{
// in this section, we are going to add items to cart
//if the item is already there in cart, then increase the quantity by 1, else add the item to cart
//if the quantity of the item has reached 10, then do nothing ---this is just a specific logic where
//i did not want any item with quantity more than 10, but if you choose not to, then just comment out
//the code.
sqLite=context.openOrCreateDatabase("mahatiffin", context.MODE_PRIVATE, null);
sqLite.execSQL("CREATE TABLE IF NOT EXISTS TB_MENU (ID INTEGER primary key autoincrement,NAME VARCHAR, DESCRIPTION VARCHAR, PRICE INTEGER)");
sqLite.execSQL("CREATE TABLE IF NOT EXISTS CART (ID INTEGER primary key autoincrement, NAME VARCHAR, DESCRIPTION VARCHAR, PRICE INTEGER, MENU_QTY INTEGER, FINAL_VALUE INTEGER)");
Cursor cc = sqLite.rawQuery("SELECT MENU_QTY, FINAL_VALUE FROM CART WHERE NAME = '"+name.getName()+ "'", null);
if (cc.getCount()== 0)
{
//product not already there in cart..add to cart
sqLite.execSQL("INSERT INTO CART (NAME,DESCRIPTION,PRICE,MENU_QTY,FINAL_VALUE) VALUES('"+name.getName()+ "','"+name.getDescription()+ "',"+ Integer.parseInt(name.getPrice())+",1,"+ Integer.parseInt(name.getPrice())+")");
Toast.makeText(context,"Item "+name.getName()+" added to Cart", Toast.LENGTH_LONG).show();
}
else
{
//product already there in cart
if(cc.moveToFirst())
{
do{
tempQty=cc.getInt(0);
tempValue = cc.getInt(1);
}while(cc.moveToNext());
}
if (tempQty < 10)
{
sqLite.execSQL("UPDATE CART SET MENU_QTY = "+ (tempQty+1)+",FINAL_VALUE = "+ (Integer.parseInt(name.getPrice())+tempValue) +" WHERE NAME ="+ name.getName());
Toast.makeText(context,"Item "+name.getName()+" added to Cart", Toast.LENGTH_LONG).show();
}
}
sqLite.close();
}
}
}
错误在于编码
In listviewadapter error is here while i m adding one item second time my logcat says error in this line
sqLite.execSQL("UPDATE CART SET MENU_QTY = "+ (tempQty+1)+",FINAL_VALUE = "+ (Integer.parseInt(name.getPrice())+tempValue) +" WHERE NAME ="+ name.getName());
In my All_Item_Fragment when i M ADDING item to cart my cart will display empty cart At this line error its shows getCartData(); in oncreateview(); and in getCartData(){sqLite=context.openOrCreateDatabase("mahatiffin",context.MODE_PRIVATE, null); }