将MYSQL数据库中的数据显示到android中的textview中

时间:2022-05-20 13:57:34

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

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

and the xml files is.....

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

this is how the screen looks like

这就是屏​​幕的样子

将MYSQL数据库中的数据显示到android中的textview中

1 个解决方案

#1


0  

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!

我想你应该在整个通话过程中尝试使用Log.d(TAG,info)。在某个地方,你得到null或空字符串,因为你正在显示它,看起来你的textview是错误的。尝试更多日志记录并查看它的中断位置。我相信你会找到你的答案 - 如果你不能,用日志更新你的答案,它将帮助其他人找出真正的错误!

#1


0  

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!

我想你应该在整个通话过程中尝试使用Log.d(TAG,info)。在某个地方,你得到null或空字符串,因为你正在显示它,看起来你的textview是错误的。尝试更多日志记录并查看它的中断位置。我相信你会找到你的答案 - 如果你不能,用日志更新你的答案,它将帮助其他人找出真正的错误!