【问题标题】:How to fill ListView with Adapters如何用适配器填充 ListView
【发布时间】:2015-07-05 21:18:31
【问题描述】:

我知道这是一个非常简单的问题,但我是 Android 开发的新手,所以请放轻松。
我遇到的问题在于我的主要活动中的一个片段(特别是 AsyncTask)。 AsyncTask 将数据发送到 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();
            }
        }
    }

如您所见,我尝试了注释代码,但 listview = (ListView) findViewById(R.id.list); 返回以下错误:

无法解析方法 findViewById(int)

这会阻止我执行程序。这非常令人沮丧,因为我实际上将所需的所有数据都放在了一个数组中,但只有一行代码阻止我显示它。

我也试过了:

    ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.list_activity, new String[] { title, content, user }, new int[] { R.id.title, R.id.content, R.id.user });
    setListAdapter(adapter);
    lv = getListView();

但与前一种情况一样,返回未解决方法的错误。这是因为 Fragment 是由 Fragment 扩展的,并且向其中添加任何内容都会使其崩溃

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

【问题讨论】:

    标签: java android android-fragments android-listview android-asynctask


    【解决方案1】:

    将此添加到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;
    }
    

    【讨论】:

      【解决方案2】:

      由于您在 Fragment 中,因此您必须在 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);
      

      【讨论】:

        猜你喜欢
        • 2015-11-22
        • 2016-11-12
        • 1970-01-01
        • 2018-02-10
        • 1970-01-01
        • 2014-07-23
        • 2013-09-10
        • 1970-01-01
        • 2017-08-21
        相关资源
        最近更新 更多