gpt4 book ai didi

java - 在 android 中实现 AsyncTask 和 Listview 的 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:38:36 25 4
gpt4 key购买 nike

这是我的 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();
}
}

}

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

最佳答案

fragment 不是上下文。您必须在使用 CommunityFragment.this 的地方使用 getActivity()

替换

mProgressDialog = new ProgressDialog(CommunityFragment.this);

mProgressDialog = new ProgressDialog(getActivity());

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

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

关于java - 在 android 中实现 AsyncTask 和 Listview 的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20841618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com