当我删除购物车中的商品或更新产品数量时,我尝试更新我的列表视图,但它不起作用。我的错在哪里??我尝试 notifyDataSetChanged(); 方法,但它不会立即起作用。我想在更改列表视图时刷新它。我该怎么做?我需要在适配器类或其他东西中编写方法吗?
public class Cart extends Activity implements OnClickListener{
private ArrayList<String> textfield;
private ArrayList<String> birimfield;
private ArrayList<String> adetfield;
private ArrayList<String> pricefield;
private ArrayList<String> shortfield;
private ArrayList<Drawable> imagefield;
private CustomAdapter customadapter;
public static double sepetbedeli;
public static double toplambedel;
public static int couponid;
private boolean isCouponExists=false;
public String adet;
public static final double kargobedeli=6.0;
String ad;
TextView greeting;
ImageButton sepet,devam;
Button uyelik,hakkimizda;
public static ArrayList<CartSystem> userCart = new ArrayList<CartSystem>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_cart);
Bundle extras = getIntent().getExtras();
if (extras != null) {
ad=extras.getString("ad");
greeting=(TextView)findViewById(R.id.greetingsText);
greeting.setText("Hoşgeldiniz, "+extras.getString("ad"));
}
for (int i = 0; i < userCart.size(); i++) {
if(userCart.get(i).birim=="adet"||userCart.get(i).birim=="Adet")
{
System.out.println("Adetli sipariş bulundu!");
userCart.get(i).adet="1";
}
}
preparetext();
preparefiyat();
preparekisa();
prepareadet();
preparebirim();
try {
prepareimage();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
customadapter.notifyDataSetChanged();
ListView gridView=(ListView)findViewById(R.id.listView1);
gridView.setCacheColorHint(Color.TRANSPARENT);
gridView.requestFocus(0);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
AlertDialog.Builder alert = new AlertDialog.Builder(Cart.this);
final int i =position;
alert.setTitle("Ürün "+userCart.get(position).birim+" miktarını değiştir.");
alert.setMessage("Yeni miktarı giriniz:");
// Set an EditText view to get user input
final EditText servername = new EditText(Cart.this);
alert.setView(servername);
alert.setPositiveButton("Değiştir", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
adet = servername.getText().toString();
userCart.get(i).adet=adet;
customadapter.notifyDataSetChanged();
gridView.setAdapter(customadapter);
}
});
alert.setNeutralButton("Ürünü Sil", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
userCart.remove(i);
customadapter.notifyDataSetChanged();
gridView.setAdapter(customadapter);
}
});
alert.show();
}
});
Kontroller();
customadapter= new CustomAdapter(this, textfield, imagefield,adetfield,birimfield,pricefield,shortfield);
gridView.setAdapter(customadapter);
TextBoxKontrolleri();
}
private void TextBoxKontrolleri()
{
TextView sepettoplam = (TextView)findViewById(R.id.txtsepetfiyati);
for (int i = 0; i < userCart.size(); i++) {
sepetbedeli+=Double.parseDouble(userCart.get(i).fiyat);
}
sepettoplam.setText("Sepet Bedeli: " +Double.toString(sepetbedeli)+" TL");
TextView kargo = (TextView)findViewById(R.id.txtkargobedeli);
TextView toplam = (TextView)findViewById(R.id.txttoplam);
TextView kargotext = (TextView)findViewById(R.id.txtkargobizden);
if(sepetbedeli+kargobedeli<100)
{
kargo.setText("Kargo Bedeli: "+Double.toString(kargobedeli)+" TL");
toplam.setText("Toplam Bedel: " +Double.toString(sepetbedeli+kargobedeli)+" TL");
kargotext.setText(Double.toString(100-sepetbedeli)+" TL kadar daha alışveriş yaparsanız kargo bizden");
}
else
{
kargo.setText("Kargo Bedeli: 0 TL");
toplam.setText("Toplam Bedel: " +Double.toString(sepetbedeli)+" TL");
kargotext.setText("Kargo bizden");
System.out.println("Toplam: "+toplambedel);
}
}
private void Kontroller() {
sepet = (ImageButton)findViewById(R.id.imageButton1);
devam = (ImageButton)findViewById(R.id.imageButton2);
uyelik =(Button) findViewById(R.id.uyelikBilgileriniz);
hakkimizda =(Button) findViewById(R.id.hakkimizda);
uyelik.setOnClickListener(this);
hakkimizda.setOnClickListener(this);
devam.setOnClickListener(this);
sepet.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.imageButton2:
try {
if(isCouponExists==false)
{
couponid=-1;
}
else
{
couponid=1;
}
System.out.println(toplambedel);
Intent intent = new Intent(this, Class.forName("com.Troyateck.sucukevim.Payment"));
intent.putExtra("ad",ad);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
} catch (ClassNotFoundException e) {
Toast.makeText(this, "Hata : " + e.toString(),
Toast.LENGTH_SHORT).show();
}
break;
}
}
public void preparetext()
{
textfield=new ArrayList<String>();
for (int i = 0; i < userCart.size(); i++) {
textfield.add(userCart.get(i).urunadi);
}
}
public void preparefiyat()
{
pricefield=new ArrayList<String>();
for (int i = 0; i < userCart.size(); i++) {
pricefield.add(userCart.get(i).fiyat);
}
}
public void preparekisa()
{
System.out.println(userCart.size());
shortfield=new ArrayList<String>();
for (int i = 0; i < userCart.size(); i++) {
shortfield.add(userCart.get(i).detay);
}
}
public void prepareadet()
{
adetfield=new ArrayList<String>();
for (int i = 0; i < userCart.size(); i++) {
adetfield.add(userCart.get(i).adet);
}
}
public void preparebirim()
{
birimfield=new ArrayList<String>();
for (int i = 0; i < userCart.size(); i++) {
birimfield.add(userCart.get(i).birim);
System.out.println(userCart.get(i).birim);
}
}
public void prepareimage() throws InterruptedException, ExecutionException
{
imagefield=new ArrayList<Drawable>();
for (int i = 0; i < userCart.size(); i++) {
LoadImageFromWeb get = new LoadImageFromWeb();
Drawable res = get.execute(new String[] { userCart.get(i).resim }).get();
imagefield.add(res);
}
}
private class LoadImageFromWeb extends AsyncTask<String, Drawable, Drawable> {
@Override
protected Drawable doInBackground(String... params) {
try
{
InputStream is = (InputStream) new URL(params[0]).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e.getMessage());
return null;
}
}
}
private class CustomAdapter extends ArrayAdapter<Object>
{
Activity activity;
ArrayList<String> textfield;
ArrayList<String> adetfield;
ArrayList<String> birimfield;
ArrayList<String> pricefield;
ArrayList<String> shortfield;
ArrayList<Drawable> imagefield;
public CustomAdapter(Activity context, ArrayList<String> name,ArrayList<Drawable> image,ArrayList<String> adetfield,ArrayList<String> birimfield,ArrayList<String> pricefield,ArrayList<String> shortfield)
{
super(context, 0);
activity=context;
this.textfield=name;
this.imagefield=image;
this.adetfield=adetfield;
this.birimfield=birimfield;
this.pricefield=pricefield;
this.shortfield=shortfield;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imgViewFlag;
TextView txtViewTitle;
TextView txtViewFiyat;
TextView txtViewDetay;
TextView txtViewAdet;
TextView txtViewBirim;
LayoutInflater inflator = activity.getLayoutInflater();
convertView = inflator.inflate(R.layout.cartgrid, null);
txtViewTitle = (TextView) convertView.findViewById(R.id.baslik);
txtViewAdet = (TextView) convertView.findViewById(R.id.textView1);
txtViewFiyat=(TextView)convertView.findViewById(R.id.fiyat);
txtViewDetay=(TextView)convertView.findViewById(R.id.aciklama);
txtViewBirim = (TextView) convertView.findViewById(R.id.textView2);
imgViewFlag = (ImageView) convertView.findViewById(R.id.resim);
txtViewTitle.setText(textfield.get(position));
txtViewAdet.setText(adetfield.get(position));
txtViewFiyat.setText(pricefield.get(position));
txtViewDetay.setText(shortfield.get(position));
txtViewBirim.setText(birimfield.get(position));
imgViewFlag.setImageDrawable(imagefield.get(position));
return convertView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return textfield.size();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return textfield.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cart, menu);
return true;
}
public void AddToCart(int id,String ad,String adet,String resim,String fiyat,String detay,String birim)
{
System.out.println("job2"+" "+id+" "+ad+" "+adet+" "+resim+" "+adet+" "+birim);
userCart.add(new CartSystem(id,ad,adet,resim,fiyat,detay,birim));
System.out.println(userCart.size());
}
public class CartSystem
{
public CartSystem()
{
}
public CartSystem(int id,String urunadi,String adet,String resim,String fiyat,String detay,String birim)
{
this.id=id;
this.urunadi=urunadi;
this.adet=adet;
this.resim=resim;
this.birim=birim;
this.fiyat=fiyat;
this.detay=detay;
}
public int id;
public String urunadi;
public String adet;
public String fiyat;
public String detay;
public String birim;
public String resim;
}
public static void setprice(Double price) {
Cart.toplambedel = price;
}
public static String getprice() {
return Double.toString(toplambedel);
}
}