我的活动有ActionBar
和tabs
。每个选项卡代表一个片段,所有片段都在唯一的活动中运行。
我在第一个fragment
内有一个Spinner和一个ListView。在Spinner中选择项目时,ListView的数据将被修改并正确显示。
当我选择第二个选项卡,在那里执行某些操作并导航回第一个选项卡时,会出现问题。在Spinner上选择项目后,ListView
没有更新。
RouteFragment:
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
}
}
TabListener:
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");
}
}
我从数据库中获取的列表clientList
不是空的,我已经检查过了,正如您在代码中看到的那样。但是,ListView为空且没有反应并保持为空。
我想重点是我失去了我的ListView,但我只是不明白在哪里。
还有一件事:只有当我从一个标签移动到另一个标签时才会出现此问题。当我移动到我的应用程序内的另一个活动然后向后移动时,我仍然可以看到列表视图(它不是空的)并且可以更新它。
你能指点吗?
答案 0 :(得分:2)
我认为这会在状态发生变化后处理片段事务时发生,
getFragmentManager().beginTransaction().show(fragment).commitAllowingStateLoss();
或
getFragmentManager().beginTransaction().add(R.id.layout,fragment).commitAllowingStateLoss();
或
getFragmentManager().beginTransaction().replace(R.id.layout,fragment).commitAllowingStateLoss();
然后,
getFragmentManager().executePendingTransactions();
已编辑... 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) {
if(lv!=null)
{
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();
if(clientAdapter !=null)
{
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();
}
else
{
ClientAdapter clientAdapter = = new ClientAdapter(this.getActivity(),
R.layout.route_listview,
this.adapter.getClientList(routeId));
List<Client> clientList = DataAdapter.getInstance(Main.getContext())
.getClientList(route.Id);
clientAdapter.clear();
clientAdapter.addAll(clientList);
lv.setAdapter(clientListAdapter);
}
}
else
{
lv=(ListView)getActivity.findViewById(R.id.route_list);
ClientAdapter clientAdapter = = new ClientAdapter(this.getActivity(),
R.layout.route_listview,
this.adapter.getClientList(routeId));
List<Client> clientList = DataAdapter.getInstance(Main.getContext())
.getClientList(route.Id);
clientAdapter.clear();
clientAdapter.addAll(clientList);
lv.setAdapter(clientListAdapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}