我在Android应用程序中使用列表视图有3个选项卡布局,我从json文件中获取数据。因此,当我运行应用程序数据时,第一个选项卡上,当我转到第二个数据时,数据不会出现相同的情况,第三个选项卡数据不会出现,但当我返回第二个选项卡时,数据将出现在2个选项卡上并且不在其他标签中。
这是我的片段类: -
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;
}
}