繁体   English   中英

片段在android中实现AsyncTask和Listview

[英]Fragment implementing AsyncTask and Listview in android

这是我的Java代码

但它不起作用我不会得到我的错误。 Snpashot

package info.androidhive.slidingmenu;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;   
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class CommunityFragment extends Fragment {


    public CommunityFragment(){}

    // Declare Variables
    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String RANK = "rank";
    static String COUNTRY = "country";
    static String POPULATION = "population";
    static String FLAG = "flag";


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

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

        new DownloadJSON().execute();
        return rootView;
    }
    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(CommunityFragment.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Student Government App");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONFunctions
                    .getJSONfromURL("http://app-dlslsg.azurewebsites.net/json/postList.php");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("post");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    map.put("rank", jsonobject.getString("id"));
                    map.put("country", jsonobject.getString("body"));
                    map.put("population", jsonobject.getString("stamp"));
                    map.put("flag", jsonobject.getString("image"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                    //Log.i("body",COUNTRY);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(CommunityFragment.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }

}

我今天尝试在网上搜索,但我没有机会看到任何相关的问题。 有人会帮我解决这个问题吗? 我会尽快回复那些回复, 感谢您的帮助!

片段不是Context。 你必须使用getActivity() ,你正在使用CommunityFragment.this

更换

mProgressDialog = new ProgressDialog(CommunityFragment.this);

mProgressDialog = new ProgressDialog(getActivity());

adapter = new ListViewAdapter(CommunityFragment.this, arraylist);

adapter = new ListViewAdapter(getActivity(), arraylist);

哦,所以有多个错误:

  1. ListViewAdapter构造函数需要一个Activity(或Context),但您传递的是Fragment。 您可以使用Fragment.getActivity()来获取Fragment.getActivity()的活动,但前提是它已附加。
  2. ProgressDialog构造函数需要Context对象,但您再次传递Fragment。 Fragment不会扩展Context,但是Activity类会扩展。 同样,您可以传递Fragment.getActivity()
  3. 最后,您尝试在AsyncTask中调用findViewById()表单。 这是一个仅在Activity内部可用的方法,因此您需要再次获取Activity并通过Activity对象调用该方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM