2013-12-09 131 views
1

嗨,我使用視圖尋呼機指標與三個片段,並在我有一個列表視圖,顯示我的SQLite數據庫的內容,但是當我的表添加數據列表視圖doesn顯示他新數據,我在這裏添加的是我的代碼:列表視圖根本不會更新

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> 

回答

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

} 
+0

謝謝!但仍然不起作用 – Like

+0

它應該......現在正在做什麼? –

+0

我已經將所有的代碼都帶到了onViewCreated Override,但仍然沒有更新列表視圖,只顯示舊數據:( – Like

0

此時您不能調用setListAdapter,因爲視圖尚未設置。你需要等到onViewCreated

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

} 
+0

ks但仍然不工作! – Like

+0

還分享您的calery_history代碼? – vandzi

+0

我已更新問題 – Like