2013-01-02 110 views
0

告訴我我做錯了什麼?Android - 片段中的ListView不顯示

我正在填寫表格。 但是數據不會出現在ListView中。

例如,在活動中它正在工作。 這是這個代碼沒有嘗試過,但從它的工作完美複製。

數據準確,檢查調試器。

public class TabFilter extends Fragment { 

    private static String TAB_FILTER_LOG = "TabFilter: "; 
    private ArrayList<HashMap<String, Object>> filterListData; 
    private ListView filterListView; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     setHasOptionsMenu(true); 

     View view = inflater.inflate(R.layout.tab_filter, container, false); 

     filterListView = (ListView) view.findViewById(R.id.filtersListView); 
     filterListData = new ArrayList<HashMap<String, Object>>(); 

     fillListView(); 

     SimpleAdapter filtersListAdapter = new SimpleAdapter(getActivity().getBaseContext(), filterListData, R.layout.filters_list_view_row, new String[] { "name", 
       "nummer" }, new int[] { R.id.filterNameTv, R.id.filterInfoTv }); 

     filterListView.setAdapter(filtersListAdapter); 
     filterListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

     // filterListView.setOnItemClickListener(this); 

     if (container == null) { 
      return null; 
     } 
     return (LinearLayout) inflater.inflate(R.layout.tab_filter, container, false); 
    } 

    private HashMap<String, Object> fillListView() { 
     HashMap<String, Object> fillMap = null; 

     SQLiteDatabase db = DatabaseHelper.getInstance(getActivity().getBaseContext()).getWritableDatabase(); 
     Cursor cursor = db.query("filters", null, null, null, null, null, null); 

     if (cursor.moveToFirst()) { 

      int nameColIndex = cursor.getColumnIndex("name"); 
      int nummerColIndex = cursor.getColumnIndex("nummer"); 

      do { 
       fillMap = new HashMap<String, Object>(); 

       fillMap.put("name", cursor.getString(nameColIndex)); 
       fillMap.put("nummer", cursor.getString(nummerColIndex)); 
       filterListData.add(fillMap); 

      } while (cursor.moveToNext()); 

      cursor.close(); 
      db.close(); 
     } else { 
      Toast.makeText(getActivity().getBaseContext(), "0 ROWS: ", Toast.LENGTH_LONG).show(); 
     } 
     return fillMap; 
    } 
} 

tab_filter.xm

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

    <ListView 
     android:id="@+id/filtersListView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 

filters_list_view_row.xml

<?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" 
    android:background="@color/BlueViolet"> 

    <TextView 
     android:id="@+id/filterNameTv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:background="@color/LightGrey"/> 

    <TextView 
     android:id="@+id/filterInfoTv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/LightSkyBlue" /> 

</LinearLayout> 

回答

4

您需要在onCreateView返回視圖對象()。請檢查您的onCreateView()在第一行創建View對象的位置,並創建與尊重第一個視圖Object的listview對象。最後,你正在膨脹另一個LinearLayout並返回它。所以你得到空白屏幕。

更換你的下面的代碼:

if(container == null) { 
    return null; 
} 
return (LinearLayout)inflater.inflate(R.layout.tab_filter, container, false); 

與此:

return view; 
+0

的感謝!有用! )) – JDev

2

您充氣另一個LinearLayout中並將其返回。那麼,你是漸漸空虛屏幕 使用這種方法,而不是上面

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    setHasOptionsMenu(true); 

    View view = inflater.inflate(R.layout.tab_filter, container, false); 

    filterListView = (ListView) view.findViewById(R.id.filtersListView); 
    filterListData = new ArrayList<HashMap<String, Object>>(); 

    fillListView(); 

    SimpleAdapter filtersListAdapter = new SimpleAdapter(getActivity().getBaseContext(), filterListData, R.layout.filters_list_view_row, new String[] { "name", 
       "nummer" }, new int[] { R.id.filterNameTv, R.id.filterInfoTv }); 

    filterListView.setAdapter(filtersListAdapter); 
    filterListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

    // filterListView.setOnItemClickListener(this); 

    if(container == null) { 
     return null; 
    } 
    return view; 
} 
+0

謝謝,它工作! – JDev

0

我有碎片的等效問題不會出現。 而一個問題是XML語句的順序,所以方向前應「佈局」的出現:

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

    android:background="@color/BlueViolet"> 
...