0

我正在尝试在 Fragment 中更新 ListView,它实际工作的唯一方法是如果我将片段实例化为新的。adpter.notifyDataSetChanged() 不工作?为什么不呢?这里的代码活动和片段:

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();
}
4

1 回答 1

0

我尝试做类似的事情,我通过简单地重做片段事务解决了这个问题。

在您的情况下,您可以在您的onListItemClick()方法中完成此操作。

你可以复制这个:

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

onListItemClick()在你的片段中进入你的方法

这将重置片段。不幸的是,这也会将您的列表设置为空。解决这个问题的一种方法是使values列表成为类的private static变量TimerListFragment。这样,对列表所做的任何更改都将保持“已保存”,并且当您重新加载片段时,它将使用新更新的列表填充。

希望这可以帮助

于 2013-08-06T00:54:23.950 回答