當我有ActionBar
和tabs
一個失去活性。每個選項卡代表一個片段,全部在唯一的活動中運行。ListView中得到改變到另一個標籤
我有一個微調和第一fragment
內一個ListView。當在Spinner中選擇的項目時,ListView的數據被修改並正確顯示。
當我選擇第二個選項卡,在那裏執行操作並導航回第一個選項卡時,就會出現問題。之後ListView
在Spinner上選擇的項目時不會更新。
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 somewher但我只是不明白的地方。
還有一件事:只有當我從一個標籤移動到另一個時,纔會出現該問題。當我移動到我的應用程序內的另一個活動,然後移回時,我仍然可以看到列表視圖(它不是空的)並可以更新它。
你能指點一下嗎?
當這個答案可以幫助你,給予好評/ Accpet頭答案.. – 2013-03-27 06:38:04
是否也能請點我應該在哪裏插入代碼?我的猜測是:tablistener.ontabselected? – cheshie 2013-03-27 07:34:21
如何處理您的交易片段,請添加/編輯代碼到你question.And還要檢查可以點擊列表視圖 – 2013-03-27 07:39:58