如何从选中的单选按钮更新数据库?

时间:2016-04-02 04:08:46

标签: android

layout-land

我已将已检查的单选按钮值检索到变量temp。并且它出现在package com.example.admin.userregistrationsample; import android.app.ProgressDialog; import android.graphics.Color; import android.os.AsyncTask; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Some extends AppCompatActivity implements View.OnClickListener{ private EditText branch; public TextView list,list2; Button bt, bt1; String temp1; String text1,temp, usn,text; String res; String[] parts; int count; RadioGroup rg; RadioButton[] rb; GetJSON gj = new GetJSON(); LinearLayout mLinearLayout; private static final String JSON_URL = "http://192.168.43.144/mini1/subject.php"; private static final String SUB_URL = "http://192.168.43.144/mini1/store.php"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_some); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); usn = getIntent().getStringExtra("USN"); bt = (Button) findViewById(R.id.buttonGet); bt.setOnClickListener(this); bt1 = (Button) findViewById(R.id.button2); bt1.setOnClickListener(this); branch = (EditText) findViewById(R.id.editTextId); //invokeLogin(); } //@Override public void onClick(View v) { if (v == bt) { invokeLogin(); } if (v == bt1) { register(temp,usn); } } public void invokeLogin() { text = branch.getText().toString(); getJSON(text); } public void getJSON(String text) { final String urlSuffix = "?branch=" + text; gj.execute(urlSuffix); } public class GetJSON extends AsyncTask<String, Void, String> { ProgressDialog loading; public GetJSON() { } @Override public void onPreExecute() { super.onPreExecute(); loading = ProgressDialog.show(Some.this, "Please Wait...", null, true, true); } @Override public String doInBackground(String... params) { String uri = params[0]; BufferedReader bufferedReader = null; try { URL url = new URL(JSON_URL + uri); HttpURLConnection con = (HttpURLConnection) url.openConnection(); StringBuilder sb = new StringBuilder(); bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); String json; while ((json = bufferedReader.readLine()) != null) { sb.append(json).append("\n"); } return sb.toString().trim(); } catch (Exception e) { return null; } } @Override public void onPostExecute(String s) { super.onPostExecute(s); loading.dismiss(); res=s.replaceAll("\"", ""); res = res.replaceAll(",","\n ").replace('[', ' ').replace(']', ' '); parts = res.split("\n"); count=parts.length; mLinearLayout = (LinearLayout) findViewById(R.id.linear2); rb = new RadioButton[count]; rg = new RadioGroup(getBaseContext()); rg.setOrientation(RadioGroup.VERTICAL); for (int i = 0; i < count; i++) { rb[i] = new RadioButton(getBaseContext()); rb[i].setTextColor(Color.parseColor("#000000")); rb[i].setTextSize(25); rg.addView(rb[i]); rb[i].setText(parts[i]); } mLinearLayout.addView(rg); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { for (int i = 0; i < group.getChildCount(); i++) { RadioButton btn = (RadioButton) group.getChildAt(i); if (btn.getId() == checkedId) { temp1 = (String) btn.getText(); list2 = (TextView) findViewById(R.id.select); list2.setText(temp); break; } } } }); } } public void register(String sub ,String usn) { String urlSuffix = "?subject=" + sub + "&usn=" +usn; class RegisterUser extends AsyncTask<String, Void, String> { ProgressDialog loading; @Override public void onPreExecute() { super.onPreExecute(); loading = ProgressDialog.show(Some.this, "Please Wait", null, true, true); } @Override public void onPostExecute(String s) { super.onPostExecute(s); loading.dismiss(); if (s.equals("successfully registered")) { Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show(); } } @Override public String doInBackground(String... params) { String s = params[0]; BufferedReader bufferedReader = null; try { URL url = new URL(SUB_URL + s); HttpURLConnection con = (HttpURLConnection) url.openConnection(); bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); String result; result = bufferedReader.readLine(); return result; } catch (Exception e) { return null; } } } RegisterUser ru=new RegisterUser(); ru.execute(urlSuffix); } } 中但是当我将该值传递给寄存器功能时,它没有取值如何做到这一点?请有人帮忙..!

1 个答案:

答案 0 :(得分:0)

使用以下代码在异步类之外使用 setOnCheckedChangeListener

protected void onPostExecute(String result) {
    if(!result.equals("null"){
        ((your activity name) activity).parseJsonResponse(result);
        pDialog.dismiss();
    }
}

并在您的活动中添加以下方法。

public void parseJsonResponse(String result) {
    //use setOnCheckedChangeListener here.
}