Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除项目后Android Listview未更新_Java_Android_Android Listview_Android Sqlite_Baseadapter - Fatal编程技术网

Java 删除项目后Android Listview未更新

Java 删除项目后Android Listview未更新,java,android,android-listview,android-sqlite,baseadapter,Java,Android,Android Listview,Android Sqlite,Baseadapter,我正在使用sqllite。我已经成功地创建了一个数据库,我可以在数据库中输入一些值。我还可以在listview中显示所有值,也可以通过listview的onitemclicklistener删除项。我有一个问题。删除项目时,listview未更新,但该项目已在数据库中删除。如何解决此问题 java代码 public class DatabaseHandler extends SQLiteOpenHelper { private static final int DATABASE_VERSIO

我正在使用sqllite。我已经成功地创建了一个数据库,我可以在数据库中输入一些值。我还可以在listview中显示所有值,也可以通过listview的onitemclicklistener删除项。我有一个问题。删除项目时,listview未更新,但该项目已在数据库中删除。如何解决此问题

java代码

public class DatabaseHandler extends SQLiteOpenHelper {


private static final int DATABASE_VERSION = 1;


private static final String DATABASE_NAME = "lvstone_2";


private static final String TABLE_CONTACTS = "CardTable1";


private static final String KEY_ID = "id";
private static final String KEY_Tittle = "name";
private static final String KEY_Description = "description";
private static final String KEY_Price = "price";

private static final String KEY_Counter = "counter";

private static final String KEY_Image = "image";
private final ArrayList<Contact> contact_list = new ArrayList<Contact>();
public static SQLiteDatabase db;

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_Tittle + " TEXT,"

            + KEY_Description + " TEXT,"

            + KEY_Price + " TEXT,"

            + KEY_Counter + " TEXT,"

            + KEY_Image + " TEXT"

            + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(db);
}

// Adding new contact
public void Add_Contact(Contact contact) {
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    if (!somethingExists(contact.getTitle())) {

        values.put(KEY_Tittle, contact.getTitle()); // Contact title
        values.put(KEY_Description, contact.getDescription()); // Contact//
                                                                // description

        values.put(KEY_Price, contact.getPrice()); // Contact price

        values.put(KEY_Counter, contact.getCounter()); // Contact image
        values.put(KEY_Image, contact.getImage()); // Contact image

        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);

        Log.e("Table Result isss", String.valueOf(values));
        db.close(); // Closing database connection

    }

}


public void deleteUser(String userName)
{
    db = this.getWritableDatabase();
    try
    {
        db.delete(TABLE_CONTACTS, "name = ?", new String[] { userName });
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        db.close();
    }
}

// Getting single contact
Contact Get_Contact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS,
            new String[] { KEY_ID, KEY_Tittle, KEY_Description, KEY_Price,
                    KEY_Counter, KEY_Image }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Contact contact = new Contact(cursor.getString(0), cursor.getString(1),
            cursor.getString(2), cursor.getString(4), cursor.getString(5));
    // return contact
    cursor.close();
    db.close();

    return contact;
}

public boolean somethingExists(String x) {
    Cursor cursor = db.rawQuery("select * from " + TABLE_CONTACTS
            + " where name like '%" + x + "%'", null);
    boolean exists = (cursor.getCount() > 0);

    Log.e("Databaseeeeeeeee", String.valueOf(cursor));
    cursor.close();
    return exists;
}

public ArrayList<Contact> Get_Contacts() {
    try {
        contact_list.clear();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();

                contact.setTitle(cursor.getString(1));

                contact.setDescription(cursor.getString(2));

                contact.setPrice(cursor.getString(3));
                contact.setCounter(cursor.getString(4));

                contact.setImage(cursor.getString(5));

                contact_list.add(contact);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return contact_list;
    } catch (Exception e) {
        // TODO: handle exception
        Log.e("all_contact", "" + e);
    }

    return contact_list;
}

public int getProfilesCount() {
    String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int cnt = cursor.getCount();
    cursor.close();
    return cnt;
}
public class StradaSQLAdapter extends BaseAdapter {
Activity activity;
int layoutResourceId;
Contact user;
ArrayList<Contact> data = new ArrayList<Contact>();
public ImageLoader imageLoader;
UserHolder holder = null;
public int itemSelected = 0;

public StradaSQLAdapter(Activity act, int layoutResourceId,
        ArrayList<Contact> data) {

    this.layoutResourceId = layoutResourceId;
    this.activity = act;
    this.data = data;
    imageLoader = new ImageLoader(act.getApplicationContext());
    notifyDataSetChanged();
}

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

    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        holder = new UserHolder();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder.Title = (TextView) row.findViewById(R.id.smalltitle1);
        holder.counter = (TextView) row.findViewById(R.id.smallCounter1);

        holder.dbcounter = (TextView) row
                .findViewById(R.id.DBSliderCounter);

        holder.Description = (TextView) row.findViewById(R.id.smallDesc1);

        holder.layout = (RelativeLayout) row
                .findViewById(R.id.DBSlideLayout);
        holder.layoutmain = (RelativeLayout) row
                .findViewById(R.id.DBSlideLayoutMain);
        holder.Price = (TextView) row.findViewById(R.id.smallPrice1);
        holder.pt = (ImageView) row.findViewById(R.id.smallthumb1);
        holder.close = (ImageView) row.findViewById(R.id.DBSliderClose);

        holder.c_minus = (ImageView) row.findViewById(R.id.counter_minus);

        holder.c_plus = (ImageView) row.findViewById(R.id.counter_plus);

        row.setTag(holder);
    } else {
        holder = (UserHolder) row.getTag();
    }
    user = data.get(position);

    holder.Title.setText(user.getTitle());
    holder.Description.setText(user.getDescription());
    holder.Price.setText(user.getPrice() + " GEL");

    holder.counter.setText(user.getCounter());

    holder.dbcounter.setText(user.getCounter());

    Log.e("image Url is........", data.get(position).toString());
    imageLoader.DisplayImage(user.getImage(), holder.pt);






    return row;

}

@Override
public int getCount() {

    return data.size();
}

@Override
public Object getItem(int position) {

    return data.get(position);
}

@Override
public long getItemId(int position) {

    return 0;
}

public class UserHolder {

    public TextView Price, counter, Description, Title, dbcounter;
    public ImageView pt,close,c_plus,c_minus;

    public RelativeLayout layout, layoutmain;

}
公共类DatabaseHandler扩展了SQLiteOpenHelper{
私有静态最终int数据库_VERSION=1;
私有静态最终字符串数据库\u NAME=“lvstone\u 2”;
私有静态最终字符串表\u CONTACTS=“CardTable1”;
私有静态最终字符串密钥\u ID=“ID”;
私有静态最终字符串密钥\u title=“name”;
私有静态最终字符串键\u Description=“Description”;
私有静态最终字符串键\u Price=“Price”;
私有静态最终字符串键\u Counter=“Counter”;
私有静态最终字符串键\u Image=“Image”;
private final ArrayList contact_list=新建ArrayList();
公共静态数据库sqlitedb;
公共数据库处理程序(上下文){
super(上下文、数据库名称、null、数据库版本);
}
//创建表
@凌驾
public void onCreate(SQLiteDatabase db){
字符串CREATE_CONTACTS_TABLE=“CREATE TABLE”+TABLE_CONTACTS+”(“
+键ID+“整数主键,”+键标题+“文本,”
+密钥描述+文本
+键+价格+文本
+按键计数器+“文本,”
+按键图像+“文本”
+ ")";
execSQL(创建联系人表);
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
db.execSQL(“如果存在删除表”+表_联系人);
//再次创建表
onCreate(db);
}
//添加新联系人
公共无效添加联系人(联系人联系人){
db=this.getWritableDatabase();
ContentValues=新的ContentValues();
如果(!somethingExists(contact.getTitle())){
value.put(KEY_title,contact.getTitle());//联系人标题
value.put(KEY_Description,contact.getDescription());//contact//
//描述
value.put(KEY_Price,contact.getPrice());//contact Price
value.put(KEY_Counter,contact.getCounter());//联系人图像
value.put(KEY_Image,contact.getImage());//contact Image
//插入行
db.插入(表_触点,空,值);
Log.e(“表结果isss”,String.valueOf(values));
db.close();//关闭数据库连接
}
}
public void deleteUser(字符串用户名)
{
db=this.getWritableDatabase();
尝试
{
db.delete(表_CONTACTS,“name=?”,新字符串[]{userName});
}
捕获(例外e)
{
e、 printStackTrace();
}
最后
{
db.close();
}
}
//获得单一联系
联系人获取联系人(int id){
SQLiteDatabase db=this.getReadableDatabase();
Cursor=db.query(表,
新字符串[]{KEY_ID,KEY_title,KEY_Description,KEY_Price,
按键计数器,按键图像},按键ID+“=?”,
新字符串[]{String.valueOf(id)},null,null,null);
如果(光标!=null)
cursor.moveToFirst();
联系人=新联系人(cursor.getString(0)、cursor.getString(1),
cursor.getString(2)、cursor.getString(4)、cursor.getString(5));
//回接
cursor.close();
db.close();
回接;
}
公共布尔somethingExists(字符串x){
Cursor Cursor=db.rawQuery(“选择*自”+表\u联系人
+其中名称类似“%”+x+“%”,null);
布尔存在=(cursor.getCount()>0);
Log.e(“databaseeee”,String.valueOf(cursor));
cursor.close();
回报存在;
}
public ArrayList Get_Contacts(){
试一试{
联系人列表。清除();
//选择所有查询
String selectQuery=“SELECT*FROM”+表格\联系人;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
触点=新触点();
contact.setTitle(cursor.getString(1));
contact.setDescription(cursor.getString(2));
contact.setPrice(cursor.getString(3));
contact.setCounter(cursor.getString(4));
contact.setImage(cursor.getString(5));
联系人列表。添加(联系人);
}while(cursor.moveToNext());
}
cursor.close();
db.close();
返回联系人列表;
}捕获(例外e){
//TODO:处理异常
Log.e(“所有联系人”和“+e”);
}
返回联系人列表;
}
public int getProfileScont(){
String countQuery=“SELECT*FROM”+表\u联系人;
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.rawQuery(countQuery,null);
int cnt=cursor.getCount();
cursor.close();
返回cnt;
}
}

SQLAdapter.java代码

public class DatabaseHandler extends SQLiteOpenHelper {


private static final int DATABASE_VERSION = 1;


private static final String DATABASE_NAME = "lvstone_2";


private static final String TABLE_CONTACTS = "CardTable1";


private static final String KEY_ID = "id";
private static final String KEY_Tittle = "name";
private static final String KEY_Description = "description";
private static final String KEY_Price = "price";

private static final String KEY_Counter = "counter";

private static final String KEY_Image = "image";
private final ArrayList<Contact> contact_list = new ArrayList<Contact>();
public static SQLiteDatabase db;

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_Tittle + " TEXT,"

            + KEY_Description + " TEXT,"

            + KEY_Price + " TEXT,"

            + KEY_Counter + " TEXT,"

            + KEY_Image + " TEXT"

            + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(db);
}

// Adding new contact
public void Add_Contact(Contact contact) {
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    if (!somethingExists(contact.getTitle())) {

        values.put(KEY_Tittle, contact.getTitle()); // Contact title
        values.put(KEY_Description, contact.getDescription()); // Contact//
                                                                // description

        values.put(KEY_Price, contact.getPrice()); // Contact price

        values.put(KEY_Counter, contact.getCounter()); // Contact image
        values.put(KEY_Image, contact.getImage()); // Contact image

        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);

        Log.e("Table Result isss", String.valueOf(values));
        db.close(); // Closing database connection

    }

}


public void deleteUser(String userName)
{
    db = this.getWritableDatabase();
    try
    {
        db.delete(TABLE_CONTACTS, "name = ?", new String[] { userName });
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        db.close();
    }
}

// Getting single contact
Contact Get_Contact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS,
            new String[] { KEY_ID, KEY_Tittle, KEY_Description, KEY_Price,
                    KEY_Counter, KEY_Image }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Contact contact = new Contact(cursor.getString(0), cursor.getString(1),
            cursor.getString(2), cursor.getString(4), cursor.getString(5));
    // return contact
    cursor.close();
    db.close();

    return contact;
}

public boolean somethingExists(String x) {
    Cursor cursor = db.rawQuery("select * from " + TABLE_CONTACTS
            + " where name like '%" + x + "%'", null);
    boolean exists = (cursor.getCount() > 0);

    Log.e("Databaseeeeeeeee", String.valueOf(cursor));
    cursor.close();
    return exists;
}

public ArrayList<Contact> Get_Contacts() {
    try {
        contact_list.clear();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();

                contact.setTitle(cursor.getString(1));

                contact.setDescription(cursor.getString(2));

                contact.setPrice(cursor.getString(3));
                contact.setCounter(cursor.getString(4));

                contact.setImage(cursor.getString(5));

                contact_list.add(contact);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return contact_list;
    } catch (Exception e) {
        // TODO: handle exception
        Log.e("all_contact", "" + e);
    }

    return contact_list;
}

public int getProfilesCount() {
    String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int cnt = cursor.getCount();
    cursor.close();
    return cnt;
}
public class StradaSQLAdapter extends BaseAdapter {
Activity activity;
int layoutResourceId;
Contact user;
ArrayList<Contact> data = new ArrayList<Contact>();
public ImageLoader imageLoader;
UserHolder holder = null;
public int itemSelected = 0;

public StradaSQLAdapter(Activity act, int layoutResourceId,
        ArrayList<Contact> data) {

    this.layoutResourceId = layoutResourceId;
    this.activity = act;
    this.data = data;
    imageLoader = new ImageLoader(act.getApplicationContext());
    notifyDataSetChanged();
}

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

    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        holder = new UserHolder();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder.Title = (TextView) row.findViewById(R.id.smalltitle1);
        holder.counter = (TextView) row.findViewById(R.id.smallCounter1);

        holder.dbcounter = (TextView) row
                .findViewById(R.id.DBSliderCounter);

        holder.Description = (TextView) row.findViewById(R.id.smallDesc1);

        holder.layout = (RelativeLayout) row
                .findViewById(R.id.DBSlideLayout);
        holder.layoutmain = (RelativeLayout) row
                .findViewById(R.id.DBSlideLayoutMain);
        holder.Price = (TextView) row.findViewById(R.id.smallPrice1);
        holder.pt = (ImageView) row.findViewById(R.id.smallthumb1);
        holder.close = (ImageView) row.findViewById(R.id.DBSliderClose);

        holder.c_minus = (ImageView) row.findViewById(R.id.counter_minus);

        holder.c_plus = (ImageView) row.findViewById(R.id.counter_plus);

        row.setTag(holder);
    } else {
        holder = (UserHolder) row.getTag();
    }
    user = data.get(position);

    holder.Title.setText(user.getTitle());
    holder.Description.setText(user.getDescription());
    holder.Price.setText(user.getPrice() + " GEL");

    holder.counter.setText(user.getCounter());

    holder.dbcounter.setText(user.getCounter());

    Log.e("image Url is........", data.get(position).toString());
    imageLoader.DisplayImage(user.getImage(), holder.pt);






    return row;

}

@Override
public int getCount() {

    return data.size();
}

@Override
public Object getItem(int position) {

    return data.get(position);
}

@Override
public long getItemId(int position) {

    return 0;
}

public class UserHolder {

    public TextView Price, counter, Description, Title, dbcounter;
    public ImageView pt,close,c_plus,c_minus;

    public RelativeLayout layout, layoutmain;

}
公共类StraaSqlAdapter扩展了BaseAdapter{
活动;
国际布局资源;
联系用户;
ArrayList数据=新的ArrayList();
公共图像加载器;
UserHolder=null;
公共int itemSelected=0;
公共设施(活动法、内部布局资源、,
ArrayList数据){
this.layoutResourceId=layoutResourceId;
这个活动=行动;
这个数据=数据;
imageLoader=新的imageLoader(act.getApplicationContext());
notifyDataSetChanged();
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视图行=转换视图;
if(行==null){
LayoutFlater充气机=LayoutFlater.from(活动);
holder=新用户holder();
行=充气机。充气(layoutResourceId,父级,false);
holder.Title=(TextView)row.f
public void removeObject (int position) {
     this.data.remove(position);
}
contact_data.remove(position);
cAdapter.removeObject(position);
cAdapter.notifyDataSetChanged();
contact_data.clear();
data.clear();