【问题标题】:Empty List Android Studio空列表 Android Studio
【发布时间】:2016-12-12 11:41:10
【问题描述】:

为什么我的手机上的列表是空的。 1 可以帮忙吗?它显示正确的 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 文件。

【问题讨论】:

    标签: php android json listview empty-list


    【解决方案1】:

    查看SimpleAdapter class reference

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

    SimpleAdapter (Context context, 
                   List<? extends Map<String, ?>> data, 
                   int resource, 
                   String[] from, 
                   int[] to)
    
    1. context Context:与此 SimpleAdapter 关联的 View 正在运行的上下文
    2. 数据列表:地图列表。列表中的每个条目对应于列表中的一行地图包含每一行的数据,并应包括“来自”中指定的所有条目
    3. resource int:定义此列表项的视图的视图布局的资源标识符。布局文件应至少包含“to”中定义的命名视图
    4. 来自字符串:将添加到与每个项目关联的地图中的列名列表
    5. to int:应在“from”参数中显示列的视图。这些都应该是 TextViews。此列表中的前 N ​​个视图被赋予 from 参数中前 N 列的值。

    您将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 的每个元素都应该是包含键“列表”的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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多