无法执行android的方法:onClick。添加到数据库

时间:2017-07-07 13:38:27

标签: java android database

单击按钮时无法执行方法。我想在点击时添加项目到SQLLite数据库,删除它,更新并显示它。

这是我的代码。

public class MainActivity2 extends AppCompatActivity {
TextView textVieww;
DBHelper mydb;
EditText talentname;
Spinner spinner;

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

    final String[] talentlvl = {
            "1 Level",
            "3 Level",
            "7 Level",
            "10 Level",
            "13 Level",
            "16 Level",
            "20 Level",

    };
    spinner = (Spinner)findViewById(R.id.spinner);

    CustomSpinnerAdapter spinnerAdapter = new CustomSpinnerAdapter(this, talentlvl);
    spinner.setAdapter(spinnerAdapter);

    talentname = (EditText) findViewById(R.id.talentname_input);
    textVieww = (TextView) findViewById(R.id.textView2);

    mydb = new DBHelper(this,"",null,1);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity2.this, talentlvl[position], Toast.LENGTH_SHORT).show();
        }

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

        }
    });


}
public void btn_click(View view)
{
    switch (view.getId())
    {

        case R.id.btn_add:
            try {
                mydb.insert_talents(spinner.getSelectedItem().toString(), talentname.getText().toString());
            }
            catch (SQLException e)
            {
            }
            break;
        case  R.id.btn_delete:
            mydb.delete_talents(talentname.getText().toString());
            break;
        case R.id.btn_uptade:
            AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity2.this);
            dialog.setTitle("ENTER NEW TALENTNAME ");

            final EditText new_talentname = new EditText(this);
            dialog.setView(new_talentname);

            dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mydb.update_talents(talentname.getText().toString(),new_talentname.getText().toString());
                }
            });

            dialog.show();
            break;
        case R.id.textView:

            mydb.list_all_talents(textVieww);
            break;
    }
}}

DataBase代码:

public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, "TALENTS.db", factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE TALENTS( ID INTEGER PRIMARY KEY AUTOINCREMENT, TALENTLVL TEXT UNIQUE, TALENTNAME TEXT); ");

}

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

public void insert_talents(String talentlvl,String talentname)
{
    ContentValues contentValues = new ContentValues();
    contentValues.put("TALENTLVL",talentlvl);
    contentValues.put("TALENTNAME",talentname);
    this.getWritableDatabase().insertOrThrow("TALENTS","",contentValues);
}

public void delete_talents(String talentname)
{
    this.getWritableDatabase().delete("TALENTS","TALENTNAME='"+talentname+"'",null);
}

public void update_talents(String old_talentname,String new_talentname)
{
    this.getWritableDatabase().execSQL("UPDATE TALENTS SET TALENTNAME='"+new_talentname+"WHERE TALENTNAME='"+old_talentname+"'");
}

public void list_all_talents(TextView textView)
{
    Cursor cursor = this.getReadableDatabase().rawQuery("SELECT * FROM TALENTS",null);
    textView.setText("");
    while (cursor.moveToNext())
    {
        textView.append(cursor.getString(1)+" "+cursor.getString(2));
    }
}}

抱歉,在编辑之前我添加了错误的课程:(

0 个答案:

没有答案