Android Json没有使用片段获取所有选项卡中的数据
我在我的android应用程序中使用列表视图有3个选项卡布局,我正在从json文件中获取数据。因此,当我运行应用程序时,数据出现在第一个选项卡上,当我转到第二个选项卡时,数据不出现在第三个选项卡中的相同情况数据不出现,但当我返回到第二个选项卡时,数据出现在第二个选项卡上,而不出现在其他选项卡中 这是我的片段类:-Android Json没有使用片段获取所有选项卡中的数据,android,json,Android,Json,我在我的android应用程序中使用列表视图有3个选项卡布局,我正在从json文件中获取数据。因此,当我运行应用程序时,数据出现在第一个选项卡上,当我转到第二个选项卡时,数据不出现在第三个选项卡中的相同情况数据不出现,但当我返回到第二个选项卡时,数据出现在第二个选项卡上,而不出现在其他选项卡中 这是我的片段类:- package ww.ibc24.in; import android.app.ProgressDialog; import android.os.AsyncTask; import
package ww.ibc24.in;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import ww.ibc24.in.model.WorldsBillionaires;
public class BigNews extends Fragment {
private static final String TAG = MainActivity.class.getSimpleName();
//Billionaires json url
private ProgressDialog pDialog;
private List<WorldsBillionaires> worldsBillionairesList = new ArrayList<>();
private ListView listView;
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String RANK = "id";
static String COUNTRY = "title";
static String POPULATION = "category";
static String FLAG = "image";
static String DATE = "releaseYear";
static String DESCRIPTION = "detail";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.primary_layout, null);
listView = (ListView) view.findViewById(R.id.list);
// Showing progress dialog before making http request
new DownloadJSON().execute();
// Creating volley request obj
return view;
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
mProgressDialog.setTitle("Android JSON Parse Tutorial");
// 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://192.168.25.18:8080/bignews.json");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("worldpopulation");
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("id", jsonobject.getString("id"));
map.put("title", jsonobject.getString("title"));
map.put("category", jsonobject.getString("category"));
map.put("image", jsonobject.getString("image"));
map.put("releaseYear", jsonobject.getString("releaseYear"));
map.put("detail", jsonobject.getString("detail"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} 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) getActivity().findViewById(R.id.list);
arraylist.clear();
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(getActivity(), arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
and this is my adapter class:-
package ww.ibc24.in;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView rank;
TextView country;
TextView population;
ImageView flag;
TextView date;
TextView desc;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
rank = (TextView) itemView.findViewById(R.id.rank);
country = (TextView) itemView.findViewById(R.id.country);
date = (TextView) itemView.findViewById(R.id.date);
population = (TextView) itemView.findViewById(R.id.population);
desc = (TextView) itemView.findViewById(R.id.desc);
// Locate the ImageView in listview_item.xml
flag = (ImageView) itemView.findViewById(R.id.thumbnail);
// Capture position and set results to the TextViews
rank.setText(resultp.get(BigNews.RANK));
country.setText(resultp.get(BigNews.COUNTRY));
population.setText(resultp.get(BigNews.POPULATION));
date.setText(resultp.get(BigNews.DATE));
desc.setText(resultp.get(BigNews.DESCRIPTION));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(BigNews.FLAG), flag);
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data rank
intent.putExtra("id", resultp.get(BigNews.RANK));
// Pass all data country
intent.putExtra("title", resultp.get(BigNews.COUNTRY));
// Pass all data population
intent.putExtra("category",resultp.get(BigNews.POPULATION));
// Pass all data flag
intent.putExtra("image", resultp.get(BigNews.FLAG));
intent.putExtra("releaseYear", resultp.get(BigNews.DATE));
intent.putExtra("detail", resultp.get(BigNews.DESCRIPTION));
// Start SingleItemView Class
context.startActivity(intent);
}
});
return itemView;
}
}
包ww.ibc24.in;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.annotation.Nullable;
导入android.support.v4.app.Fragment;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ListView;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入ww.ibc24.in.model.WorldsBillionaires;
公共类BigNews扩展了片段{
私有静态最终字符串标记=MainActivity.class.getSimpleName();
//亿万富翁json网址
私人对话;
私有列表worldsBillionairesList=newarraylist();
私有列表视图列表视图;
JSONObject JSONObject;
JSONArray JSONArray;
列表视图列表视图;
ListViewAdapter适配器;
进程对话框;
ArrayList ArrayList;
静态字符串RANK=“id”;
静态字符串COUNTRY=“title”;
静态字符串POPULATION=“category”;
静态字符串FLAG=“image”;
静态字符串DATE=“releaseYear”;
静态字符串DESCRIPTION=“detail”;
@可空
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.primary_layout,null);
listView=(listView)view.findViewById(R.id.list);
//在发出http请求之前显示进度对话框
新建下载JSON().execute();
//创建截击请求对象
返回视图;
}
//下载JSON异步任务
私有类下载JSON扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//创建一个progressdialog
mProgressDialog=newprogressdialog(getActivity());
//设置进程对话框标题
setTitle(“Android JSON解析教程”);
//设置进程对话框消息
设置消息(“加载…”);
mProgressDialog.setUndeterminate(false);
//显示进度对话框
mProgressDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//创建一个数组
arraylist=新的arraylist();
//从给定的URL地址检索JSON对象
jsonobject=JSONfunctions
.getJSONfromURL(“http://192.168.25.18:8080/bignews.json");
试一试{
//在JSON中找到数组名称
jsonarray=jsonobject.getJSONArray(“世界人口”);
for(int i=0;i