2013-03-26 61 views
1

當我有ActionBartabs一個失去活性。每個選項卡代表一個片段,全部在唯一的活動中運行。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但我只是不明白的地方。

還有一件事:只有當我從一個標籤移動到另一個時,纔會出現該問題。當我移動到我的應用程序內的另一個活動,然後移回時,我仍然可以看到列表視圖(它不是空的)並可以更新它。

你能指點一下嗎?

回答

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 

} 
} 
+0

當這個答案可以幫助你,給予好評/ Accpet頭答案.. – 2013-03-27 06:38:04

+0

是否也能請點我應該在哪裏插入代碼?我的猜測是:tablistener.ontabselected? – cheshie 2013-03-27 07:34:21

+0

如何處理您的交易片段,請添加/編輯代碼到你question.And還要檢查可以點擊列表視圖 – 2013-03-27 07:39:58