【问题标题】:JSON to listView in androidJSON到android中的listView
【发布时间】:2014-03-03 09:12:13
【问题描述】:

我正在尝试将 JSON 放入 ListView。我正在从 http://api.androidhive.info/contacts/ 获取数据(仅使用名称字段)我能够将它们放入 array,但我无法将它们放入 list

[注意] 但是ListView 为 13 个条目(名称的数量)精确地制作了 13 行,但这些行是空白的

private class GetJidla extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(TableMenuActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                jidla = jsonObj.getJSONArray(TAG_CONTACTS);

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

                  //  String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);


                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                  //  contact.put(TAG_ID, id);
                    contact.put(TAG_NAME, name);


                    // adding contact to contact list
                    jidlaList.add(contact);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }



        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                TableMenuActivity.this, jidlaList,
                android.R.layout.simple_list_item_1, new String[] {TAG_NAME}, new int[] {android.R.id.list,
                         });

        menu = (ListView)findViewById(android.R.id.list);
        menu.setAdapter(adapter); 

    } 

列表在这里

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip" >



    </ListView>

i need it to be on one screen, in one activity here is the image, the brown lsit is where i need it

【问题讨论】:

  • 你是否设置了一个日志来检查数据是否到来?如果数据即将到来,那么您可以将列表视图的背景更改为黑色,因为有时文本视图是白色的并且文本似乎不可见
  • 是的,我什至可以将数据放入数组中,logcat: 02-06 07:37:05.895: D/jidla:(1487): > [{name=Ravi Tamada}, {name =约翰尼·德普},{name=莱昂纳多·迪卡普里奥},{name=约翰·韦恩},{name=安吉丽娜·朱莉},{name=Dido},{name=阿黛尔},{name=休·杰克曼},{name=威尔·史密斯}, {name=Clint Eastwood}, {name=Barack Obama}, {name=Kate Winslet}, {name=Eminem}] 这是数组的日志,改变颜色没有帮助
  • 那您可以尝试创建自己的自定义适配器吗?

标签: android json list view


【解决方案1】:

这是一个非常简单的json列表视图示例

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

下载项目并在 MainActivity.java 中使用您自己的服务

用你的 url 替换服务 URl 并相应地格式化你的数组

快乐的编码。

【讨论】:

    【解决方案2】:

    我有一个例子:

    public class MainActivity extends Activity {
         //json string
         private String jsonString = "{\"employee\":[{\"emp_name\":\"employee1\",\"emp_no\":\"101700\"},{\"emp_name\":\"employee2\",\"emp_no\":\"101701\"},{\"emp_name\":\"employee3\",\"emp_no\":\"101702\"},"+
                "{\"emp_name\":\"employee4\",\"emp_no\":\"101703\"},{\"emp_name\":\"employee5\",\"emp_no\":\"101704\"},{\"emp_name\":\"employee6\",\"emp_no\":\"101705\"},"+
                "{\"emp_name\":\"employee7\",\"emp_no\":\"101706\"},{\"emp_name\":\"employee8\",\"emp_no\":\"101707\"},{\"emp_name\":\"employee9\",\"emp_no\":\"101708\"},"+
                "{\"emp_name\":\"employee10\",\"emp_no\":\"101709\"},{\"emp_name\":\"employee11\",\"emp_no\":\"101710\"},{\"emp_name\":\"employee12\",\"emp_no\":\"101711\"},"+
                "{\"emp_name\":\"employee13\",\"emp_no\":\"101712\"},{\"emp_name\":\"employee14\",\"emp_no\":\"101713\"},{\"emp_name\":\"employee15\",\"emp_no\":\"101712\"}]}";
         @Override
         protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          initList();
          ListView listView = (ListView) findViewById(R.id.listView1);
          SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList, android.R.layout.simple_list_item_1, new String[] {"employees"}, new int[] {android.R.id.text1});
          listView.setAdapter(simpleAdapter);
         }
    
         @Override
         public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
         }
    
         List<Map<String,String>> employeeList = new ArrayList<Map<String,String>>();
         private void initList(){
    
          try{
           JSONObject jsonResponse = new JSONObject(jsonString);
           JSONArray jsonMainNode = jsonResponse.optJSONArray("employee");
    
          for(int i = 0; i<jsonMainNode.length();i++){
           JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
           String name = jsonChildNode.optString("emp_name");
           String number = jsonChildNode.optString("emp_no");
           String outPut = name + "-" +number;
           employeeList.add(createEmployee("employees", outPut));
          }
         }
          catch(JSONException e){
           Toast.makeText(getApplicationContext(), "Error"+e.toString(), Toast.LENGTH_SHORT).show();
          }
         }
    
         private HashMap<String, String>createEmployee(String name,String number){
          HashMap<String, String> employeeNameNo = new HashMap<String, String>();
          employeeNameNo.put(name, number);
          return employeeNameNo;
         }
    
        }
    

    我使用这个代码,它工作正常!

    来自JSON Exemple的代码

    【讨论】:

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