Android ListView在返回到其片段后变为空
我在片段中有一个Listview。我面临的问题是,每当我切换到另一个片段时,列表就会变为空,并且必须再次加载 我如何保存列表的实例,就像他们在Instagram上做的那样(在同一个列表项上完全按照您留下的方式获取列表) 片段的代码:Android ListView在返回到其片段后变为空,android,listview,android-fragments,instance,Android,Listview,Android Fragments,Instance,我在片段中有一个Listview。我面临的问题是,每当我切换到另一个片段时,列表就会变为空,并且必须再次加载 我如何保存列表的实例,就像他们在Instagram上做的那样(在同一个列表项上完全按照您留下的方式获取列表) 片段的代码: public class ListActivity extends ListFragment { public void ToastLoadShout(String msg) { Toast.makeText(getActivity(),
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 = "";
String savedlat="0.0";
String savedlon="0.0";
@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();
}
@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)
{
//adapter.deleteList();
//list.destroyDrawingCache();
if (loc.get(Preferences.LAT) != savedlat && loc.get(Preferences.LON)!=savedlon){
new Load().execute();
savedlat=loc.get(Preferences.LAT);
savedlon=loc.get(Preferences.LON);
}
}
else
ToastLoadShout("Get Location First.");
relist = (Button) getView().findViewById(R.id.relist);
relist.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
listload.setVisibility(View.INVISIBLE);
// if (adapter!=null){
// list.destroyDrawingCache();
// new Load().execute();
// 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){
adapter.deleteList();
list.destroyDrawingCache();
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();
}
}
但这没用
编辑#2:
我的代码现在看起来是这样的,但它不起作用:
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 = "";
String savedlat="0.0";
String savedlon="0.0";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_list, container, false);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
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)
{
//adapter.deleteList();
//list.destroyDrawingCache();
if (loc.get(Preferences.LAT) != savedlat && loc.get(Preferences.LON)!=savedlon){
new Load().execute();
savedlat=loc.get(Preferences.LAT);
savedlon=loc.get(Preferences.LON);
}
}
else
ToastLoadShout("Get Location First.");
relist = (Button) getView().findViewById(R.id.relist);
relist.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
listload.setVisibility(View.INVISIBLE);
// if (adapter!=null){
// list.destroyDrawingCache();
// new Load().execute();
// 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){
adapter.deleteList();
list.destroyDrawingCache();
new Load().execute();}
else
ToastLoadShout("Get Location First.");
}});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
@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();
}
}
公共类ListActivity扩展ListFragment{
public void ToastLoadShout(字符串消息){
Toast.makeText(getActivity(),msg.toString(),Toast.LENGTH_LONG).show();
}
私有静态视图;
HttpClient;
HttpPost-HttpPost;
HttpGet-HttpGet;
JSONObject json;
清单项目;
列表标记=新的ArrayList();
手机适配器;
列表视图列表;
进度条列表加载;
按钮重新列表;
偏好优先;
字符串datadata=“”;
字符串savedlat=“0.0”;
字符串savedlon=“0.0”;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
返回充气机。充气(R.layout.activity\u列表,容器,错误);
}
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setRetainInstance(真);
试一试{
pref=新的首选项(getActivity());
list=(ListView)getView().findviewbyd(android.R.id.list);
listload=(ProgressBar)getView().findViewById(R.id.listload);
HashMap loc=pref.getData();
ToastLoadShout(loc.get(Preferences.LAT)+“,”
+loc.get(Preferences.LON));
if(loc.get(Preferences.LAT)!=“0.0”和&loc.get(Preferences.LAT)!=null)
{
//adapter.deleteList();
//list.destroyDrawingCache();
if(loc.get(Preferences.LAT)!=savedlat&&loc.get(Preferences.LON)!=savedlon){
新加载().execute();
savedlat=loc.get(Preferences.LAT);
savedlon=loc.get(Preferences.LON);
}
}
其他的
ToastLoadShout(“首先获取位置”);
relist=(按钮)getView().findViewById(R.id.relist);
relist.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
listload.setVisibility(View.INVISIBLE);
//if(适配器!=null){
//list.destroyDrawingCache();
//新加载().execute();
//adapter.deleteList();
//
// }
HashMap loc=pref.getData();
ToastLoadShout(loc.get(Preferences.LAT)+“,”
+loc.get(Preferences.LON));
if(loc.get(Preferences.LAT)!=“0.0”
&&loc.get(Preferences.LAT)!=null){
adapter.deleteList();
list.destroyDrawingCache();
新加载().execute();}
其他的
ToastLoadShout(“首先获取位置”);
}});
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setRetainInstance(真);
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
//获取所选项目
//字符串selectedValue=(字符串)getListAdapter().getItem(位置);
String selectedValue=markers.get(position.getTitle();
Toast.makeText(getActivity(),selectedValue,Toast.LENGTH_SHORT).show();
}
}
编辑#3:
片段的主要(主机)活动:
public class Fragments extends FragmentActivity {
Fragment newFragment;
Button Add;
public void ToastLoadShout(String msg) {
Toast.makeText(this, msg.toString(), Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragments);
//Set Custom actionBar<
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.titlebar);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
//Set Custom actionBar>
ListActivity fragment = new ListActivity();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.fragment_place, fragment,"Nearby");
transaction.commit();
turnGPSOn();
Add = (Button)findViewById(R.id.add);
Add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent = new Intent(Fragments.this,Add.class);
Bundle bndlanimation =
ActivityOptions.makeCustomAnimation(getApplicationContext(),
R.anim.animation,R.anim.animation2).toBundle();
startActivity(Intent, bndlanimation);
}
});
/* For putting commas in attractin's checkIns
String number = "1345";
int amount = Integer.parseInt(number);
DecimalFormat formatter = new DecimalFormat("#,###");
ToastLoadShout(formatter.format(amount));*/
}
public void onSelectFragment(View view) {
String fragTag="";
boolean needNew=false;
if (view == findViewById(R.id.map))
{
Fragment f = getSupportFragmentManager().findFragmentByTag("Map");
if (f==null){
newFragment = new MainActivity();
needNew=true;
fragTag="Map";
}
else{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_place, f, "Map"); //or whatever other string you want to use
transaction.addToBackStack(null);
transaction.commit();
}
}
else if (view == findViewById(R.id.nearby))
{
Fragment f = getSupportFragmentManager().findFragmentByTag("Nearby");
if (f==null){
newFragment = new ListActivity();
needNew=true;
fragTag="Nearby";
}
else{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_place, f, "Nearby"); //or whatever other string you want to use
transaction.addToBackStack(null);
transaction.commit();
}
}
// else
// {
// newFragment = new ListActivity();
// }
if (needNew) {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_place, newFragment, fragTag);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
公共类片段扩展了FragmentActivity{
新片段;
按钮添加;
public void ToastLoadShout(字符串消息){
Toast.makeText(this,msg.toString(),Toast.LENGTH_SHORT).show();
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_片段);
//设置自定义操作栏<
getActionBar().setDisplayOptions(ActionBar.DISPLAY\u SHOW\u CUSTOM);
getActionBar().setCustomView(R.layout.titlebar);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
//设置自定义操作栏>
ListActivity片段=新的ListActivity();
FragmentTransaction=getSupportFragmentManager()
.beginTransaction();
交易。添加(R.id.fragment_place,fragment,“附近”);
commit();
turnGPSOn();
Add=(按钮)findViewById(R.id.Add);
Add.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
Intent Intent=newintent(Fragments.this,Add.class);
捆绑式动画=
ActivityOptions.makeCustomAnimation(getApplicationContext(),
R.anim.animation,R.anim.animation2).toBundle();
startActivity(意图、动画);
}
});
/*用于在attractin的签入中添加逗号
字符串编号=“1345”;
int amount=Integer.parseInt(数字);
DecimalFormat formatter=新的DecimalFormat(“#,##,##”);
ToastLoadShout(formatter.format(amount))*/
}
公共void onSelectFragment(视图){
字符串fragTag=“”;
布尔needNew=false;
if(view==findViewById(R.id.map))
{
片段f=getSupportFragmentManager().findFragmentByTag(“映射”);
如果(f)=nu
public class Fragments extends FragmentActivity {
Fragment newFragment;
Button Add;
public void ToastLoadShout(String msg) {
Toast.makeText(this, msg.toString(), Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragments);
//Set Custom actionBar<
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.titlebar);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
//Set Custom actionBar>
ListActivity fragment = new ListActivity();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.fragment_place, fragment,"Nearby");
transaction.commit();
turnGPSOn();
Add = (Button)findViewById(R.id.add);
Add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent = new Intent(Fragments.this,Add.class);
Bundle bndlanimation =
ActivityOptions.makeCustomAnimation(getApplicationContext(),
R.anim.animation,R.anim.animation2).toBundle();
startActivity(Intent, bndlanimation);
}
});
/* For putting commas in attractin's checkIns
String number = "1345";
int amount = Integer.parseInt(number);
DecimalFormat formatter = new DecimalFormat("#,###");
ToastLoadShout(formatter.format(amount));*/
}
public void onSelectFragment(View view) {
String fragTag="";
boolean needNew=false;
if (view == findViewById(R.id.map))
{
Fragment f = getSupportFragmentManager().findFragmentByTag("Map");
if (f==null){
newFragment = new MainActivity();
needNew=true;
fragTag="Map";
}
else{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_place, f, "Map"); //or whatever other string you want to use
transaction.addToBackStack(null);
transaction.commit();
}
}
else if (view == findViewById(R.id.nearby))
{
Fragment f = getSupportFragmentManager().findFragmentByTag("Nearby");
if (f==null){
newFragment = new ListActivity();
needNew=true;
fragTag="Nearby";
}
else{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_place, f, "Nearby"); //or whatever other string you want to use
transaction.addToBackStack(null);
transaction.commit();
}
}
// else
// {
// newFragment = new ListActivity();
// }
if (needNew) {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_place, newFragment, fragTag);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
@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 View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_list, container, false);
}