如何在添加数据后刷新Fragment类中的Listview?

时间:2014-03-04 10:31:48

标签: android android-listview android-fragments android-sqlite

我将数据插入Sqlitedatabase。我希望这些数据显示在片段类的Listview中。问题是我的listview没有立即加载新数据。换句话说,在我去其他活动并回来之前,它并没有得到更新。任何人都可以建议我一个解决方案。我的代码如下:

public class SentTaskFragment extends Fragment {
 ArrayList<HashMap<String, String>>val=new ArrayList<HashMap<String,String>>(); 
public SentTaskFragment(){}
     public View onCreateView(LayoutInflater inflater1, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater1.inflate(R.layout.listview_header_row, container, false);
        Databasehandler db=new Databasehandler(getActivity().getApplicationContext());
        final ListView l=(ListView) rootView.findViewById(R.id.listViewad);
        int count=db.getcounttask();
         Log.i("count task",""+count);
         if(count==0)
         {
        Toast.makeText(getActivity(), "No Task to Display", Toast.LENGTH_SHORT).show();
    }
        else
    {
            if(id.compareTo("empty")!=0)
            {
    val=db.getTaskSent(id);
    Toast.makeText(getActivity(), "arg2"+val, Toast.LENGTH_SHORT).show();
            }
   ListAdapter k=new SimpleAdapter(getActivity(),val,R.layout.r,new String[]{"TaskId","heading","status"},new int[]{R.id.textViews,R.id.textViews1,R.id.textstatus});

           l.setAdapter(k);
    }

1 个答案:

答案 0 :(得分:4)

在将数据插入数据库之后输入此代码

((BaseAdapter)l.getAdapter()).notifyDataSetChanged();