【问题标题】:Android Insert CheckBox value into mysqlAndroid将CheckBox值插入mysql
【发布时间】:2018-01-23 18:48:26
【问题描述】:

我正在编写一个 android 应用程序,我想将一些字符串数据插入到我的 mysql 数据库中。我的 MainActivity 中有一个 CheckBox。如果选中,我想在我的 mysql-databse 中插入“是”,如果未选中,我想插入“否”。目前它不起作用。我认为我的主要问题是,它说 str_cb_gesdeutsch 从未使用过。为什么我不能在“public void OnAnghinzu”中使用它?

MainActivity.java

public class MainActivity extends AppCompatActivity {
  CheckBox cb_gesdeutsch;

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

        cb_gesdeutsch = (CheckBox) findViewById(R.id.cb_gesdeutsch);
        }


  public void CheckboxUpload() {

        boolean cb_checked = ((CheckBox) cb_gesdeutsch).isChecked();
            if(cb_checked){
                Toast.makeText(AnghinzuActivity.this,"Checkbox is checked",Toast.LENGTH_SHORT).show();
                 String str_cb_gesdeutsch = "Yes";
        } else {
                Toast.makeText(AnghinzuActivity.this,"Checkbox is unchecked",Toast.LENGTH_SHORT).show();
                String str_cb_gesdeutsch = "No";
        }
    }


  public void OnAnghinzu(View view) {
    CheckboxUpload();
    String type = "add";
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, str_cb_gesdeutsch);
    
  }
}

BackgroundWorker.java

public class BackgroundWorker extends AsyncTask<String,Void,String> {

    Context context;
    AlertDialog alertDialog;
    BackgroundWorker(Context ctx) {
        context = ctx;
    }
  if(type.equals("add")) {
            try {
                String gesdeutsch = params[1];
                URL url = new URL("*******");
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("Gesamtdeutschland", "UTF-8")+"="+URLEncoder.encode(gesdeutsch, "UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));//iso-8859-1
                String result = "";
                String line = "";
                while((line = bufferedReader.readLine()) != null){
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Status");
    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();
    }
}

【问题讨论】:

    标签: java android mysql checkbox


    【解决方案1】:

    只需复制并粘贴下面的代码即可正常工作

    public class MainActivity extends AppCompatActivity {
    
    CheckBox cb_gesdeutsch;
    String str_cb_gesdeutsch = "";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        cb_gesdeutsch = (CheckBox) findViewById(R.id.cb_gesdeutsch);
    
      }
    
     public void OnAnghinzu(View view) {
        CheckboxUpload(view);
      }
    
    public void CheckboxUpload(View view) {
        boolean cb_checked = ((CheckBox) cb_gesdeutsch).isChecked();
        if (cb_checked) {
            str_cb_gesdeutsch = "Yes";
            Toast.makeText(MainActivity.this, "Checkbox is checked  " + str_cb_gesdeutsch, Toast.LENGTH_SHORT).show();
    
        } else {
            str_cb_gesdeutsch = "No";
            Toast.makeText(MainActivity.this, "Checkbox is unchecked  " + str_cb_gesdeutsch, Toast.LENGTH_SHORT).show();
    
         }
       }
     }
    

    【讨论】:

    • 感谢您的回答。但是还是有同样的问题:变量 'str_cb_gesdeutsch' 从来没有被使用过。
    • 查看我的更新答案。只需在方法之外声明变量。
    • 更新的答案不起作用。方法还是有同样的问题。
    • 尝试清理和重建项目,如果问题仍然存在,则尝试使缓存无效并重新启动
    • 我试过了。它没有帮助。现在,当单击 CheckBox 时,Toast 也不起作用。在插入更改之前,这是可行的。
    猜你喜欢
    • 2012-04-28
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 2017-10-05
    • 1970-01-01
    相关资源
    最近更新 更多