无法在片段中创建适配器。想要显示来自JSON的数据

时间:2014-10-08 13:57:07

标签: android json android-fragments

我想在片段中使用JSON显示数组列表。代码在活动中运行良好,但在片段中运行不正常。代码是。我只想使用JSON显示数据列表,如果用户单击数据必须在另一个片段中显示的代码。

package com.example.everwinvidhyashram;


public class PrincipalSpeechFragment extends Fragment implements OnClickListener {

private ProgressDialog pDialog;
private static String url =    

 "http://imaginetventures.net/sample/everwin_vidhyashram/webservice/rest/?module=speech&from=1-9-  

2014&to=30-9-2014";

// JSON Node names
private static final String TAG_PRINCIPAL_SPEECH ="Principal Speech";
private static final String TAG_SPEECH= "speech";
private static final String TAG_DESC = "desc";


// contacts JSONArray
JSONArray contacts = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> speechlist;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_principal_speech, container, false);

    return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

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

    //      ListView lv = getListView();

    new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {

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

    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        ServiceHandler sh = new ServiceHandler();
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

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

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

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray(TAG_PRINCIPAL_SPEECH);

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

                    String id = c.getString(TAG_SPEECH);




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

                            // adding each child node to HashMap key => value
                    speech.put(TAG_SPEECH, id);
                    //        speech.put(TAG_DESC, name);

                    // adding contact to contact list
                    speechlist.add(speech);
                }
            } 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(PrincipalSpeechFragment.this, speechlist,
                R.layout.principal_speech_items, new String[] { TAG_SPEECH, 
                }, new int[] { R.id.principal,
                });



        setListAdapter(adapter);

    }

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}
}

问题出现在

ListAdapter adapter = new SimpleAdapter(PrincipalSpeechFragment.this, speechlist,
                R.layout.principal_speech_items, new String[] { TAG_SPEECH, 
                }, new int[] { R.id.principal,
                });



        setListAdapter(adapter);

1 个答案:

答案 0 :(得分:0)

我在片段中使用了这个列表适配器。可能会有所帮助。

protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter =new SimpleAdapter(
                    getActivity(), speechlist,
                    R.layout.principal_speech_items, new String[] { TAG_SPEECH },
                    new int[] { R.id.principal });
                // updating listview
                setListAdapter(adapter);

               }//run

           });//runOnUiThread

    }//onpostexecute