更新EditText和数据库onClick

时间:2017-03-17 17:38:00

标签: java android sqlite android-edittext

我目前有三个按钮,一个用于更新,允许更新标题。删除,允许删除数据库条目。这些目前工作正常。

我添加了一个名为Complete(completeBtn)的第三个按钮。当用户按下Complete按钮时,我希望EditText(titleText)更改为“Marked Complete”,并在数据库中更改标题条目。我认为我非常接近,但不确定问题是什么。

目前没有显示错误,但是当我按下“完成”按钮时,它不会将titleText的EditText更改为“标记为完成”或将存储在数据库中的标题值更改为“标记为完成”

任何帮助将不胜感激。 :)

ModifyDeadlines.java

package com.example.pc.achieve;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ModifyDeadline extends Activity implements OnClickListener {

private EditText titleText;
private Button updateBtn, completeBtn, deleteBtn;
private EditText descText;

private long _id;

private DBManager dbManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTitle("Modify Record");

    setContentView(R.layout.activity_modify_record);

    dbManager = new DBManager(this);
    dbManager.open();

    titleText = (EditText) findViewById(R.id.subject_edittext);
    descText = (EditText) findViewById(R.id.description_edittext);

    updateBtn = (Button) findViewById(R.id.btn_update);
    deleteBtn = (Button) findViewById(R.id.btn_delete);

    completeBtn = (Button) findViewById(R.id.btn_complete);
    titleText.setText( "Marked Complete" + completeBtn.getText() );

    Intent intent = getIntent();
    String id = intent.getStringExtra("id");
    String name = intent.getStringExtra("title");
    String desc = intent.getStringExtra("desc");

    _id = Long.parseLong(id);

    titleText.setText(name);
    descText.setText(desc);

    updateBtn.setOnClickListener(this);
    deleteBtn.setOnClickListener(this);
    completeBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_update:
        case R.id.btn_complete:
            String title = titleText.getText().toString();
            String desc = descText.getText().toString();

            dbManager.update(_id, title, desc);
            this.returnHome();
            break;

        case R.id.btn_delete:
            dbManager.delete(_id);
            this.returnHome();
            break;
    }
}

public void returnHome() {
    Intent home_intent = new Intent(getApplicationContext(), ViewDeadlines.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(home_intent);
}
}

1 个答案:

答案 0 :(得分:1)

我认为您错过了匹配button点击实施

试试这个;

 @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_update:

                String title = titleText.getText().toString();
                String desc = descText.getText().toString();

                dbManager.update(_id, title, desc);
                this.returnHome();
                break;   //missing break statement

            case R.id.btn_complete:
                titleText.setText( "Marked Complete" + completeBtn.getText());
                complete();
                break;

            case R.id.btn_delete:
                dbManager.delete(_id);
                this.returnHome();
                break;
        }
    }

    public void complete(){
        String title = titleText.getText().toString();
        String desc = descText.getText().toString();

        dbManager.update(_id, title, desc);
    }