【问题标题】:Android: Send 'waiting' string data to database after clicked submit buttonAndroid:单击提交按钮后将“等待”字符串数据发送到数据库
【发布时间】:2019-05-06 03:54:22
【问题描述】:

根据我的问题,我想将用户填写的所有数据提交到名为"mysuggestion" 的数据库表中。由于“状态”和“评论”都没有用户输入,除"status""comment" 列外,表格中的所有列都填充了用户的输入数据。这两列需要填写“等待”字样。

怎么做?下面是我的代码。

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

【问题讨论】:

    标签: java android mysql


    【解决方案1】:

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

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

    我希望它对你有用。谢谢

    【讨论】:

    • @WanDila 只需将String status = sharedPreferences.getString("status",DEFAULT); String comment = sharedPreferences.getString("comments",DEFAULT); 替换为以上两行
    • save()函数怎么样?
    • save1(姓名、徽章、职位、部门、工厂、审稿人、职称、年月、建议、现在、详情、福利、照片、状态、评论);先生的“状态”和“评论”红色。请帮忙
    • 把你的鼠标移到那里它会告诉你为什么它显示为红色
    猜你喜欢
    • 2012-10-26
    • 1970-01-01
    • 2017-09-27
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 2015-06-27
    相关资源
    最近更新 更多