简体   繁体   中英

displaying data from MYSQL database into textview in android

I'm very new in android and I'm developing an application that displays data from mysql database into text view..i'm nt getting any error but data from the db is not being displayed in the textview...the code is posted below...please help me out..!!

    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();
        }
    }
}

and the xml files is.....

 <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>

this is how the screen looks like

在此处输入图片说明

I think you should try and Log.d(TAG, info) on your way throughout your calls. Somewhere, you are getting null or empty string and since you are displaying it, it seems like your textview is wrong. Try more logging and see where it breaks. I'm sure you will find your answer then -- if you cant, update your answer with the logs, it will help others find out what is really wrong!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM