JSONObject未显示在Listview上

时间:2013-05-12 21:29:23

标签: java android json listview

我正在尝试获取JSONObject并在listview中显示。对象的请求正在返回应该的内容,似乎其余的代码无法正常工作....

这是我的代码:

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.JsonReader;
import android.util.Log;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class DisplayServiceActivity extends ListActivity {
    private ListView listOfServices;

    //JSONArrays?
    JSONArray directory;


    //JSON Node names
    private static String TAG_ID = "id";
    private static String TAG_NAME= "name";
    private static String TAG_DIRECTORY = "Categories";
    private final static String url= "APILINKHERE";
    JSONObject json;
    jsonParser jParser = new jsonParser();
    ArrayList<HashMap<String, String>> directoryList;

    @SuppressLint("NewApi")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Request request = new Request();
        request.execute();

        listOfServices =getListView(); //get builtin listView


        directoryList = new ArrayList<HashMap<String, String>>();

        ListAdapter adapter = new SimpleAdapter(this,
                directoryList, 
                R.layout.list_item, 
                new String[] { TAG_ID,TAG_NAME }, 
                new int[] { android.R.id.text1,android.R.id.text2 });

        setListAdapter(adapter);
        setContentView(R.layout.service);
        // Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }// end of onCreate Method
    @SuppressWarnings("unused")
    public class Request extends AsyncTask<String, Void, JSONObject> {

        private static final int REGISTRATION_TIMEOUT = 3 * 1000;
        private static final int WAIT_TIMEOUT = 30 * 1000;
        private ProgressDialog dialog = 
                new ProgressDialog(DisplayServiceActivity.this);


        protected void onPreExecute() {
            dialog = new ProgressDialog(DisplayServiceActivity.this);
            dialog.setMessage("Getting your info real quick... Please wait...");
            dialog.show();
        }

        protected JSONObject doInBackground(String... params) {

            json = jParser.getJSONfromURL(url);
            Log.e("JSON Variable", "json returns this value: " + json);

            return json;

        }

        protected void onPostExecute(JSONObject s) {          
            super.onPostExecute(s);

            dialog.dismiss();
            Log.e("Test JSON","JSON s returns: " + s);
            try {
                directory = s.getJSONArray("Categories");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            for(int i = 0; i< directory.length(); i++){
                String str_id = directory.optString(i, "id");
                String str_name = directory.optString(i, "name");
                displayCatList(str_id, str_name);

                Log.e("Test directory","Directory returns: " + json);
            }

        }

    }

    public void displayCatList(String id, String name){                 

        //create new HashMap
        HashMap<String,String> map = new HashMap<String, String>();

        //add each child node to HashMap key
        map.put(TAG_ID, id);
        map.put(TAG_NAME, name);

        //adding HashList to ArrarList
        directoryList.add(map);


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

logcat的:

05-12 17:26:33.165: I/Adreno200-EGLSUB(10190): <ConfigWindowMatch:2087>: Format RGBA_8888.
05-12 17:26:34.497: E/success(10190): created object: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.497: E/JSON Variable(10190): json returns this value: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.537: E/Test JSON(10190): JSON s returns: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.537: E/Test directory(10190): Directory returns: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.537: E/Test directory(10190): Directory returns: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.537: E/Test directory(10190): Directory returns: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.547: E/Test directory(10190): Directory returns: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.547: E/Test directory(10190): Directory returns: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.547: E/Test directory(10190): Directory returns: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.547: E/Test directory(10190): Directory returns: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.547: E/Test directory(10190): Directory returns: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.547: E/Test directory(10190): Directory returns: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.557: E/Test directory(10190): Directory returns: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}
05-12 17:26:34.557: E/Test directory(10190): Directory returns: {"Categories":[{"id":"2","name":"Glass Repair"},{"id":"3","name":"Appliance Repair"},{"id":"4","name":"Air Conditioning"},{"id":"5","name":"Community Involvement"},{"id":"6","name":"Electrical"},{"id":"7","name":"Flooring"},{"id":"8","name":"Heating Repair"},{"id":"9","name":"Landscaping"},{"id":"10","name":"Plumbing"},{"id":"11","name":"Remodeling\/Renovation"},{"id":"12","name":"Window Coverings"}]}

正如您所看到的那样,JSON信息正在被拉动,但它并没有从那里做很多事情。在应用程序本身,屏幕只保留空白,没有列表视图或正在填充。

3 个答案:

答案 0 :(得分:1)

  

JSONObject未在Listview上显示

因为在向项目添加项目之前将directoryList传递给适配器。因此,您需要在displayCatList方法而不是onCreate方法中创建适配器实例:

 public void displayCatList(String id, String name){                 

        //....your code here

        //adding HashList to ArrarList
        directoryList.add(map);

         // set Adapter for ListView here

          ListAdapter adapter = new SimpleAdapter(DisplayServiceActivity.this,
                directoryList, 
                R.layout.list_item, 
                new String[] { TAG_ID,TAG_NAME }, 
                new int[] { android.R.id.text1,android.R.id.text2 });

        DisplayServiceActivity.this.setListAdapter(adapter);
    }

答案 1 :(得分:1)

AsyncTask在BackGround中执行,因此在填充时间directlyList之前,您将其设置为listAdapter,在setListAdapter

的填充后需要directoryList

你必须在setListAdapteronPostExecute这样 - &gt;

protected void onPostExecute(JSONObject s) {          
        super.onPostExecute(s);

        dialog.dismiss();
        Log.e("Test JSON","JSON s returns: " + s);
        try {
            directory = s.getJSONArray("Categories");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for(int i = 0; i< directory.length(); i++){
            String str_id = directory.optString(i, "id");
            String str_name = directory.optString(i, "name");
            displayCatList(str_id, str_name);

            Log.e("Test directory","Directory returns: " + json);
        }
        listOfServices = DisplayServiceActivity.this.getListView(); //get builtin listView
        ListAdapter adapter = new SimpleAdapter(DisplayServiceActivity.this,
            directoryList, 
            R.layout.list_item, 
            new String[] { TAG_ID,TAG_NAME }, 
            new int[] { android.R.id.text1,android.R.id.text2 });

        setListAdapter(adapter);
    }

答案 2 :(得分:0)

我认为您在检索JSON数据之前设置了ListAdapter。尝试在onPostExecute方法中设置listadapter,看看是否有效。或者在notifyDataSetChanged()中调用列表适配器的onPostExecute方法。

我个人总是通过在内部类中继承BaseAdapter来创建自己的列表适配器实现