如何在editTextView中按ID显示SQL数据?

时间:2019-07-29 01:54:30

标签: java android android-edittext

您好,我想问一下这段代码,问题是我只想在getData中显示EditTextview。但这只是在敬酒中显示您认为我的代码将getData中的数据调用为EditText所需要的这些修订是什么,非常感谢您的帮助。

package com.example.serviceapplication;


    import android.database.Cursor;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;


    import androidx.appcompat.app.AlertDialog;
    import androidx.appcompat.app.AppCompatActivity;

    public class Timeinsms extends AppCompatActivity {

    DatabaseHelper myDb;
    EditText editTextId,editTextsmsi;
    Button btngetData,btnView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timeinsms);
        myDb = new DatabaseHelper(this);
        editTextId = (EditText) findViewById(R.id.editText_idin);
        btngetData = (Button) findViewById(R.id.button_view);
        btnView = (Button) findViewById(R.id.button_viewALL);
        getData();
        viewAll();

    }



    public void getData() {
        btngetData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = editTextId.getText().toString();

                if (id.equals(String.valueOf(""))) {
                    editTextId.setError("Enter id to get data");
                    return;
                }
                Cursor res = myDb.getData(id);
                String data = null;
                if (res.moveToFirst()) {

                    data =

                                    "Id:" + res.getString(0) + "\n\n" +
                                    "Time In :" + res.getString(1) + "\n\n" +
                                    "Customer :" + res.getString(2) + "\n\n"+
                                    "Branch :" + res.getString(3) + "\n\n"+
                                    "Machine :" + res.getString(4) + "\n\n";

                }
                showMessage("TIME OUT FORM"+"\n\n", data);
            }
        });
    }


    public void viewAll(){
        btnView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor res=myDb.getAllData();
                if(res.getCount() == 0) {
                    showMessage("Error","Nothing found");
                    return;
                }
                StringBuffer buffer=new StringBuffer();
                while(res.moveToNext()){
                    buffer.append("Id:"+res.getString(0)+"\n\n");
                    buffer.append("Time :"+ res.getString(1)+"\n\n");
                    buffer.append("Customer :"+ res.getString(2)+"\n\n");
                    buffer.append("Branch :"+ res.getString(3)+"\n\n");
                    buffer.append("Machine :"+ res.getString(4)+"\n\n\n");
                }
                showMessage("Time In History",buffer.toString());
            }
        });
    }




    private void showMessage(String title, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.create();
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();

    }

}

1 个答案:

答案 0 :(得分:0)

您没有在Toast中显示它,而是在使用Alertdialog显示消息。 这将是烤面包

Toast.makeText(this,"MESSAGE TO SHOW",Toast.LENGTH_LONG).show();

在此位置

data =                      "Id:" + res.getString(0) + "\n\n" +
                            "Time In :" + res.getString(1) + "\n\n" +
                            "Customer :" + res.getString(2) + "\n\n"+
                            "Branch :" + res.getString(3) + "\n\n"+
                            "Machine :" + res.getString(4) + "\n\n";

您已经在创建一个包含所有数据的字符串,您只需要使用此字符串设置Edittext。

editTextId.setText("TIME OUT FORM"+"\n\n"+data);

必须将其放置在OnClick方法内,因为数据是此方法的局部变量。