有没有任何方法可以限制重复值在微调器中填充,一旦活动启动,这些值就来自sqlite数据库



我在sqlite数据库中插入了一些伪值,并在微调器中显示这些值,但一旦活动再次启动,微调器就会显示重复的值,我也尝试对列使用unique,但它表示unique contint失败。我想知道如何限制在微调器中填充重复值。

DbListHelper.java

public static class DbListHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "Vehicle";
    private static final String TABLE_NAME_PUMP = "PumpDetails";
    private static final String TABLE_NAME_ACCOUNT  = "AccountDetails";
    private static final int DATABASE_VERSION = 1;
    private Context context;
    public static final String UID = "_id";
    public static final String ACCOUNT_TYPE = "account_type";
    public static final String ACCOUNT_NAME = "account_name";
    public static final String ACCOUNT_CODE = "account_code";
    public static final String ACCOUNT_OFFICE = "account_office";
    public static final String VEHICLE_NO = "vehicle_no";
    public static final String VEHICLE_CODE = "vehicle_code";
    public static final String REGISTRATION_NO = "registration_no";
    public static final String DRIVER_NAME = "driver_name";
    public static final String DRIVER_CODE = "driver_code";
    public static final String DIESEL_RATE = "diesel_rate";
    public static final String DIESEL_TYPE = "diesel_type";
    public static final String DIESEL_RATE_DATE = "diesel_rate_date";
    public static final String DIESEL_QUANTITY = "diesel_quantity";
    public static final String AMOUNT = "amount";
    public static final String REFERENCE_NO = "reference_no";
    public static final String NO_OF_COPIES = "no_of_copies";
    public static final String TRANSACTION_ID = "transcation_id";
    public static final String DATE = "date";
    public static final String TIME = "time";
    private static final String CREATE_TABLE_PUMP = "Create table 
    "+TABLE_NAME_PUMP+" (" +UID+" INTEGER PRIMARY KEY AUTOINCREMENT, 
    "+ACCOUNT_TYPE+" VARCHAR(500), "+VEHICLE_NO
    +" VARCHAR(255), "+DRIVER_NAME+" VARCHAR(500), "+DIESEL_RATE+" 
    VARCHAR(500), "+DIESEL_QUANTITY+" VARCHAR(500), "+AMOUNT+" VARCHAR(500), 
    "+REFERENCE_NO+" 
    VARCHAR(255), "+NO_OF_COPIES+" VARCHAR(255), "+DATE+" VARCHAR(255), 
    "+TIME+" VARCHAR(255), "+TRANSACTION_ID+" VARCHAR(500))";
    private static final String DROP_TABLE_PUMP = "Drop table If Exists 
    "+TABLE_NAME_PUMP;
    private static final String CREATE_TABLE_ACCOUNT = "Create table  
    "+TABLE_NAME_ACCOUNT+" (" +UID+" INTEGER PRIMARY KEY AUTOINCREMENT, 
    "+ACCOUNT_CODE+" VARCHAR(500) NOT NULL, "+ACCOUNT_NAME
            +" VARCHAR(255) NOT NULL UNIQUE, "+ACCOUNT_TYPE+" VARCHAR(255) 
    NOT NULL, "+NO_OF_COPIES+" VARCHAR(10) NOT NULL, "+ACCOUNT_OFFICE+" 
    VARCHAR(500) NOT NULL)";
    private static final String DROP_TABLE_ACCOUNT  = "Drop table If Exists  
    "+TABLE_NAME_ACCOUNT;
    public DbListHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
        // Message.message(context, "Constructor called");
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try {
            // Message.message(context, "onCreate called");
            db.execSQL(CREATE_TABLE_PUMP);
            db.execSQL(CREATE_TABLE_CASH);
            db.execSQL(CREATE_TABLE_DRIVER);
            db.execSQL(CREATE_TABLE_ACCOUNT);
            db.execSQL(CREATE_TABLE_VEHICLE);
            db.execSQL(CREATE_TABLE_FUEL);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)           
       {
        // TODO Auto-generated method stub
        try {
            // Message.message(context, "onUpgrage called");
            db.execSQL(DROP_TABLE_PUMP);
            db.execSQL(DROP_TABLE_CASH);
            db.execSQL(DROP_TABLE_DRIVER);
            db.execSQL(DROP_TABLE_ACCOUNT);
            db.execSQL(DROP_TABLE_VEHICLE);
            db.execSQL(DROP_TABLE_FUEL);
            onCreate(db);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

AddRecords.java

public class AddRecords extends Activity {
private SqliteVehicleDetails listHelper;
Spinner spinnerType, spinnerFuelRate, spinnerAccountName;
long accountid1, accountid2, accountid3, accountid4,
accountid5, accountid6, accountid7, accountid8, accountid9,
accountid10, accountid11, accountid12, accountid13, accountid14;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_records);
    listHelper = new SqliteVehicleDetails(getBaseContext());
    listHelper.open(getBaseContext());
    // //////////// Inserting account data /////////////////////////
    accountid1 = listHelper.insertAccountData("HPCL1", "Om Sai Petroleum",
            "Pump", "2", "Bandra");
    accountid2 = listHelper.insertAccountData("BPCL1", "Surya Petroleum",
            "Pump", "3", "Worli");
    accountid3 = listHelper.insertAccountData("IOCL1",
            "Poonam Auto Services", "Pump", "4", "Sewree");
    accountid4 = listHelper.insertAccountData("CV", "Cash Voucher", "Cash",
            "2", "Worli");
    accountid5 = listHelper.insertAccountData("TE", "Travelling Expense",
            "Cash", "2", "Bhandup");
    accountid6 = listHelper.insertAccountData("FE", "Food Expense", "Cash",
            "2", "Worli");
    accountid7 = listHelper.insertAccountData("VE", "Vehicle Expense",
            "Cash", "2", "JNPT");
    accountid8 = listHelper.insertAccountData("VM", "Vehicle Maintenance",
            "Cash", "2", "JNPT");
    accountid9 = listHelper.insertAccountData("PV", "Purchase Voucher",
            "Cash", "2", "Malad");
    accountid10 = listHelper.insertAccountData("PM", "Plant & Machinary",
            "Cash", "2", "Malad");
    accountid11 = listHelper.insertAccountData("LT", "Local taxes", "Cash",
            "2", "Worli");
    accountid12 = listHelper.insertAccountData("RE",
            "Registration Expense", "Cash", "2", "Andheri");
    accountid13 = listHelper.insertAccountData("IOCL2",
            "Prakash Expressway", "Pump", "2", "Khopoli");
    accountid14 = listHelper.insertAccountData("BPCL2",
            "Coco Vehicle Service", "Pump", "3", "Kaman");
    // //////////////// getting account type spinner data
    // ///////////////////
    cursor = listHelper.getAccountType();
    int[] to4 = new int[] { android.R.id.text1 };
    String[] from4 = new String[] { DbListHelper.ACCOUNT_TYPE };
    final SimpleCursorAdapter adapter4 = new SimpleCursorAdapter(
            getBaseContext(), android.R.layout.simple_list_item_1, cursor,
            from4, to4) {
        public View getView(int position, View convertView, ViewGroup 
        parent) {
            View v = super.getView(position, convertView, parent);
            ((TextView) v).setTextSize(18);
            ((TextView) v).setGravity(Gravity.CENTER);
            ((TextView) v).setTextColor(Color.parseColor("#1C689C"));
            return v;
        }
    };

    adapter4.setDropDownViewResource
    (android.R.layout.simple_spinner_dropdown_item);
    spinnerType.setAdapter(adapter4);
}

首先使用游标从数据库中获取数据,如果count为0,则检查count,然后插入伪数据,否则不插入伪数据。

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_records);
listHelper = new SqliteVehicleDetails(getBaseContext());
listHelper.open(getBaseContext());
cursor = listHelper.getAccountType();
 if(cursor.getCount()==0){
// //////////// Inserting account data /////////////////////////
accountid1 = listHelper.insertAccountData("HPCL1", "Om Sai Petroleum",
        "Pump", "2", "Bandra");
accountid2 = listHelper.insertAccountData("BPCL1", "Surya Petroleum",
        "Pump", "3", "Worli");
accountid3 = listHelper.insertAccountData("IOCL1",
        "Poonam Auto Services", "Pump", "4", "Sewree");
accountid4 = listHelper.insertAccountData("CV", "Cash Voucher", "Cash",
        "2", "Worli");
accountid5 = listHelper.insertAccountData("TE", "Travelling Expense",
        "Cash", "2", "Bhandup");
accountid6 = listHelper.insertAccountData("FE", "Food Expense", "Cash",
        "2", "Worli");
accountid7 = listHelper.insertAccountData("VE", "Vehicle Expense",
        "Cash", "2", "JNPT");
accountid8 = listHelper.insertAccountData("VM", "Vehicle Maintenance",
        "Cash", "2", "JNPT");
accountid9 = listHelper.insertAccountData("PV", "Purchase Voucher",
        "Cash", "2", "Malad");
accountid10 = listHelper.insertAccountData("PM", "Plant & Machinary",
        "Cash", "2", "Malad");
accountid11 = listHelper.insertAccountData("LT", "Local taxes", "Cash",
        "2", "Worli");
accountid12 = listHelper.insertAccountData("RE",
        "Registration Expense", "Cash", "2", "Andheri");
accountid13 = listHelper.insertAccountData("IOCL2",
        "Prakash Expressway", "Pump", "2", "Khopoli");
accountid14 = listHelper.insertAccountData("BPCL2",
        "Coco Vehicle Service", "Pump", "3", "Kaman");
   cursor = listHelper.getAccountType();
}
   //do your stuff
}

希望这对你有帮助。

最新更新