Insert data to a database using dialogbox

二次信任 提交于 2020-01-16 14:19:53

问题


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();
     }
});

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();
        }
      }
    });
  }
}

回答1:


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();
            });
        }
    }
}


来源:https://stackoverflow.com/questions/49200154/insert-data-to-a-database-using-dialogbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!