notifyDatasetChanged不适用于listview

时间:2016-01-25 11:04:34

标签: android sqlite listview baseadapter

我的ListView包含每行中包含删除按钮的行。

单击“删除”按钮必须从Sqlite数据库中删除特定行并刷新列表视图。

问题

我可以从数据库中删除行。但是删除行后我无法刷新页面。

为了刷新我尝试使用notifyDatasetChanged(),但没有运气。

如果我回到之前的活动并回到相同的活动,我能够看到新列表。

请在下面找到adapter和类别代码。

MainClass

public class AddToCart extends AppCompatActivity {

ListView cart_list;
DatabaseHandler db = new DatabaseHandler(this);
Cursor todoCursor;
Context context;
String name, price, image, model;
ArrayList<String> bitmapArray = new ArrayList<String>();
ArrayList<String> list_name = new ArrayList<String>();
ArrayList<String> list_price = new ArrayList<String>();
ArrayList<String> list_model = new ArrayList<String>();
Toolbar toolbar;
Button checkout;
static CustomAdapter_cart customAdapter_cart;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addtocart);
    context = this;
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    cart_list = (ListView) findViewById(R.id.listview_cart);

    checkout = (Button) findViewById(R.id.checkout);
    checkout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(AddToCart.this, Signup.class);
            startActivity(in);
        }
    });

    Intent i = getIntent();

    // Get access to the underlying writeable database

    // Query for items from the database and get a cursor back
    // todoCursor = db1.rawQuery("SELECT id as _id, * from cart ", null);
    // db1.execSQL("DELETE FROM cart");
    // Reading all contacts
    List<Cart> contacts = db.getAllContacts();

    cart_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });

    for (Cart cn : contacts) {
        name = cn.getName();
        list_name.add(name);
        price = cn.getPhoneNumber();
        list_price.add(price);
        image = cn.getImage_list();
        bitmapArray.add(image);
        model = cn.getModel();
        list_model.add(model);


    }
    customAdapter_cart = new CustomAdapter_cart(this, list_name, list_price, bitmapArray, list_model);
    cart_list.setAdapter(customAdapter_cart);


}


public static class MyLovelyOnClickListener implements View.OnClickListener

{
    CustomAdapter_cart contextnew;
    String myLovelyVariable;

    Context context;


    public MyLovelyOnClickListener(Context contextnew, CustomAdapter_cart context, String tv_model) {

        this.contextnew = context;
        this.context = contextnew;
        this.myLovelyVariable = tv_model;
    }

    @Override
    public void onClick(View v) {


        final DatabaseHandler db = new DatabaseHandler(context);

        SQLiteDatabase db1 = db.getWritableDatabase();
        try {
            db1.delete("cart", "model = ?", new String[]{myLovelyVariable});

            customAdapter_cart.notifyDataSetChanged();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
    }


}

CustomAdapter.class

public class CustomAdapter_cart extends BaseAdapter  {
ArrayList<String> list_name = new ArrayList<String>();
ArrayList<String> list_price = new ArrayList<String>();
ArrayList<String> list_images = new ArrayList<String>();
ArrayList<String> list_model = new ArrayList<String>();
CustomAdapter_cart cart_refresh;
Bitmap b;
View rowView;
Context context;
AddToCart cart;
private static LayoutInflater inflater = null;
Cursor cu;
Context contextnew;
AddToCart.MyLovelyOnClickListener listener;



String model_item;


public CustomAdapter_cart(Context context, ArrayList<String> list_name, ArrayList<String> list_price, ArrayList<String> bitmapArray, ArrayList<String> list_model) {
    this.context = context;
    this.list_name = list_name;
    this.list_price = list_price;
    this.list_images = bitmapArray;
    this.list_model = list_model;
    this.cart_refresh = this;

    inflater = LayoutInflater.from(context);
}

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

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

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



public class Holder {
    TextView tv_name, tv_price, tv_model;
    ImageView image;
    Button delete;

}

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


    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    final Holder holder = new Holder();

    rowView = inflater.inflate(R.layout.list_items_cart, null);
    holder.tv_name = (TextView) rowView.findViewById(R.id.name_cart);
    holder.tv_price = (TextView) rowView.findViewById(R.id.price_cart);
    holder.image = (ImageView) rowView.findViewById(R.id.image_cart);
    holder.tv_model = (TextView) rowView.findViewById(R.id.model_cart);
    holder.delete = (Button) rowView.findViewById(R.id.delete);




    holder.tv_name.setText(list_name.get(position));

    holder.tv_price.setText(list_price.get(position));
    holder.tv_model.setText(list_model.get(position));
    String n = holder.tv_model.getText().toString();

    holder.image.setImageBitmap(loadImageFromStorage(list_images.get(position)));


   // holder.delete.setTag(holder.tv_model);

    listener = new AddToCart.MyLovelyOnClickListener(context,CustomAdapter_cart.this,n);
    holder.delete.setOnClickListener(listener);


    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name_item = ((TextView) rowView.findViewById(R.id.name_cart)).getText().toString();
            String price_item = ((TextView) rowView.findViewById(R.id.price_cart)).getText().toString();
            model_item = ((TextView) rowView.findViewById(R.id.model_cart)).getText().toString();

            Intent in = new Intent(context, AddCart_FullImage.class);
            in.putExtra("model", model_item);
            in.putExtra("name",  name_item);
            in.putExtra("price", price_item);
            context.startActivity(in);
        }
    });
    return rowView;
}

private Bitmap loadImageFromStorage(String path) {

    try {
        File f = new File(path, "");
        f.canRead();
        b = BitmapFactory.decodeStream(new FileInputStream(f));
        return b;


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

任何帮助都会非常有用。

感谢。

1 个答案:

答案 0 :(得分:3)

您只是从数据库中删除一行。您的适配器仍具有相同的数据集。因此,首先更新适配器的数据集,然后调用notifydatasetchanged()