列表视图根本不更新

时间:2013-12-09 15:33:28

标签: android sqlite listview

您好我正在使用带有三个片段的查看寻呼机指示器,其中一个我有一个列表视图,显示我的SQLite数据库的内容但是当我向我的表添加数据时,列表视图没有显示他的新数据我在这里添加了我的代码:

public class caleryhistory extends SherlockListFragment {
    List<calery_lagari> Calery_lagari;
    calery_lagari_SQLiteData data;
    ArrayAdapter<calery_lagari> adapter;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.calery_history, null);


        data = new calery_lagari_SQLiteData(getActivity());
        data.open();
        Calery_lagari = data.findall();
        adapter = new ArrayAdapter<calery_lagari>(getActivity(),
                R.layout.listback_layout, Calery_lagari);
        adapter.notifyDataSetChanged();

        setListAdapter(adapter);

        adapter.notifyDataSetChanged();

        return v;

    }

所以有什么帮助吗? 提前致谢! :)

更新

import java.util.List;

import mr.chag.va.lagar.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockListFragment;


public class caleryhistory extends SherlockListFragment {
    List<calery_lagari> Calery_lagari;
    calery_lagari_SQLiteData data;
    ArrayAdapter<calery_lagari> adapter;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.calery_history, null);




        return v;

    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        data = new calery_lagari_SQLiteData(getActivity());
        data.open();
        Calery_lagari = data.findall();
        adapter = new ArrayAdapter<calery_lagari>(getActivity(),
                R.layout.listback_layout, Calery_lagari);
        adapter.notifyDataSetChanged();
         ListView listView = (ListView)view.findViewById(android.R.id.list);
         listView.setAdapter(adapter);


        adapter.notifyDataSetChanged();
        super.onViewCreated(view, savedInstanceState);
    }

}

我的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="clear"
        android:text="clear all" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

3 个答案:

答案 0 :(得分:2)

扩展SherlockListFragments时的最佳方法是让onCreateView返回膨胀的视图,然后覆盖onViewCreated。并将您的代码更改为您的视图。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b){
     return inflater.inflate(R.layout.calery_history, null);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) { 
     data = new calery_lagari_SQLiteData(getActivity());
     data.open();
     Calery_lagari = data.findall();
     adapter = new ArrayAdapter<calery_lagari>(getActivity(), view.R.layout.listback_layout, Calery_lagari);
     adapter.notifyDataSetChanged();

     setListAdapter(adapter);

     adapter.notifyDataSetChanged();

}

答案 1 :(得分:1)

第一个问题是您在onCreateView中设置了适配器。在onCreateView列表视图中尚未创建。其次,您需要设置适配器以更正列表视图。因为您覆盖oncreateview,所以不能使用setListAdapter,因为它指的是默认viet中的列表视图。试试这个:

       @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.calery_history, null);
}

    @Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    data = new calery_lagari_SQLiteData(getActivity());
    data.open();
    Calery_lagari = data.findall();
    adapter = new ArrayAdapter<calery_lagari>(getActivity(),
            view.R.layout.listback_layout, Calery_lagari);

    ListView listView = (ListView)view.findViewById(R.id.YOUR_LISTVIEW_ID);
    listView.setAdapter(adapter);

    adapter.notifyDataSetChanged();

}

答案 2 :(得分:0)

此时您无法呼叫setListAdapter,因为视图尚未设置。

您需要等到onViewCreated