我是 android 的新手,我正在开发一个将 mysql 数据库中的数据显示到文本视图中的应用程序。我没有收到任何错误,但来自 db 的数据没有显示在 textview 中……代码是贴在下面...请帮帮我..!!
package com.example.evoting;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ContestantsInfoActivity extends Activity{
TextView txtName;
TextView txtBranch;
//TextView txtDesc;
//TextView txtCreatedAt;
// Button btnSave;
// Button btnDelete;
String rollno;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
private static final String url_contestant_detials = "http://10.0.2.2/evoting/get_contestant_details.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_contestant = "product";
private static final String TAG_rollno = "rollno";
private static final String TAG_NAME = "name";
private static final String TAG_branch = "branch";
// private static final String TAG_DESCRIPTION = "description";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contestants_info_activity);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
rollno = i.getStringExtra(TAG_rollno);
// Getting complete product details in background thread
new GetContestantsDetails().execute();
}
/**
* Background Async Task to Get complete product details
* */
class GetContestantsDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ContestantsInfoActivity.this);
pDialog.setMessage("Loading contestants details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("rollno", rollno));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_contestant_detials, "GET", params);
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
//Intent i = new Intent(getApplicationContext(),
// CreateVoterSuccess.class);
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_contestant); // JSON Array
// get first product object from JSON Array
JSONObject contestant = productObj.getJSONObject(0);
// product with this pid found
// Edit Text
txtName = (TextView) findViewById(R.id.outputName);
txtBranch = (TextView) findViewById(R.id.outputBranch);
// display product data in EditText
txtName.setText(contestant.getString(TAG_NAME));
txtBranch.setText(contestant.getString(TAG_branch));
}else{
Intent i = new Intent(getApplicationContext(),
CreateVoterFail.class);
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
}
xml文件是.....
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Contestants Details"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="Name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/outputName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_toLeftOf="@+id/textView1"
android:editable = "true"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/outputName"
android:layout_marginTop="18dp"
android:text="Branch:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/outputBranch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_toLeftOf="@+id/textView1"
android:editable = "true" />
</RelativeLayout>
这就是屏幕的样子