我有一个活动ActionBar
和tabs
。每个选项卡代表一个片段,所有片段都在唯一的活动中运行。
我在第一个里面有一个 Spinner 和一个 ListView fragment
。当在 Spinner 中选择项目时,ListView 的数据被修改并正确显示。
当我选择第二个选项卡,在那里做某事并导航回第一个选项卡时,就会出现问题。之后ListView
在 Spinner 上选择项目时不会更新。
路由片段:
public class RouteFragment extends FragmentBase {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragView = inflater.inflate(R.layout.route, container, false);
Spinner spinner = (Spinner) fragView.findViewById(R.id.route_list);
ArrayAdapter<Route> routeListAdapter = new ArrayAdapter<Route>(this.getActivity(),
android.R.layout.simple_spinner_item,
this.adapter.getRouteList());
spinner.setAdapter(routeListAdapter);
long routeId = ((Route) spinner.getSelectedItem()).Id;
ListView lv = (ListView) fragView.findViewById(R.id.client_list);
final ClientAdapter clientListAdapter = new ClientAdapter(this.getActivity(),
R.layout.route_listview,
this.adapter.getClientList(routeId));
lv.setAdapter(clientListAdapter);
routeListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new RouteSpinnerItemSelectedListener(lv));
return fragView;
}
}
RouteSpinnerItemSelectedListener:
public class RouteSpinnerItemSelectedListener implements OnItemSelectedListener {
private ListView lv;
public RouteSpinnerItemSelectedListener(ListView fragView) {
this.lv = fragView;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Route route = (Route) parent.getItemAtPosition(position);
android.util.Log.v("Salesman", "selected route id: " + route.Id);
android.util.Log.v("Salesman", "selected route name: " + route.Name);
ClientAdapter clientAdapter = (ClientAdapter) lv.getAdapter();
Log.v("Salesman", "before clear: " + clientAdapter.getCount() + "");
clientAdapter.clear();
Log.v("Salesman", "after clear: " + clientAdapter.getCount() + "");
List<Client> clientList = DataAdapter.getInstance(Main.getContext())
.getClientList(route.Id);
Log.v("Salesman", "raw from DB" + clientList.size() + "");
clientAdapter.addAll(clientList);
Log.v("Salesman", "after addAll()" + clientAdapter.getCount() + "");
clientAdapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
选项卡监听器:
public class TabListener implements ActionBar.TabListener {
private FragmentBase mFragment;
public TabListener(FragmentBase fragment) {
this.mFragment = fragment;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.fragment_content, mFragment, mFragment.getName());
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
android.util.Log.v("Salesman", "reselected");
}
}
我从数据库中获得的ListclientList
不是空的,我已经检查过了,正如您在代码中看到的那样。但是,ListView 是空的并且没有反应并且保持为空。
我想关键是我在某个地方丢失了我的 ListView,但我只是不明白在哪里。
还有一件事:只有当我从一个选项卡移动到另一个选项卡时才会出现该问题。当我移动到我的应用程序中的另一个活动然后返回时,我仍然可以看到列表视图(它不是空的)并且可以更新它。
你能指点一下吗?