简体   繁体   English

如何在数据库处理程序类中调用Add方法

[英]How to call an Add Method in a Database Handler Class

Hey so this is my first post but I am absolutely stuck. 嘿,这是我的第一篇文章,但我绝对被困住了。 Either I don't understand database (which i really am kinda slapping this together to try and learn) I have created a basic CRUD DBHandlerclass with a method to add a plane. 要么我不了解数据库(我真的很想把它拍下来尝试学习),但是我已经创建了一个基本的CRUD DBHandler类,并带有添加平面的方法。 and a Activity that allows the user to input the info to save to the database. 以及一个允许用户输入要保存到数据库的信息的活动。 I really appriciate any pointers, hints, or if you can show me a good tutorial. 我真的很喜欢任何指针,提示,或者如果您可以给我看一个好的教程的话。 the tutorial that I used to build this database I found at http://mobilesiri.com/android-sqlite-database-tutorial-using-android-studio/ http://mobilesiri.com/android-sqlite-database-tutorial-using-android-studio/找到的用于构建该数据库的教程

here is the DBHandler class. 这是DBHandler类。

   public class DBHandler extends SQLiteOpenHelper {

//Database Version
private static final int DATABASE_VERSION = 1;
//Database Name
private static final String DATABASE_NAME = "avtoolsInfo";
//Plane info table name
private static final String TABLE_PLANES = "planeInfo";
// planes table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_TYPE = "type";
private static final String KEY_PLANE_CLASS = "plane_class";
private static final String KEY_FUEL_STYLE = "fuel_style";
private static final String KEY_NOTES = "notes";




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

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_PLANE_TABLE = "CREATE TABLE " + TABLE_PLANES + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_TYPE + " TEXT," + KEY_PLANE_CLASS + " TEXT," + KEY_FUEL_STYLE + " TEXT," + KEY_NOTES + " TEXT" + ")";
    db.execSQL(CREATE_PLANE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    //Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLANES);
    //Creating tables again
    onCreate(db);
}

// Adding a new plane
public void addPlane(PlaneInfo planeInfo)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, planeInfo.getName()); //plane name
    values.put(KEY_TYPE, planeInfo.getType()); //plane type
    values.put(KEY_PLANE_CLASS, planeInfo.getPlaneClass()); //plane class
    values.put(KEY_FUEL_STYLE, planeInfo.getFuelStyle());//plane fuel style
    values.put(KEY_NOTES, planeInfo.getNotes());//plane notes
// Inserting Row
    db.insert(TABLE_PLANES, null, values);
// Closeing Database
    db.close();

Here is the activity to add. 这是要添加的活动。 [edit. [编辑。 this is the layout file] 这是布局文件]

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="60dp">

<android.support.design.widget.TextInputLayout
    android:id="@+id/input_layout_plane_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/input_plane_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/plane_name"
        android:maxLines="1" />

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/input_plane_type"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/input_layout_plane_name">

    <EditText
        android:id="@+id/plane_type"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/plane_type" />

</android.support.design.widget.TextInputLayout>

    <TextView
        android:id="@+id/plane_class_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/input_plane_type"
        android:layout_alignParentLeft="true"
        android:text="@string/plane_class_textview"/>

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/plane_class_text"
        android:layout_below="@id/input_plane_type"
        ></Spinner>

    <TextView
        android:id="@+id/fuel_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/spinner"
        android:text="@string/fueling_type_textview"/>
    <Spinner
        android:id="@+id/spinner2"
        android:layout_below="@id/spinner"
        android:layout_toRightOf="@id/fuel_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">


    </Spinner>





    <Button
        android:id="@+id/save_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/save_button"
        android:onClick="addPlane"/>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/input_plane_notes"
        android:layout_alignParentBottom="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/spinner2"
        android:layout_above="@id/save_button">

        <EditText
            android:id="@+id/plane_notes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/plane_user_notes" />

    </android.support.design.widget.TextInputLayout>

</RelativeLayout>

I really apriciate all the help. 我真的很感谢所有帮助。

[edit here is the add class] [这里是添加类的编辑]

    package com.example.jerem.avtools;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.graphics.drawable.DrawerArrowDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class AddNewPlane extends AppCompatActivity {

    Spinner spinner2;
    Spinner spinner;
    ArrayAdapter<CharSequence> adapter;
    ArrayAdapter<CharSequence> adapter2;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_new_plane);
        spinner = (Spinner) findViewById(R.id.spinner);
        adapter = ArrayAdapter.createFromResource(this,R.array.plane_class_spinner_data,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+" is selected",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        spinner2 = (Spinner) findViewById(R.id.spinner2);
        adapter2 = ArrayAdapter.createFromResource(this,R.array.fueling_styles,android.R.layout.simple_spinner_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter2);
    }
}

I feel like that link shows how to use that class... Anyways, here 我觉得该链接显示了如何使用该类...无论如何,这里

public class AddNewPlane extends AppCompatActivity {

    Spinner spinner2;
    Spinner spinner;
    ArrayAdapter<CharSequence> adapter;
    ArrayAdapter<CharSequence> adapter2;

    DBHandler dbHandler; // add field

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_new_plane);

        dbHandler = new DbHandler(this); // initialize

  } 

    public void addPlane(View view) {
        PlaneInfo plane = new PlaneInfo();

        EditText edtPlaneType = (EditText) findViewById(R.id.plane_type);
        plane.setType(edtPlaneType.getText().toString());

        // TODO: Set more properties

        dbHandler.addPlane(plane);
    }
}

Regarding the id value, you should return the result of the insertion of the database handler. 关于id值,您应该return插入数据库处理程序的结果。 That will return the row number of the inserted record. 这将返回插入记录的行号。 You then can set the ID value on the PlaneInfo object from that. 然后,您可以由此在PlaneInfo对象上设置ID值。

the spinners threw me for a loop. 旋转器把我扔了一圈。 took a bit to figure them out but this is what i got thanks for your help guys. 花了一点时间弄清楚它们,但这就是我感谢您的帮助人员。

public void addPlane(View view){
    PlaneInfo plane = new PlaneInfo();

    EditText editPlaneName = (EditText) findViewById(R.id.input_plane_name);
    plane.setType(editPlaneName.getText().toString());

    EditText editPlaneType = (EditText) findViewById(R.id.plane_type);
    plane.setType(editPlaneType.getText().toString());

    Spinner spinnerPlaneClass = (Spinner) findViewById(R.id.spinner);
    plane.setType(spinnerPlaneClass.getSelectedItem().toString());

    Spinner spinner1PlaneStyle = (Spinner) findViewById(R.id.spinner2);
    plane.setType(spinner1PlaneStyle.getSelectedItem().toString());

    EditText editPlaneNotes = (EditText) findViewById(R.id.plane_notes);
    plane.setType(editPlaneNotes.getText().toString());

    dbHandler.addPlane(plane);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM