Java 如何使用适配器填充ListView

Java 如何使用适配器填充ListView,java,android,android-fragments,android-listview,android-asynctask,Java,Android,Android Fragments,Android Listview,Android Asynctask,我知道这是一个非常简单的问题,但我是Android开发的新手,所以请对我放松。 我的问题在于我的主要活动中的一个片段(特别是AsyncTask)。 AsyncTask将数据发送到php脚本,然后php脚本以json格式返回相应的数据。然后对其进行处理并保存到jsonlist数组中。直到执行后一切正常,数据被下载、处理等。然而,当程序达到执行后问题开始弹出。基本上,我无法列出从jsonlist到listview的所有数据 //setting up an array ArrayLis

我知道这是一个非常简单的问题,但我是Android开发的新手,所以请对我放松。
我的问题在于我的主要活动中的一个片段(特别是AsyncTask)。 AsyncTask将数据发送到php脚本,然后php脚本以json格式返回相应的数据。然后对其进行处理并保存到
jsonlist
数组中。直到执行后一切正常,数据被下载、处理等。然而,当程序达到执行后问题开始弹出。基本上,我无法列出从
jsonlist
到listview的所有数据

    //setting up an array
    ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
    //creating list view variable
    ListView listview;
    //Define work in progress dialog
    private ProgressDialog mProgressDialog;  

private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            // Create a progressdialog
            mProgressDialog = new ProgressDialog(getActivity());

            // Set progressdialog title
            mProgressDialog.setTitle("Please wait");

            // Set progressdialog message
            mProgressDialog.setMessage("Fetching data...");
            mProgressDialog.setIndeterminate(false);

        }

        @Override
        protected Boolean doInBackground(String... params) {
            //To do in the background
            //Define variable of JSON parser type
            JSONParser jParser = new JSONParser();

            //Pass url to json parser class to fetch and save it into array variable
            JSONArray json = jParser.getJSONFromUrl(url);

            //loop from 0 to length of an array by increasing by 1
            for (int i = 0; i < json.length(); i++) {

                //Try and catch routine to prevent any crashes
                try {
                    //Get an object defined in { ... } in original json file
                    JSONObject c = json.getJSONObject(i);

                    //Separate object by obtaining values for each of the sections
                    String vtitle = c.getString(title);
                    String vcontent = c.getString(content);
                    String vuser = c.getString(user);

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

                    //Fill up an array with data extracted
                    map.put(title, vtitle);
                    map.put(content, vcontent);
                    map.put(user, vuser);

                    //Add values into jsonlist
                    jsonlist.add(map);

                } catch (JSONException e)
                {
                    //In case of any error Stack trace will be returned
                    e.printStackTrace();
                }
            }
            //Once everything has been done return null value
            return null;
        }
        @Override
        protected void onPostExecute(Boolean aBoolean) {
            //Insert all data downloaded through list adapter
            ListAdapter adapter = new SimpleAdapter(getActivity(), jsonlist, R.layout.list_activity, new String[] { title, content, user }, new int[] { R.id.title, R.id.content, R.id.user });

            // Locate the listview 
            //listview = (ListView) findViewById(R.id.list);

            // Set the adapter to the ListView
            //listview.setAdapter(adapter);

            //Get rid off dialog when operation of fetching data has been done
            if (mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
            }
        }
    }
但和前面的例子一样,返回未解析方法的错误。这是因为片段是由片段扩展的,添加任何内容都会使其崩溃

public class Fragment2 extends Fragment, ListActivity {...}

将其添加到
Fragment2
类中的代码中

private ListView listview;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.YOUR_FRAGMENT_LAYOUT, container, false);

    listview = (ListView) v.findViewById(R.id.R.id.list);
    return v;
}

由于您处于片段中,因此必须在
findViewById
之前调用
getView()
,如下所示

//Insert all data downloaded through list adapter
ListAdapter adapter = new SimpleAdapter(getActivity(), jsonlist, R.layout.list_activity, new String[] { title, content, user }, new int[] { R.id.title, R.id.content, R.id.user });

// Locate the listview 
listview = (ListView) getView().findViewById(R.id.list);
//Insert all data downloaded through list adapter
ListAdapter adapter = new SimpleAdapter(getActivity(), jsonlist, R.layout.list_activity, new String[] { title, content, user }, new int[] { R.id.title, R.id.content, R.id.user });

// Locate the listview 
listview = (ListView) getView().findViewById(R.id.list);