2015-08-27 73 views
0

我有兩個選項卡的活動。使用擴展FragmentStatePagerAdapter的Custom Adpater創建選項卡。將項目添加到ListView AFTER使用自定義適配器刪除ListView中的所有項目?

public CustomerScrollAdpater(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb) { 
    super(fm); 

    this.Titles = mTitles; 
    this.NumbOfTabs = mNumbOfTabsumb; 
    addCustomer=new AddCustomerFragment(); 
    editCustomer=new CustomerFragment(); 
} 

//This method return the fragment for the every position in the View Pager 
@Override 
public Fragment getItem(int position) { 
    if(position == 0) // if the position is 0 we are returning the First tab 
    { 
     return addCustomer; 
    } 
    else{ // else if the position is 1 we are returning the second tab 
     return editCustomer; 
    } 
} 

AddCustomerFragment有以下方法連接到按鈕,更新一個共同的ArrayList(此ArrayList是任何活動或片段訪問)。

public void saveCustomer(){ 
    Customer customer = new Customer(); 
    customer.setNic("2222222222V"); 
    AppController.getInstance().getCustomers().add(customer); 

    Toast.makeText(getActivity(),"Customer successfully added",Toast.LENGTH_SHORT).show(); 
} 

變化ArrayList中會顯示在下一個製表CustomerFragment.Some代碼在CustomerFragment,

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_customer_list, container, false); 

    listView = (ListView)view.findViewById(R.id.customerListView); 

    adapter = new CustomerListAdapter(AppController.getInstance().getCustomers(),getActivity()); 

    listView.setAdapter(adapter); 

    return view; 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    Toast.makeText(getActivity(),"Started again",Toast.LENGTH_SHORT).show(); 
} 

這裏是我的自定義adaper。

public class CustomerListAdapter extends BaseAdapter { 

private ArrayList<Customer> customers; 
private Context context; 

public CustomerListAdapter(ArrayList<Customer> customers,Context context){ 
    super(); 
    this.customers = customers; 
    this.context = context; 
} 
@Override 
public int getCount() { 
    return customers.size(); 
} 

@Override 
public Customer getItem(int position) { 
    return (null == customers)? null : customers.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final int cusPosition = position; 
    LayoutInflater layoutInflater = LayoutInflater.from(context); 
    final View theView = layoutInflater.inflate(R.layout.customer_list_item, parent, false); 

    TextView textView = (TextView) theView.findViewById(R.id.textCustomerNic); 
    textView.setText(customers.get(cusPosition).getNic()); 

    ImageView imageView = (ImageView) theView.findViewById(R.id.deleteCustomer); 
    imageView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      AppController.getInstance().getCustomers().remove(customers.get(cusPosition)); 
      notifyDataSetChanged(); 
     } 
    }); 
    return theView; 
} 

}

這工作得很好。我可以刪除客戶並添加客戶。

的問題發生在我,

  1. 點擊ImageView的刪除所有客戶。 (如果我刪除所有客戶,問題就會發生,否則它會正常工作。)

  2. 去添加客戶選項卡。

  3. 添加客戶,然後單擊保存按鈕(觸發saveCustomer()。)
  4. 滑回以查看所有客戶。
  5. 沒有添加客戶(即使顯示客戶添加的信息)。

我做錯了什麼?請解釋。謝謝。

回答

1

你需要調用

adapter.notifyDataSetChanged(); 

在片段saveCustomer()方法。

或者您可以使用Android LocalBroadCastManager觸發廣播,您可以在其中調用adaper.notifyDataSetChanged()方法的適配器的另一個片段中偵聽。

+0

肯定。我不得不調用adapter.notifyDataSetChanged()。但首先,我必須在AppController中創建我的CustomerAdapter,它從應用程序擴展而來。這使我的CustomerAdapter全球通用。感謝maveroid。 – SajithK

+0

而且我發現這個答案@Tyler Davis在[這個問題](http://stackoverflow.com/questions/10073214/viewpager-setoffscreenpagelimit0-doesnt-work-as-expected)是非常有趣的。我在CustomerFragment中使用了這個'@Override public void setUserVisibleHint(boolean isVisibleToUser){ super.setUserVisibleHint(isVisibleToUser);如果(isVisibleToUser){ adapter.notifyDataSetChanged(); } }' – SajithK

0

您需要更改適配器內的引用列表。將此方法添加到適配器,並在列表更改時將新列表作爲參數進行調用。

public void refresh(ArrayList<Customer> customers){   
     this.customers = customers; 
     notifyDataSetChanged(); 
    } 
+0

所以我必須註冊一個監聽器到列表中?如果是的話監聽器是什麼? – SajithK

+0

如果您從適配器進行更改,只需調用refresh(AppController.getInstance()。getCustomers(),getActivity()); 但是,如果您是從其他地方進行更改,例如從片段中調用 adapter.refresh(AppController.getInstance()。getCustomers(),getActivity()); –

0

如果我找到了你,你在一個活動中顯示兩個片段。 一個用於添加客戶 一個用於顯示客戶列表 嘗試調試和檢查新創建的客戶數據,其實就是這樣,如果你的理想就只需要通知列表適配器 adapter.notifyDataSetChanged()

但是,如果它仍然沒有反映,那麼當你滑動時可能沒有任何片段實際上從內存中移除,並且它顯示舊內容。嘗試有力地重新創建片段。

FragmentTransaction tr = getFragmentManager().beginTransaction(); 
 
tr.replace(R.id.your_fragment_container, yourFragmentInstance); 
 
tr.commit()

相關問題