在SQlite数据库中添加新行,然后在列表视图中

时间:2019-01-30 07:04:52

标签: java android android-listview android-sqlite

我正在构建一个移动应用程序,但是我仍然遇到这个问题。我做了一个列表视图,它从SQlite表中获取了所有数据。直到昨晚一切正常,但今天通过在数据库中添加新行来重复保存的所有行。行仅在列表视图中重复,而不在SQLite中重复。当我打开应用程序时,一切都很好,在按下对话框中的添加按钮后,它会重复执行。

我进行了很多搜索,但找不到解决方案。

这是我在列表视图中添加新项目的代码:

alertDialog.setPositiveButton("Save", new 
DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        if (input.length() != 0 && inputBlnc.length() != 0) {
                            String result = input.getText().toString();
                            input.setText("");
                            String resultBlnc = inputBlnc.getText().toString();
                            inputBlnc.setText("");
                            addDataInAcc(result, resultBlnc);
                            dialog.dismiss();
                            Cursor data = mdatabaseHelper.getData();
                            if (data != null) {
                                while (data.moveToNext()) {
                                    listRec.add(new ModelAccounts(data.getString(1), data.getString(3)));
                                    Log.d(TAG, "ADDING " + data.getString(1) + " to ARRAY");
                                }
                                customAdapter = new AccountsAdapter(getApplicationContext(), listRec);
                                customAdapter.notifyDataSetChanged();
                                listView.setAdapter(customAdapter);
                            } else {
                                Toast.makeText(AccountsActivity.this, "Create a new account", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(AccountsActivity.this, "Please enter both things", Toast.LENGTH_SHORT).show();
                        }
                    }catch (Exception exc){
                        Toast.makeText(AccountsActivity.this, ""+ exc.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });

DataBaseHelper

   public boolean addDataInAccounts(String accountName, String opBlnc)
   {


    SQLiteDatabase db1 = this.getWritableDatabase() ;


    ContentValues contentValues1 = new ContentValues() ;

    contentValues1.put(TABLE_COL2, accountName);

    contentValues1.put(TABLE_COL4, opBlnc);

    Log.d(TAG, "Add Data : Adding " + accountName + " and " + opBlnc + " to " + TABLE_NAME) ;

    long result = db1.insert(TABLE_NAME, null, contentValues1) ;

    if (result == -1){
        return false ;
    }else {
        return true ;
    }
}

完整代码:DataBaseHelper类:

package com.example.fahad.softixtechnologies.Helpers;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import com.example.fahad.softixtechnologies.models.ModelAccounts;

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String TAG = "DatabaseHelper" ;
private static final String TAG1 = "DatabaseHelper" ;

public static final String DB_NAME = "softix.db" ;
public static final String TABLE_NAME = "Account_Master" ;
public static final String TABLE_COL4 = "op_blnc" ;
public static final String TABLE_COL1 = "acc_id" ;
public static final String TABLE_COL2 = "acc_title" ;
public static final String TABLE_COL3 = "group_id" ;
public static final String TABLE1_NAME = "groups" ;
public static final String TABLE2_NAME = "users" ;
public static final int DB_VERSION = 2 ;
private final Context context ;

public DatabaseHelper(Context context){
    super(context, DB_NAME, null, DB_VERSION);
    this.context=context ;
}

@Override
public void onCreate(SQLiteDatabase db) {

    String groupsTabledb = "CREATE TABLE " + TABLE1_NAME + " " +
            "(group_id INTEGER PRIMARY KEY AUTOINCREMENT, group_title VARCHAR(30))" ;
    String accountsTabledb = "CREATE TABLE " + TABLE_NAME + " " +
            "(" + TABLE_COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TABLE_COL2 + " VARCHAR(50), " +
            TABLE_COL3 + " INTEGER, " + TABLE_COL4 + " VARCHAR(50))" ;
    String usersTabledb = "CREATE TABLE " + TABLE2_NAME + " " +
            "(user_id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT)" ;

    try {
        db.execSQL(groupsTabledb);
        db.execSQL(accountsTabledb);
        db.execSQL(usersTabledb);
    }catch (Exception e){
        Log.d(TAG1, e.getMessage());
    }
}

public boolean addUser(String name, String password){

    SQLiteDatabase db = getWritableDatabase() ;
    ContentValues contentValues = new ContentValues() ;
    contentValues.put("username", name);
    contentValues.put("password", password);
    long result = db.insert(TABLE2_NAME,null, contentValues) ;

    if (result == -1){
        return false ;
    }else {
        return true ;
    }
}

public boolean addDataInAccounts(String accountName, String opBlnc){

    SQLiteDatabase db1 = this.getWritableDatabase() ;
    ContentValues contentValues1 = new ContentValues() ;
    contentValues1.put(TABLE_COL2, accountName);
    contentValues1.put(TABLE_COL4, opBlnc);

    Log.d(TAG, "Add Data : Adding " + accountName + " and " + opBlnc + " to " + TABLE_NAME) ;

    long result = db1.insert(TABLE_NAME, null, contentValues1) ;

    if (result == -1){
        return false ;
    }else {
        return true ;
    }
}
public boolean checkUsernamePassword(String username,String password){
    SQLiteDatabase db = this.getReadableDatabase() ;
    Cursor cursor = db.rawQuery("Select * from " + TABLE2_NAME +
            " where username=? and password=?", new String[]{username,password}) ;
    if (cursor.getCount()>0){return true;}else {return false;}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String groupsTabledb = "DROP TABLE IF EXISTS " + TABLE1_NAME ;
    String accountsTabledb = "DROP TABLE IF EXISTS " + TABLE_NAME ;
    String usersTabledb = "DROP TABLE IF EXISTS " + TABLE2_NAME ;
    if (oldVersion == 2) {
        db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + TABLE_COL4 + " VARCHAR(60)");
    }else{
        Toast.makeText(context, "Data not upgraded", Toast.LENGTH_SHORT).show();
    }
    db.execSQL(groupsTabledb);
    db.execSQL(accountsTabledb);
    db.execSQL(usersTabledb);

    onCreate(db);
}
public Cursor getData(){
    SQLiteDatabase database = this.getWritableDatabase() ;
    String query = "SELECT * FROM " + TABLE_NAME ;
    Cursor datas = database.rawQuery(query, null) ;
    return datas ;
}

public void deleteRow(ModelAccounts nameAcc){
    SQLiteDatabase database = this.getWritableDatabase() ;
    database.delete(TABLE_NAME, TABLE_COL2 + "= ?",
            new String[]{String.valueOf(nameAcc.getAccName())});
    database.close();
}

}

1 个答案:

答案 0 :(得分:1)

清除现有列表并设置新列表

    Cursor data = mdatabaseHelper.getData();
                                if (data != null) {
       listRec=new ArrayList(ModelAccounts); // create new instance or clear existing list

                                    while (data.moveToNext()) {
                                        listRec.add(new ModelAccounts(data.getString(1), data.getString(3)));
                                        Log.d(TAG, "ADDING " + data.getString(1) + " to ARRAY");
                                    }
                                    customAdapter = new AccountsAdapter(getApplicationContext(), listRec);
                                    customAdapter.notifyDataSetChanged();
                                    listView.setAdapter(customAdapter);
                                } else {
                                    Toast.makeText(AccountsActivity.this, "Create a new account", Toast.LENGTH_SHORT).show();
                                }

或者您可以从数据库中获取最后插入的记录并设置为现有列表

  Ex.
    listRec.add(new ModelAccounts(data.getString(1), data.getString(3)));
customAdapter.notifyDataSetChanged();

和刷新适配器。