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 が表示されない

アイテムを追加する前に Adapter に渡しているため、次 のように onCreate メソッドの代わりにメソッドdirectoryList内に Adapter インスタンスを作成する必要があります。displayCatList

 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 に答える
0

JSON データが取得される前に ListAdapter を設定していると思います。メソッドでlistadapter を設定してみて、onPostExecuteそれが機能するかどうかを確認してください。notifyDataSetChanged()または、 でリスト アダプタのメソッドを呼び出しますonPostExecute

BaseAdapter私は個人的に常に、内部クラスでサブクラス化することにより、リストアダプターの独自の実装を作成します

于 2013-05-12T21:35:00.387 に答える