0

すでに JSON から文字列値を取得していますが、ハッシュ マップなどに問題があるようです。List アダプターで null 値を取得しています。誰かが私を助けたり、コードで何か間違ったことをしたかどうか教えてくれますか? 前もって感謝します。これが私のコードです。

アクティビティ -

public class TestJSON extends Activity{

    public static ListView lv;
    public static ProgressDialog pDialog;
    public static LazyAdapter adapter;

    static final String KEY_ITEMS = "items";
    static final String KEY_TITLE = "title";
    static final String KEY_ID = "id";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_json_view);

        new AsyncInitial().execute();
        // Showing progress dialog before sending http request
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait..");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.show();

        lv = (ListView)findViewById(R.id.list);
        //lv.setAdapter(new ArrayAdapter(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, items));
    }

    private class AsyncInitial extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {

            ArrayList<HashMap<String, String>> menuItems;

            @Override
            protected void onPreExecute() {
            }

            @Override
            protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {

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

                try {
                    //if (vid_num <= 0) {
                    // Get a httpclient to talk to the internet
                    HttpClient client = new DefaultHttpClient();
                    // Perform a GET request to YouTube for a JSON list of all the videos by a specific user
                    //https://gdata.youtube.com/feeds/api/videos?author="+username+"&v=2&alt=jsonc
                    HttpUriRequest request = new HttpGet("http://gdata.youtube.com/feeds/api/playlists/SP86E04995E07F6BA8?v=2&start-index=1&max-results=50&alt=jsonc");
                    // Get the response that YouTube sends back
                    HttpResponse response = client.execute(request);
                    // Convert this response into a readable string
                    String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
                    // Create a JSON object that we can use from the String
                    JSONObject json = new JSONObject(jsonString);

                    // For further information about the syntax of this request and JSON-C
                    // see the documentation on YouTube http://code.google.com/apis/youtube/2.0/developers_guide_jsonc.html

                    // Get are search result items
                    JSONArray jsonArray = json.getJSONObject("data").getJSONArray(KEY_ITEMS);

                    // Get the total number of video
                    //String vid_num = json.getJSONObject("data").getString("totalItems");
                    //System.out.println("vid_num-------->"+  vid_num);

                    // Loop round our JSON list of videos creating Video objects to use within our app
                    for (int i = 0; i < jsonArray.length(); i++) {

                        HashMap<String, String> map = new HashMap<String, String>();
                        JSONObject jsonObject = jsonArray.getJSONObject(i);

                        // The title of the video
                        String title = jsonObject.getJSONObject("video").getString(KEY_TITLE);
                        System.out.println("Title-------->"+  title);

                        // A url to the thumbnail image of the video
                        // We will use this later to get an image using a Custom ImageView
                        //String TAG_thumbUrl = jsonObject.getJSONObject("video").getJSONObject("thumbnail").getString("sqDefault");
                        //System.out.println("thumbUrl-------->"+  thumbUrl);

                        String id = jsonObject.getJSONObject("video").getString(KEY_ID);
                        System.out.println("video_id-------->"+  id);

                        map.put(title, KEY_TITLE);
                        map.put(id, KEY_ID);

                        menuItems.add(map); 

                    }


                } catch (ClientProtocolException e) {
                //Log.e("Feck", e);
                } catch (IOException e) {
                //Log.e("Feck", e);
                } catch (JSONException e) {
                //Log.e("Feck", e);
                }

                return menuItems;
            }


            @Override
            protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
                super.onPostExecute(result);
                adapter = new LazyAdapter(TestJSON.this,menuItems);    
                lv.setAdapter(adapter); 
                pDialog.dismiss();
            }


    }

}

そしてアダプター

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private static LayoutInflater inflater;
    private ArrayList<HashMap<String, String>> data;

    public LazyAdapter(TestJSON testJSON, ArrayList<HashMap<String, String>> menuItems) {
        activity = testJSON;
        data = menuItems;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override   
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi=convertView;
        if(convertView==null)vi = inflater.inflate(R.layout.list_row, null);

        TextView title = (TextView)vi.findViewById(R.id.title); 
        TextView id = (TextView)vi.findViewById(R.id.artist);


        HashMap<String, String> item = new HashMap<String, String>();
        item = data.get(position);

        title.setText(item.get(TestJSON.KEY_TITLE));
        id.setText(item.get(TestJSON.KEY_ID));

        return vi;
    }

}
4

0 に答える 0