【问题标题】:list view did not show any value in android studio列表视图在 android studio 中没有显示任何值
【发布时间】:2015-08-13 09:34:18
【问题描述】:

目前我正在尝试开发使用 PHP 和 Android Studio 的项目。我正在使用列表视图。我已经在谷歌上搜索了其他教程,但没有找到任何答案,使用列表视图仍然有问题。

05-30 23:10:15.592    7820-7849/com.example.ayim.madoc D/All Products:﹕ {"success":1,"patient":[{"id":"1","name":"Ikhwan Johari"},{"id":"2","name":"Ruminah Bedah"},{"id":"3","name":"Kamal Sado"}]}
05-30 23:10:15.592    7820-7849/com.example.ayim.madoc W/System.err﹕ org.json.JSONException: No value for idPatient
05-30 23:10:15.593    7820-7849/com.example.ayim.madoc W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:354)
05-30 23:10:15.593    7820-7849/com.example.ayim.madoc W/System.err﹕ at org.json.JSONObject.getString(JSONObject.java:510)
05-30 23:10:15.593    7820-7849/com.example.ayim.madoc W/System.err﹕ at com.example.ayim.madoc.ListPatient$LoadAllPatient.doInBackground(ListPatient.java:112)
05-30 23:10:15.593    7820-7849/com.example.ayim.madoc W/System.err﹕ at com.example.ayim.madoc.ListPatient$LoadAllPatient.doInBackground(ListPatient.java:69)
05-30 23:10:15.593    7820-7849/com.example.ayim.madoc W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-30 23:10:15.593    7820-7849/com.example.ayim.madoc W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-30 23:10:15.593    7820-7849/com.example.ayim.madoc W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-30 23:10:15.593    7820-7849/com.example.ayim.madoc W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-30 23:10:15.594    7820-7849/com.example.ayim.madoc W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-30 23:10:15.594    7820-7849/com.example.ayim.madoc W/System.err﹕ at java.lang.Thread.run(Thread.java:838)

这是我的堆栈显示的内容。数据库中的值已获取,但未显示在列表视图中。我不知道如何解决这个问题。谁能帮帮我?

我还是这个 Android Studio 的新手。谢谢你。

这是我的 Java 代码。

public class ListPatient extends ListActivity {

    //String intentID = getIntent().getExtras().getString("id");
    // Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> patientList;

    JSONArray patient = null;

    TextView idShow;

    private static final String list_url = "http://104.223.3.210/madoc/all_patient.php";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_patient);



         idShow = (TextView) findViewById(R.id.id);
        idShow.setText(getIntent().getExtras().getString("id"));


        // Hashmap for ListView
        patientList = new ArrayList<HashMap<String, String>>();

        // Loading products in Background Thread
        new LoadAllPatient().execute();

        // Get listview
        ListView lv = getListView();
    }



    class LoadAllPatient extends AsyncTask<String, String, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ListPatient.this);
            pDialog.setMessage("Loading products. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {



            try {

                List<NameValuePair> paras = new ArrayList<NameValuePair>();
                // getting JSON string from URL
                paras.add(new BasicNameValuePair("id", getIntent().getExtras().getString("id")));
                JSONObject json1 = jParser.makeHttpRequest(list_url, "POST", paras);
                Log.d("All Products: ", json1.toString());

                // Checking for SUCCESS TAG
                int success = json1.getInt("success");

                if (success == 1) {

                    patient = json1.getJSONArray("patient");

                    // looping through All Products
                    for (int i = 0; i < patient.length(); i++) {
                        JSONObject c = patient.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString("idPatient");
                        String name = c.getString("name");

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put("id", id);
                        map.put("name", name);

                        // adding HashList to ArrayList
                        patientList.add(map);
                    }
                } else {


                    idShow.setText("No Patient");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


            return null;
        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            ListPatient.this, patientList,
                            R.layout.list_patient, new String[] { "id",
                            "name"},
                            new int[] { R.id.id, R.id.name });
                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }
    }

}

【问题讨论】:

  • 你想要代码将你进入的数据放入列表视图(来自 JSON 的值)
  • @DJphy 对不起,我不明白你在说什么。
  • 我没听懂你,哈利...你不知道如何使用你所说的列表视图??
  • @DJphy 带有数组的列表视图,是的,我愿意。但不是用 php 或 JSON..

标签: android json listview android-listview


【解决方案1】:

这里抛出异常:

String id = c.getString("idPatient");

您的 JSON 中没有 "idPatient" 键。尝试将其替换为 "id"

【讨论】:

  • 就是这样!为什么我想不通。 @fRoStBiT
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
相关资源
最近更新 更多