Android如何刷新或重新加载listview数据和最终总计数据,这些数据是购物车总价的总和
这是我的代码
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolderItem viewHolder;
if(convertView==null){
inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.cart_item, parent, false);
viewHolder = new ViewHolderItem();
viewHolder.toptext = (TextView) convertView.findViewById(R.id.cartname);
viewHolder.prc = (TextView) convertView.findViewById(R.id.textView76);
viewHolder.shp = (TextView) convertView.findViewById(R.id.textView77);
viewHolder.subtotal = (TextView) convertView.findViewById(R.id.textView79);
//viewHolder.textView80 = (TextView) convertView.findViewById(R.id.textView80);
viewHolder.img = (ImageView) convertView.findViewById(R.id.imageView7);
viewHolder.imageView2 = (ImageView) convertView.findViewById(R.id.imageView2);
viewHolder.spinner = (Spinner)convertView.findViewById(R.id.spinner);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolderItem) convertView.getTag();
}
final CartResponse response = getItem(position);
int subtotal1=0;
mydb = new DBHelper(getContext());
if(response != null) {
/* ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(),
R.array.qty, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
viewHolder.spinner.setAdapter(adapter);*/
viewHolder.spinner.setSelection(Integer.parseInt(response.getQty())-1);
viewHolder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
response.setQty(viewHolder.spinner.getSelectedItem()+"");
if(mydb.updateContact(Integer.parseInt(response.getid()),response.getPname(),Integer.parseInt(response.getPrice()),Integer.parseInt(response.getCutPrice()),++pos,Integer.parseInt(response.getShipping()),response.getPhoto())){
Log.d("key",""+response.getQty());
updateResults();
//Toast.makeText(getContext(),"updated", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
viewHolder.imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mydb.deleteContact(Integer.parseInt(response.getid()));
//int index =callback.indexOf(response.getid());
callback.remove(response);
updateResults();
Toast.makeText(getContext(),"Remove", Toast.LENGTH_SHORT).show();
}
});
int tamount=0,tship=0,gt=0;
tamount=tamount+(Integer.parseInt(response.getPrice())* Integer.parseInt(response.getQty()));
tship=tship+(Integer.parseInt(response.getShipping())* Integer.parseInt(response.getQty()));
gt=tamount+tship;
subtotal1=Integer.parseInt(response.getPrice()) + Integer.parseInt(response.getShipping());
viewHolder.toptext.setText(response.getPname());
viewHolder.prc.setText("Rs."+tamount);
//Log.d("key",""+response.getPrice());
viewHolder.shp.setText("Rs."+tship);
viewHolder.subtotal.setText("Sub total Rs."+gt);
Picasso.get().load("https://www.exmple.com/photos/"+response.getPhoto()+".png").placeholder(R.drawable.placeholder).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(viewHolder.img);
}
购物车课程
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_cart, container, false);
mydb = new DBHelper(getContext());
cart_list = mydb.getAllCotacts();
cartitems=cart_list.size();
int amount=0,shping=0;
for(int i=0;i<cartitems;i++){
int price = (Integer.parseInt(cart_list.get(i).getPrice())*Integer.parseInt(cart_list.get(i).getQty()));
int shp=(Integer.parseInt(cart_list.get(i).getShipping())*Integer.parseInt(cart_list.get(i).getQty()));
amount=amount+price;
shping=shping+shp;
}
TextView cout=(TextView)view.findViewById(R.id.textView75);
TextView gprice=(TextView)view.findViewById(R.id.textView87);
TextView gship=(TextView)view.findViewById(R.id.textView88);
gprice.setText("Rs."+amount);
gship.setText("Rs."+shping);
if(cartitems>0){
cout.setText("Your Cart Item");
}else{
cout.setText("Shopping Cart is Empty");
}
final CartAdapter adapter = new CartAdapter(getContext(), R.layout.cart_item, cart_list);
final ListView order_list = (ListView)view.findViewById(R.id.cartlist);
order_list.setAdapter(adapter);
adapter.notifyDataSetChanged();
order_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
adapter.notifyDataSetChanged();
//mydb.deleteContact(cart_list.get(position).getid());
//Log.d("key",""+cart_list.get(position).getQty());
//cart_list.get(position).getid();
}});
return view;
}