Android:按其ID更新数据

时间:2016-01-15 17:05:46

标签: android sqlite

我一直在尝试更新sqlite数据库上的数据,但在数据更新之前,用户将搜索数据的Id并将加载到Edittext。我的Logcat没有错误但是当我点击更新按钮时它只会显示toast消息,当我检查数据时,没有任何变化。我需要帮助..请检查我的密码。谢谢..

我的DatabaseHelper类:

  public void updateData(int id, String new_question, String new_ans1, String new_ans2, String new_ans3, String new_ans4, SQLiteDatabase sqLiteDatabase )
    {
        ContentValues contentValues = new ContentValues();

        contentValues.put(AdminContact.Question.COLUMN_QUESTION, new_question);
        contentValues.put(AdminContact.Question.COLUMN_ANSWER1, new_ans1);
        contentValues.put(AdminContact.Question.COLUMN_ANSWER2, new_ans2);
        contentValues.put(AdminContact.Question.COLUMN_ANSWER3, new_ans3);
        contentValues.put(AdminContact.Question.COLUMN_ANSWER4, new_ans4);


        sqLiteDatabase.update(AdminContact.Question.TABLE_QUIZ, contentValues, AdminContact.Question.ID +"="+id, null);

我的更新课程:

    EditText Search_Id, New_Question, New_Ans1, New_Ans2, New_Ans3, New_Ans4;
    DatabaseHelper helper;
    String SearchID, NewId, NewQuestion, NewAns1, NewAns2, NewAns3, NewAns4;
    SQLiteDatabase sqLiteDatabase;
    TextView title_text;
    Button updateButton;
    int id;

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

        Search_Id = (EditText) findViewById(R.id.id_search);
        New_Question = (EditText) findViewById(R.id.new_question);
        New_Ans1 = (EditText) findViewById(R.id.new_ans1);
        New_Ans2 = (EditText) findViewById(R.id.new_ans2);
        New_Ans3 = (EditText) findViewById(R.id.new_ans3);
        New_Ans4 = (EditText) findViewById(R.id.new_ans4);
        title_text = (TextView) findViewById(R.id.title_text);
        updateButton = (Button) findViewById(R.id.update_button);
        New_Question.setVisibility(View.GONE);
        New_Ans4.setVisibility(View.GONE);
        New_Ans3.setVisibility(View.GONE);
        New_Ans2.setVisibility(View.GONE);
        New_Ans1.setVisibility(View.GONE);
        title_text.setVisibility(View.GONE);
        updateButton.setVisibility(View.GONE);


        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.update_button) ;

                sqLiteDatabase = helper.getWritableDatabase();
                String question, ans1, ans2, ans3, ans4;

                question = New_Question.getText().toString();
                ans1 = New_Ans1.getText().toString();
                ans2 = New_Ans2.getText().toString();
                ans3 = New_Ans3. getText().toString();
                ans4 = New_Ans4.getText().toString();

                helper.updateData(id,question,ans1,ans2,ans3,ans4,sqLiteDatabase);
                Toast.makeText(getApplicationContext(),"Successfully Updated", Toast.LENGTH_LONG).show();
            }
        });

    }

    public void searchContact(View view) {

        SearchID = Search_Id.getText().toString();
        sqLiteDatabase = helper.getReadableDatabase();
        Cursor cursor = helper.getData(SearchID, sqLiteDatabase);
        if (cursor.moveToFirst()) {
            NewQuestion = cursor.getString(0);
            NewAns1 = cursor.getString(1);
            NewAns2 = cursor.getString(2);
            NewAns3 = cursor.getString(3);
            NewAns4 = cursor.getString(4);
            NewId = SearchID;

            New_Question.setText(NewQuestion);
            New_Ans1.setText(NewAns1);
            New_Ans2.setText(NewAns2);
            New_Ans3.setText(NewAns3);
            New_Ans4.setText(NewAns4);
            New_Ans4.setVisibility(View.VISIBLE);
            New_Ans3.setVisibility(View.VISIBLE);
            New_Ans2.setVisibility(View.VISIBLE);
            New_Ans1.setVisibility(View.VISIBLE);
            New_Question.setVisibility(View.VISIBLE);
            title_text.setVisibility(View.VISIBLE);
            updateButton.setVisibility(View.VISIBLE);

        }

    }

1 个答案:

答案 0 :(得分:1)

Button function to Update pass the SearchID.

 public void updateData(View view)
    {
        sqLiteDatabase = helper.getWritableDatabase();
        String question, ans1, ans2, ans3, ans4;

        question = New_Question.getText().toString();
        ans1 = New_Ans1.getText().toString();
        ans2 = New_Ans2.getText().toString();
        ans3 = New_Ans3.getText().toString();
        ans4 = New_Ans4.getText().toString();

        helper.updateData(SearchID,question,ans1,ans2,ans3,ans4,sqLiteDatabase);
        Toast.makeText(getApplicationContext(),"Successfully Updated", Toast.LENGTH_LONG).show();

    }


    }