从mysql服务器加载的文本不会出现在列表视图中

时间:2016-03-22 23:02:19

标签: php android mysql

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

出现问题,因为我点击按钮将数据加载到列表视图中,由于某种原因文本不会出现在该列表中。 当我按照代码时,我敏锐地看到正确的数据被插入到相应的相关哈希映射“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>

2 个答案:

答案 0 :(得分:1)

在从服务器获取数据之前,您正在创建列表适配器。那时,您的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);

之后

{{1}}

答案 1 :(得分:0)

尝试完成所有这些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);

onPostExecute之后的oslist.add(map)

我认为这是因为你设置适配器你的oslist是空的。并且在添加所有数据后,适配器不知道他需要刷新。 如果在适配器中有.notifychange()方法,请尝试。我不确定你是否有这种适配器