删除Listview中的项目时,Android Listview更新不起作用

删除Listview中的项目时,Android Listview更新不起作用,android,listview,Android,Listview,当我删除购物车中的物品或更新产品的数量时,我尝试更新我的列表视图,但它不起作用。我的错在哪里??我尝试使用notifyDataSetChanged();方法,但它不会立即起作用。我想在更改listview时刷新它。我怎么做?我需要在适配器类中编写一个方法吗 public class Cart extends Activity implements OnClickListener{ private ArrayList<String> textfield; pri

当我删除购物车中的物品或更新产品的数量时,我尝试更新我的列表视图,但它不起作用。我的错在哪里??我尝试使用notifyDataSetChanged();方法,但它不会立即起作用。我想在更改listview时刷新它。我怎么做?我需要在适配器类中编写一个方法吗

    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);
}
 }
公共类购物车扩展活动实现OnClickListener{
私有ArrayList文本字段;
私人ArrayList birimfield;
私人ArrayList adetfield;
private ArrayList pricefield;
私人ArrayList shortfield;
私有ArrayList imagefield;
专用自定义适配器;
公共静态双分离床;
公共静态双toplambedel;
公共静态int-couponid;
私有布尔值isCouponExists=false;
公共字符串adet;
公共静态最终双卡戈贝德利=6.0;
字符串广告;
文本视图问候语;
图像按钮分离,devam;
巴顿·乌耶利克,哈基米兹达;
public static ArrayList userCart=new ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
setContentView(R.layout.activity\u cart);
Bundle extras=getIntent().getExtras();
如果(附加值!=null){
ad=额外的获取字符串(“ad”);
问候语=(TextView)findViewById(R.id.greetingsText);
greeting.setText(“Hoşgeldiniz,”+extras.getString(“ad”);
}
对于(int i=0;i如果(separatbedeli+kargobedeli尝试listViewVariable.notify()。

在setAdapter()方法之后应该调用notifyDataSetChanged()方法。

这很难理解,请:1.尽可能使用CamelCase 2.只显示应用程序中发生数据更改的部分(可能提供指向完整代码的链接)3.尽可能使用英语而不是土耳其语
但是我的第一个猜测是您需要在
onClickListener
java.lang.Object.notify()中切换
gridView.setAdapter(customadapter);
同步方法,此处不太可能有帮助。