我建了一個app,在應(yīng)用程序中,當(dāng)點擊按鈕時就發(fā)送一個字符串給下一個activity,在第二個activity中我用這個值從數(shù)據(jù)庫中檢索數(shù)據(jù)。
下邊是代碼
Intent myIntent = getIntent(); // 從前一個activity中獲得值myIntent.getStringExtra("lbl_name");postParameters = new ArrayList();postParameters.add(new BasicNameValuePair("product", lbl_name//傳遞name給database .getText().toString()));
問題是它不會繼承l(wèi)bl_name
我在網(wǎng)上找了很久,但是都沒有找到解決方法。麻煩告訴我一下這么做對不對?
傳遞值的代碼.
Intent myIntent = new Intent(SinglePlaceActivity.this,RecoProd.class); myIntent.putExtra("lbl_name", "lbl_name"); SinglePlaceActivity.this.startActivity(myIntent);
訪問數(shù)據(jù)庫的代碼.
public class RecoProd extends Activity { EditText pd; TextView error; ArrayList postParameters; /** 當(dāng)activity第一次被創(chuàng)建的時候調(diào)用 */ @Override public void onCreate(Bundle savedInstanceState) { try { Class> strictModeClass = Class.forName("android.os.StrictMode", true, Thread.currentThread() .getContextClassLoader()); Class> threadPolicyClass = Class.forName("android.os.StrictMode$ThreadPolicy", true, Thread .currentThread().getContextClassLoader()); Class> threadPolicyBuilderClass = Class.forName("android.os.StrictMode$ThreadPolicy$Builder", true, Thread.currentThread().getContextClassLoader()); Method setThreadPolicyMethod = strictModeClass.getMethod("setThreadPolicy", threadPolicyClass); Method detectAllMethod = threadPolicyBuilderClass.getMethod("detectAll"); Method penaltyMethod = threadPolicyBuilderClass.getMethod("penaltyLog"); Method buildMethod = threadPolicyBuilderClass.getMethod("build"); Constructor> threadPolicyBuilderConstructor = threadPolicyBuilderClass.getConstructor(); Object threadPolicyBuilderObject = threadPolicyBuilderConstructor.newInstance(); Object obj = detectAllMethod.invoke(threadPolicyBuilderObject); obj = penaltyMethod.invoke(obj); Object threadPolicyObject = buildMethod.invoke(obj); setThreadPolicyMethod.invoke(strictModeClass, threadPolicyObject); } catch (Exception ex) { String TAG = null; Log.w(TAG, ex); } super.onCreate(savedInstanceState); setContentView(R.layout.recomain); Intent myIntent = getIntent(); // 從前一個activity中獲得值 String lbl_name= myIntent.getStringExtra("lbl_name");//< 從intent中獲得 pd = (EditText) findViewById(R.id.name); error = (TextView) findViewById(R.id.showresult); postParameters = new ArrayList(); postParameters.add(new BasicNameValuePair("product", lbl_name )); // String valid = "1"; DownloadWebPageTask dw = new DownloadWebPageTask(); dw.execute(""); } private class DownloadWebPageTask extends AsyncTask { @Override protected String doInBackground(String... urls) { String response = null; for (String url : urls) { try { response = CustomHttpClient.executeHttpPost("http://192.168.1.7/abc/check2.php", postParameters); String res = response.toString(); // res = res.trim(); res = res.replaceAll("\\s+", ""); // error.setText(res); try{ res = ""; JSONArray jArray = new JSONArray(res); for(int i=0;i
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com