使用对话框将数据插入数据库 [英] Insert data to a database using dialogbox

查看:103
本文介绍了使用对话框将数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向da数据库添加一些数据.通过将意图(在下面的文本中注释)用于活动(稍后显示),可以很好地工作. 现在,我正在尝试使用dialog做同样的事情,但是我对如何做到这一点感到困惑.

I am trying to add some data to da database. This is working fine, using an intent (commented in below text) to an activity(shown later). Now, I am trying to do the same using a dialog, but I am confused about how to do that.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_layout);
  ........
  fab1.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {

    /* THIS IS WORKING WITH ADDITEM ACTIVITY
    Intent i = new Intent(PlacesActivity.this, AddItemActivity.class);
    startActivity(i);
    finish();*/

    // HERE I AM TRYING TO DO SAME THING VIA A DIALOG
    AlertDialog.Builder placeLLDialog = new AlertDialog.Builder(PlacesActivity.this);
    /* This lines are giving Can not resolve error:
    LayoutInflater inflater = this.getLayoutInflater();
    View DialogView =inflater.inflate(R.layout.place_add_dialog);*/

    placeLLDialog.setMessage("This should do the same thing as Intent Above")
        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
            setContentView(R.layout.activity_add_item);
            EditText todo = (EditText) findViewById(R.id.placeN);
            EditText time = (EditText) findViewById(R.id.placell);
            EditText longi = findViewById(R.id.placell2);
            //button = (TextView) findViewById(R.id.button);

            final PlaceDatabase db = Room.databaseBuilder(getApplicationContext(), PlaceDatabase.class,"production")
                .build();
            if(!todo.getText().toString().equals("") &&
                !time.getText().toString().equals("") &&
                !longi.getText().toString().equals("")){

            final PlaceSaved placeSaved = new PlaceSaved(todo.getText().toString(),
            time.getText().toString(),longi.getText().toString());
            //save the item before leaving the activity
          AsyncTask.execute(new Runnable() {
          @Override
          public void run() {
              db.databaseInterface().insertAll(placeSaved);
          }
      });
          }
        }})
        .setNegativeButton("Cancel",null)
        .create();

    placeLLDialog.setTitle("Add Place");
    placeLLDialog.show();
     }
});

AddItemActivity是:

The AddItemActivity is:

public class AddItemActivity extends AppCompatActivity {
  EditText todo;
  EditText time;
  EditText longi;
  TextView button;
  PlaceDatabase db;

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

    db = Room.databaseBuilder(getApplicationContext(), PlaceDatabase.class,"production")
            .build();

    todo = (EditText) findViewById(R.id.placeN);
    time = (EditText) findViewById(R.id.placell);
    longi = findViewById(R.id.placell2);
    button = (TextView) findViewById(R.id.button);

    final PlaceDatabase db = Room.databaseBuilder(getApplicationContext(), PlaceDatabase.class,"production")
            .build();

    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(!todo.getText().toString().equals("") &&
            !time.getText().toString().equals("") &&
            !longi.getText().toString().equals("")){

          final PlaceSaved placeSaved = new PlaceSaved(todo.getText().toString(),
              time.getText().toString(),longi.getText().toString());
          //save the item before leaving the activity
          AsyncTask.execute(new Runnable() {
              @Override
              public void run() {
                  db.databaseInterface().insertAll(placeSaved);
              }
          });
          Intent i = new Intent(AddItemActivity.this,PlacesActivity.class);
          startActivity(i);

          finish();
        }
      }
    });
  }
}

推荐答案

我还没有测试我的代码.可能是错误的.但这是您应该遵循的通常模式.

I haven't tested my code. It might be wrong. But this is the usual pattern you should follow.

public class MainActivity extends ActionBarActivity {
    private Context context = this;
    private TextView textFromDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textFromDialog = (TextView) findViewById(R.id.text_from_dialog);
        Button openDialogButton = (Button) findViewById(R.id.dialog_button);
        openDialogButton.setOnClickListener(new OnClickListener() {

            // This button will open the dialog
            @Override
            public void onClick(View view) {
                LayoutInflater inflater = LayoutInflater.from(context);
                View dialogLayout = inflater.inflate(R.layout.customalertdialog_layout, null);
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setView(dialogLayout);
                TextView indtruction = (TextView) dialogLayout.findViewById(R.id.text_instruction);
                final EditText userInput = (EditText) dialogLayout.findViewById(R.id.user_input);

                builder.setPositiveButton("Update", new
                                DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // Get data from Dialog UI, Then update to Database
                                        dialog.dismiss();
                                    }
                                }
                        AlertDialog customAlertDialog = builder.create();
                customAlertDialog.show();
            });
        }
    }
}

这篇关于使用对话框将数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆