2012-10-09 133 views
0

我是非常新的android和我正在開發一個應用程序,顯示數據從MySQL數據庫到文本視圖..我沒有得到任何錯誤,但數據從數據庫是沒有在textview中顯示...該代碼張貼在下面...請幫我出來..!在Android中顯示數據從MYSQL數據庫到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> 

這是屏幕看起來的樣子

enter image description here

+0

不要在異類中運行OnUthThread,這是行不通的,它有點矛盾 – njzk2

回答

0

我想你應該嘗試和Log.d(標籤,信息)在你的通話過程中。某處,你得到null或空字符串,並且因爲你正在顯示它,所以看起來你的textview是錯誤的。嘗試更多的日誌記錄並查看它的中斷點。我相信你會找到你的答案,然後 - 如果你不能,更新你的答案與日誌,這將幫助其他人找出真正的錯誤!

相關問題