php - 空列表 Android Studio

标签 php android json listview empty-list

为什么我的手机上显示空列表。有人能帮忙吗?它显示了正确的 7 个位置,但它们是空的。 有我的 php 文件,它可能没问题。我使用 json 编码。

public class ClientActivity extends UserAreaActivity {

private String jsonResult;
private String url = "http://vinusek.000webhostapp.com/Client2.php";
private ListView listView;
private TextView nazwa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client);
    listView = (ListView) findViewById(R.id.listView1);
    nazwa = (TextView) findViewById(R.id.textView2) ;
    accessWebService();
}

// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(params[0]);
        try {
            HttpResponse response = httpclient.execute(httppost);
            jsonResult = inputStreamToString(
                    response.getEntity().getContent()).toString();
        }

        catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        try {
            while ((rLine = rd.readLine()) != null) {
                answer.append(rLine);
            }
        }

        catch (IOException e) {
            // e.printStackTrace();
            Toast.makeText(getApplicationContext(),
                    "Error..." + e.toString(), Toast.LENGTH_LONG).show();
        }
        return answer;
    }

    @Override
    protected void onPostExecute(String result) {
        ListDrwaer();
    }
}// end async task

public void accessWebService() {
    JsonReadTask task = new JsonReadTask();
    // passes values for the urls string array
    task.execute(new String[] { url });
}

// build hash set for list view
public void ListDrwaer() {
    List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();

    try {

        JSONObject jsonResponse = new JSONObject(jsonResult);
        JSONArray jsonMainNode = jsonResponse.optJSONArray("result");

        for (int i = 0; i < jsonMainNode.length(); i++) {
            JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
            String number = jsonChildNode.getString("id");
            String name = jsonChildNode.getString("login");
            String pass = jsonChildNode.getString("haslo");
            String outPut = number + "-" +  name + "-" + pass;
            employeeList.add(createEmployee("list", outPut));
            nazwa.setText(outPut);
        }


    } catch (JSONException e) {
        Toast.makeText(getApplicationContext(), "Error" + e.toString(),
                Toast.LENGTH_SHORT).show();
    }


    SimpleAdapter simpleAdapter = new SimpleAdapter(ClientActivity.this, employeeList,
            android.R.layout.simple_list_item_1,
            new String[] {"list"}, new int[] { android.R.id.text1 });
    listView.setAdapter(simpleAdapter);
}

private HashMap<String, String> createEmployee(String name, String number) {
    HashMap<String, String> employeeNameNo = new HashMap<String, String>();
    employeeNameNo.put(number, name);
    return employeeNameNo;
}}

这里有什么问题,我从这里得到代码http://codeoncloud.blogspot.in/2013/07/android-mysql-php-json-tutorial.html 它适用于他的 php 文件。

最佳答案

检查 SimpleAdapter class reference :

如您所见,构造函数采用以下参数:

SimpleAdapter (Context context, 
               List<? extends Map<String, ?>> data, 
               int resource, 
               String[] from, 
               int[] to)
  1. context Context: The context where the View associated with this SimpleAdapter is running
  2. data List: A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
  3. resource int: Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
  4. from String: A list of column names that will be added to the Map associated with each item.
  5. to int: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

您实例化 SimpleAdapter :

SimpleAdapter simpleAdapter = new SimpleAdapter(ClientActivity.this, employeeList,
            android.R.layout.simple_list_item_1,
            new String[] {"list"}, new int[] { android.R.id.text1 });

这意味着您的 employeeList List 的每个元素都应该是一个包含键“list”的 Map

因此

employeeList.add(createEmployee("list", outPut));

应该向 List 添加一个 Map,其中包含一个包含键“list”和值 outPut 的条目。

但是,您的 createEmployee() 方法会创建一个 HashMap,其中包含交换键和值的单个条目。

你应该改变

private HashMap<String, String> createEmployee(String name, String number) {
    HashMap<String, String> employeeNameNo = new HashMap<String, String>();
    employeeNameNo.put(number, name);
    return employeeNameNo;
}

private HashMap<String, String> createEmployee(String name, String number) {
    HashMap<String, String> employeeNameNo = new HashMap<String, String>();
    employeeNameNo.put(name, number);
    return employeeNameNo;
}

关于php - 空列表 Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41100056/

相关文章:

php - 如何让PHP运行得更快?

android - 使用引导类加载器找不到类;没有可用的堆栈跟踪

javascript - 图片的高度与手机/平板电脑的高度相同 - 只能在 Chrome 中正常工作

json - 结合 Jackson @JsonView 和 @JsonProperty

php - Google Adwords API 报告实用程序 - PHP - XML 无效?

php - 使用我的数据库创建一个简单的数组

php - 创建 PHP future 日期的更好方法

android - 禁用屏幕上的按钮 - Android Studio

java - 为什么原始数组不允许添加到 GSON 中的 JSON 结构

json - 如何将 JSON 解码为 pandas 数据帧?