gpt4 book ai didi

java - Android:单击提交按钮后将 'waiting' 字符串数据发送到数据库

转载 作者:行者123 更新时间:2023-11-30 21:31:21 25 4
gpt4 key购买 nike

根据我的问题,我想提交用户在名为"mysuggestion" 的数据库表中填写的所有数据。表格中的所有列都填充了用户的输入数据,除了 "status""comment" 列,因为“status”和“comment”都没有用户输入.这两栏需要填写“waiting”。

如何实现?下面是我的代码。

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

final ActionBar abar = getSupportActionBar();
View viewActionBar = getLayoutInflater().inflate(R.layout.activity_new_suggestion, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
TextView tvTitle = viewActionBar.findViewById(R.id.title);
tvTitle.setText("CONFIRMATION");
abar.setCustomView(viewActionBar, params);
abar.setDisplayShowCustomEnabled(true);
abar.setDisplayShowTitleEnabled(false);
//abar.setDisplayHomeAsUpEnabled(true);
abar.setHomeButtonEnabled(true);


txtName = findViewById(R.id.txtName);
txtBadgeID = findViewById(R.id.txtBadgeID);
txtPosition = findViewById(R.id.txtPosition);
txtDepartment = findViewById(R.id.txtDepartment);
txtFactory = findViewById(R.id.txtFactory);

//getting the current user
User user = SharedPrefManager.getInstance(this).getUser();

//setting the values to the textviews
txtName.setText(user.getName());
txtBadgeID.setText(user.getBadgeid());
txtPosition.setText(user.getPosition());
txtDepartment.setText(user.getDepartment());
txtFactory.setText(user.getFactory());

etReviewer = findViewById(R.id.etReviewer);
etTitle = findViewById(R.id.etTitle);
etYear = findViewById(R.id.etYear);
etMonth = findViewById(R.id.etMonth);
etSuggestWill = findViewById(R.id.etSuggestWill);
etPresent = findViewById(R.id.etPresent);
etDetails = findViewById(R.id.etDetails);
etBenefit = findViewById(R.id.etBenefit);
imgAttach = findViewById(R.id.imgAttach);

SharedPreferences sharedPreferences = getSharedPreferences("MyData", MODE_PRIVATE);
String reviewer = sharedPreferences.getString("reviewer",DEFAULT);
String title = sharedPreferences.getString("title",DEFAULT);
String year = sharedPreferences.getString("year",DEFAULT);
String month = sharedPreferences.getString("month",DEFAULT);
String suggestionwill = sharedPreferences.getString("suggestionwill",DEFAULT);
String present = sharedPreferences.getString("present",DEFAULT);
String details = sharedPreferences.getString("details",DEFAULT);
String benefit = sharedPreferences.getString("benefit",DEFAULT);
String photo = sharedPreferences.getString("photo",DEFAULT);
String status = sharedPreferences.getString("status",DEFAULT);
String comment = sharedPreferences.getString("comments",DEFAULT);

etReviewer.setText(reviewer);
etTitle.setText(title);
etYear.setText(year);
etMonth.setText(month);
etSuggestWill.setText(suggestionwill);
etPresent.setText(present);
etDetails.setText(details);
etBenefit.setText(benefit);
imgAttach.setImageBitmap(base64ToBitmap(photo));

}

public static Bitmap base64ToBitmap(String encodedString) {
byte[] decodedString = Base64.decode(encodedString, Base64.DEFAULT);
Bitmap bitmap= BitmapFactory.decodeByteArray(decodedString , 0,
decodedString.length);
return bitmap;
}

public void submit(View v ) {

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Submit");
builder.setMessage("Do you want to submit this suggestion?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

save();
Intent intent = new Intent(ConfirmSuggestion.this, Submitted.class);
startActivity(intent);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.show();
}

private void save() {
final String name = txtName.getText().toString().trim();
final String badgeid = txtBadgeID.getText().toString().trim();
final String position = txtPosition.getText().toString().trim();
final String department = txtDepartment.getText().toString().trim();
final String factory = txtFactory.getText().toString().trim();
String reviewer = etReviewer.getText().toString().trim();
reviewer = reviewer.split("-")[0]; // 004
final String title = etTitle.getText().toString().trim();
final String year = etYear.getText().toString().trim();
final String month = etMonth.getText().toString().trim();
final String suggestionwill = etSuggestWill.getText().toString().trim();
final String present = etPresent.getText().toString().trim();
final String details = etDetails.getText().toString().trim();
final String benefit = etBenefit.getText().toString().trim();

BitmapDrawable drawable = (BitmapDrawable) imgAttach.getDrawable();
final String photo = bitmapToBase64(drawable.getBitmap());

save1(name, badgeid, position, department, factory, reviewer, title, year, month,
suggestionwill, present, details, benefit,photo,status, comment);
}

public static String bitmapToBase64(Bitmap image) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, os);
byte[] byteArray = os.toByteArray();
String encodedImageString = Base64.encodeToString(byteArray, Base64.DEFAULT);
return encodedImageString ;
}

private void save1(String name, String badgeid, String position, String department, String factory, String reviewer,
String title, String year, String month, String suggestionwill, String present, String details,
String benefit, String photo, String status, String comment) {

User user = SharedPrefManager.getInstance(this).getUser();
SharedPrefManager.getInstance(this).userLogin(new User(user.getId(),name, badgeid, position, department, factory, reviewer,
title, year, month, suggestionwill, present, details,
benefit, photo, status, comment));

class saveSuggest extends AsyncTask<String, Void, String> {
ProgressDialog loading;
RequestHandler requestHandler = new RequestHandler();

@Override
protected String doInBackground(String... params) {

HashMap<String, String> data = new HashMap<String, String>();
data.put("name", params[0]);
data.put("badgeid", params[1]);
data.put("position", params[2]);
data.put("department", params[3]);
data.put("factory", params[4]);
data.put("reviewer", params[5]);
data.put("title", params[6]);
data.put("year", params[7]);
data.put("month", params[8]);
data.put("suggestionwill", params[9]);
data.put("present", params[10]);
data.put("details", params[11]);
data.put("benefit", params[12]);
data.put("photo", params[13]);
data.put("status", params[14]);
data.put("comment", params[15]);

String result = requestHandler.sendPostRequest(URLs.URL_SAVE, data);

return result;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(ConfirmSuggestion.this, "Saving..", null, true, true);
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}

saveSuggest sl1 = new saveSuggest();
sl1.execute(name, badgeid, position, department, factory, reviewer, title, year, month,
suggestionwill, present, details, benefit, photo, status, comment);
}

最佳答案

当您从 SharedPreferences 获取statuscomment 数据时,将它们的默认值设置为“waiting”,如下所示-

String status = sharedPreferences.getString("status","waiting");
String comment = sharedPreferences.getString("comments","waiting");

我希望它对你有用。谢谢

关于java - Android:单击提交按钮后将 'waiting' 字符串数据发送到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55998594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com