0

我正在尝试获取 JSONObject 并在列表视图中显示。对对象的请求正在返回它应该返回的内容,似乎其余代码不起作用....

这是我的代码:

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);
    }
}

日志猫:

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 信息,但从那里并没有做太多事情。在应用程序本身上,屏幕只是保持空白,没有列表视图或它被填充。

4

3 回答 3

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);
    }
于 2013-05-12T21:35:22.703 回答
1

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

你必须setListAdapteronPostExecute这样 ->

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);
    }
于 2013-05-12T21:36:45.157 回答
0

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

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

于 2013-05-12T21:35:00.387 回答