我想从数据库中获取名称列并在微调器上显示

时间:2016-01-16 19:24:25

标签: android

我想从数据库中获取名称列并在微调器上显示,当我点击名称时,它必须在textview中显示该名称的所有其他行详细信息我有一个微调器和4个textview,我想在微调器中显示名称textview中的其他细节

Entry.java:

public class Entry extends AppCompatActivity {
    Button btnsbmt1;
    Spinner spiner2;
    EditText milkcaret,amount, amountp,bal;


    DatabaseHelper mydb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entry);
        mydb=new DatabaseHelper(this);

        btnsbmt1=(Button)findViewById(R.id.btnsubmit2);
        spiner2=(Spinner)findViewById(R.id.spinner2);
        milkcaret=(EditText)findViewById(R.id.txt1_de);
        amount=(EditText)findViewById(R.id.txt2_de);
        amountp=(EditText)findViewById(R.id.txt3_de);
        bal=(EditText)findViewById(R.id.txt4_de);




        adddata2();

    }



    public void adddata2(){
        btnsbmt1.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean inserteddata = mydb.insertdatat(milkcaret.getText().toString(),amount.getText().toString(),amountp.getText().toString(),bal.getText().toString());

                        if (inserteddata)
                            Toast.makeText(Entry.this, "data inserted", Toast.LENGTH_SHORT).show();
                        else {
                            Toast.makeText(Entry.this, "database error", Toast.LENGTH_SHORT).show();
                        }

                    }
                }
        );
    }
}

databasehelper.java

    public class DatabaseHelper extends SQLiteOpenHelper {

        public static final String DATABASE_NAME="Sales.db";
        public static final String TABLE_NAME1="Insert_table";
        public static final String TABLE_NAME2="Entry_table";




        public static final String COL_ID="ID";
        public static final String Col_NAME="NAME";



        public static final String Col_CARET="CARET";
        public static final String Col_AMOUNT="AMOUNT";
        public static final String Col_AMOUNT_PAID="AMOUNT_PAID";
        public static final String Col_BALANCE="BALANCE";




        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, 1);


        }


        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table " + TABLE_NAME1 + " (ID_NAME INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT)");
            db.execSQL("create table " + TABLE_NAME2 + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,CARET INTEGER, AMOUNT INTEGER,AMOUNT_PAID INTEGER,BALANCE INTEGER,FOREIGN KEY(ID) REFERENCES Insert_table(ID_NAME))");

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME1);
            db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME2);
            onCreate(db);

        }
        public boolean insertdata(String name){
            SQLiteDatabase db=this.getWritableDatabase();
            ContentValues contentValues =new ContentValues();
            contentValues.put(Col_NAME, name);

            long result= db.insert(TABLE_NAME1,null,contentValues);
            if(result== -1)
                return false;
            else
                return true;
        }


        public boolean insertdatat(String caret,String amount,String amount_paid,String balance ){
            SQLiteDatabase db=this.getWritableDatabase();
            ContentValues contentValues =new ContentValues();

            contentValues.put(Col_CARET,caret);
            contentValues.put(Col_AMOUNT,amount);
            contentValues.put(Col_AMOUNT_PAID,amount_paid);
            contentValues.put(Col_BALANCE,balance);

            long result= db.insert(TABLE_NAME2,null,contentValues);
            if(result== -1)
                return false;
            else
                return true;
        }




    }

0 个答案:

没有答案