vendredi 26 juin 2015

Is there any way to restrict duplicate values getting populated inside spinner where values are coming from sqlite database once activity is started?

I am inserting some dummy values into sqlite databse and showing those values inside spinner , but the spinner is showing duplicate values once the activity starts again , i also tried using unique to the column but it says unique contraint failed. I want to know how to restrict the duplicate values getting populated inside my spinner.

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);
}

Aucun commentaire:

Enregistrer un commentaire