php - 从 mysql 服务器加载的文本没有出现在 ListView 中

标签 php android mysql

我正在尝试构建一个从 MySQL 数据库接收数据到 android 应用程序的 android 应用程序。 我遵循了本教程: http://www.geeks.gallery/simple-json-parsing-example-in-android-part-iii/

当我单击将数据加载到 ListView 的按钮时出现问题,由于某种原因文本没有出现在该列表中。 当我跟随代码时,我敏锐地看到正确的数据被插入到相应的相关 HashMap “oslist”中。

有什么建议吗? 谢谢, 哈南

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:weightSum="1"
    android:orientation="vertical">


    <ListView
    android:id="@+id/list"
    android:layout_width="wrap_content"
    android:layout_height="335dp"
    android:layout_above="@+id/getdata"
        android:choiceMode="singleChoice" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="load data"
        android:id="@+id/getdata"
        android:layout_gravity="center_horizontal" />


</LinearLayout>

main_activity.java

package com.example.aradline;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
 import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
ListView list;
TextView ver;
TextView name;
TextView api;
ArrayList<HashMap<String, String>> oslist = new ArrayList<>();


private static String url = "http://www.geeks.gallery/android/android_version_json_data.php";
private static final String TAG_OS = "Android Version List";
private static final String TAG_VER = "Version No";
private static final String TAG_NAME = "Version Name";
private static final String TAG_API = "API Level";

JSONArray android = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button getdata = (Button) findViewById(R.id.getdata);
    getdata.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new JSONParse().execute();
        }
    });

    list = (ListView) findViewById(R.id.list);

    ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
            R.layout.list_child,
            new String[] { TAG_VER, TAG_NAME, TAG_API }, new int[] {
            R.id.vers, R.id.name, R.id.api });
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Toast.makeText(MainActivity.this,
                    "You Clicked at " + oslist.get(+position).get("name"),
                    Toast.LENGTH_SHORT).show();
        }
    });
}

private class JSONParse extends AsyncTask<String, String, JSONObject> {
    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        ver = (TextView) findViewById(R.id.vers);
        name = (TextView) findViewById(R.id.name);
        api = (TextView) findViewById(R.id.api);
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... args) {
        JsonParser jParser = new JsonParser();
// Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {

       pDialog.dismiss();
        try {
// Getting JSON Array from URL
            android = json.getJSONArray(TAG_OS);


            for (int i = 0; i < android.length(); i++) {
                JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
                String name = c.getString(TAG_NAME);
                String ver = c.getString(TAG_VER);
                String api = c.getString(TAG_API);


// Adding value HashMap key => value
                HashMap<String, String> map = new HashMap<>();

                map.put(TAG_VER, ver);
                map.put(TAG_NAME, name);
                map.put(TAG_API, api);

                oslist.add(map);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
}

list_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
<TextView
    android:id="@+id/vers"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

<TextView
    android:id="@+id/api"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

最佳答案

在从服务器获取数据之前,您正在创建列表适配器。那时,您的 oslist 是空的,因此您无法在列表中看到任何数据。 从服务器获取数据后,您必须更新列表。 最好的地方在onPostExecute()

onPostExecute() 中添加 -

adapter = new SimpleAdapter(MainActivity.this, oslist,
            R.layout.list_child,
            new String[] { TAG_VER, TAG_NAME, TAG_API }, new int[] {
            R.id.vers, R.id.name, R.id.api });
    adapter.notifyDataSetChanged();

之后

 oslist.add(map);

关于php - 从 mysql 服务器加载的文本没有出现在 ListView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36166849/

相关文章:

php - 如何获取(通过引用)数组的长度

java - Android kotlin google play billing onPurchasesUpdated 不覆盖任何内容或从未使用过

java - 使用 java/tomcat 在 mysql 5.5 中插入表情符号

php - 在php中根据时区计算时间

php - ReflectionException 在 Composer 更新存在的文件时抛出

php - 获取 Woocommerce 订单接收页面中的订单 ID 作为简码

php - 是否可以使用 codeigniter 1.7 和循环进行多文件上传?

android - 从服务到 Activity 的 ResultReceiver

android - 单击 WebView 中的表单字段时禁用缩放?

mysql - PDO 语句错误 #1064