我在片段中有一个列表视图。问题是每次我打开该片段时它都会重新加载。
我从网上的列表中获取信息,因此每次下载并构建列表都是一个真正的问题。
那是我的代码:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.free.MainActivity.Load;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
public class ListActivity extends ListFragment {
public void ToastLoadShout(String msg) {
Toast.makeText(getActivity(), msg.toString(), Toast.LENGTH_LONG).show();
}
private static View View;
HttpClient client;
HttpPost httppost;
HttpGet httpget;
JSONObject json;
List<List<String>> items;
List<item> markers = new ArrayList<item>();
MobileArrayAdapter adapter;
ListView list;
ProgressBar listload;
Button relist;
Preferences pref;
String datadata = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (View != null) {
ViewGroup parent = (ViewGroup) View.getParent();
if (parent != null)
parent.removeView(View);
}
View = inflater.inflate(R.layout.activity_list, container, false);
return View;
}
@Override
public void onStart() {
super.onStart();
// pref = new Preferences(getActivity());
//
// listload = (ProgressBar) getView().findViewById(R.id.listload);
//
// relist = (Button) getView().findViewById(R.id.relist);
// relist.setOnClickListener(new OnClickListener() {
// public void onClick(View v) {
// listload.setVisibility(View.INVISIBLE);
// if (adapter!=null)
// adapter.deleteList();
// HashMap<String, String> loc = pref.getData();
// ToastLoadShout(loc.get(Preferences.LAT) + ","
// + loc.get(Preferences.LON));
// if (loc.get(Preferences.LAT) != "0.0"
// && loc.get(Preferences.LAT) != null)
// new Load().execute();
// else
// ToastLoadShout("Get Location First.");
// }});
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
try {
pref = new Preferences(getActivity());
list = (ListView) getView().findViewById(android.R.id.list);
listload = (ProgressBar) getView().findViewById(R.id.listload);
HashMap<String, String> loc = pref.getData();
ToastLoadShout(loc.get(Preferences.LAT) + ","
+ loc.get(Preferences.LON));
if (loc.get(Preferences.LAT) != "0.0"
&& loc.get(Preferences.LAT) != null)
new Load().execute();
else
ToastLoadShout("Get Location First.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// get selected items
//String selectedValue = (String) getListAdapter().getItem(position);
String selectedValue = markers.get(position).getTitle();
Toast.makeText(getActivity(), selectedValue, Toast.LENGTH_SHORT).show();
}
class Load extends AsyncTask<String, Integer, Boolean> {
@Override
protected void onPreExecute() {
listload.setVisibility(View.VISIBLE);
}
@Override
protected Boolean doInBackground(String... params) {
try {
items = DownloadList();
if (items != null)
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
@Override
protected void onPostExecute(Boolean res) {
// TODO Auto-generated method stub
if (res) {
ArrangePutMarkers();
adapter=new MobileArrayAdapter(getActivity(), markers);
list.setAdapter(adapter);
// ToastLoadShout(Integer.parseInt(items.get(0).get(0))/* id */+","+ items
// .get(1).get(0)/* title */+","+ Integer.parseInt(items
// .get(2).get(0))/* cat */+","+ Integer
// .parseInt(items.get(3).get(0))/* program */+","+
// items.get(4).get(0)/* desc */+","+
// items.get(5).get(0)/* pub */+","+ Integer.parseInt(items
// .get(6).get(0))/* view */+","+ Integer
// .parseInt(items.get(7).get(0))/* like */+","+ Integer
// .parseInt(items.get(8).get(0))/* upvote */+","+
// Integer.parseInt(items.get(9).get(0))/* downvote */+","+
// Double.parseDouble(items.get(10).get(0))/* lat */+","+
// Double.parseDouble(items.get(11).get(0))/* lon */);
} else {
ToastLoadShout("Error");
ToastLoadShout(datadata);
}
listload.setVisibility(View.INVISIBLE);
}
}
public void ArrangePutMarkers() {
if (items != null) {
for (int i = 0; i < items.get(0).size(); i++) {
markers.add(new item(
Integer.parseInt(items.get(0).get(i))/* id */, items
.get(1).get(i)/* title */, Integer.parseInt(items
.get(2).get(i))/* cat */, Integer
.parseInt(items.get(3).get(i))/* program */,
items.get(4).get(i)/* desc */,
items.get(5).get(i)/* pub */, Integer.parseInt(items
.get(6).get(i))/* view */, Integer
.parseInt(items.get(7).get(i))/* like */, Integer
.parseInt(items.get(8).get(i))/* upvote */,
Integer.parseInt(items.get(9).get(i))/* downvote */,
Double.parseDouble(items.get(10).get(i))/* lat */,
Double.parseDouble(items.get(11).get(i))/* lon */));
}
}
}
public void onBackPressed() {
}
}
这里有什么问题?