如何更新片段中的ListView?

时间:2012-10-29 08:40:57

标签: java android listview android-fragments android-listfragment

我试图在Fragment中更新ListView,它实际上唯一可行的方法就是将片段设置为new。 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();
}

1 个答案:

答案 0 :(得分:0)

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

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

你可以复制这个:

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

进入片段中的onListItemClick()方法

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

希望这会有所帮助