0

我試圖在一個片段中更新一個ListView它實際上將工作的唯一方法是如果我instanciate片段新。 adpter.notifyDataSetChanged()不起作用?不是嗎?這裏的代碼的活動和片段:如何更新片段中的ListView?

public class TimerList extends Activity{ 

private DataSource datasource; 
TimerListFragment timerfragment; 
IntervalListFragment intervalfragment; 
public List<TimerObject> values; 
public String name; 
public String intervals; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.interval_fragment_container); 
    datasource = new DataSource(this); 
    datasource.open(); 
    values = datasource.getAllComments(); 

    if (savedInstanceState == null) { 

     Toast.makeText(getApplicationContext(), "MADE NEW FRAGMENTS", Toast.LENGTH_SHORT).show(); 
     timerfragment = new TimerListFragment(); 
     intervalfragment = new IntervalListFragment(); 

    } 

} 





public void delete(Long position){ 

    TimerObject timerobject = datasource.getTimerObject(position); 
    datasource.deleteComment(timerobject); 
    values = datasource.getAllComments(); 
} 

@Override 
protected void onStart() { 


    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.add(R.id.fragment_container, timerfragment); 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 


    super.onStart(); 
} 

我的片段:

public class TimerListFragment extends ListFragment { 

List<TimerObject> values ; 
Activity a; 
ArrayAdapter<TimerObject> adapter; 
ListView lv; 


@Override 
public void onCreate(Bundle savedInstanceState) { 

    setHasOptionsMenu(true); 
    values = ((TimerList)getActivity()).getValues(); 
    setBar(); 
    adapter = new ArrayAdapter<TimerObject>(getActivity(), android.R.layout.simple_list_item_1, values); 
    setListAdapter(adapter); 
    super.onCreate(savedInstanceState); 
} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 


    ((TimerList)getActivity()).play(Long.valueOf(position)); 
    super.onListItemClick(l, v, position, id); 
} 





public void setBar(){ 

     ActionBar actionbar = getActivity().getActionBar(); 
     actionbar.setTitle("Timer List"); 

} 

public void update(){ 
    setBar(); 
    Toast.makeText(getActivity(), "UPDATE", Toast.LENGTH_LONG).show(); 
    values = ((TimerList)getActivity()).getValues(); 
    adapter.notifyDataSetChanged(); 

} 



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

    return view; 

} 



@Override 
public void onStart() { 
    update(); 
    lv = getListView(); 
    lv.setOnItemLongClickListener(new OnItemLongClickListener() { 
     public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { 

       final TimerObject selected = adapter.getItem(position); 
       final Dialog d = new Dialog(getActivity()); 
       d.setContentView(R.layout.interval_deletetimer_dialog); 
       d.setTitle("Delete " + selected.getComment() + "?"); 
       ImageButton delete = (ImageButton) d.findViewById(R.id.delete); 
       delete.setOnClickListener(new OnClickListener() { 
        @Override 
         public void onClick(View v) { 
         ((TimerList)getActivity()).delete(Long.valueOf(selected.getId())); 


         update(); 
         } 
        }); 

       d.show(); 

       return true; 
       } 
      }); 
    super.onStart(); 
} 

回答

0

我試圖做同樣的事情,我簡單地重做片段交易解決了這個問題。

對於您的情況,您可以在您的onListItemClick()方法中完成此操作。在您的片段

這將重置片段

FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.add(R.id.fragment_container, timerfragment); 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 

到您的onListItemClick()方法:

你可以複製這一點。不幸的是,這也會將您的列表設置爲空。解決此問題的方法是使values列出TimerListFragment類的private static變量。這樣,對列表所做的任何更改都將保持「保存」狀態,並且當您重新加載片段時,它將填充新更新的列表。

希望這有助於