Android的 - 在的ListView片段不显示 [英] Android - ListView in Fragment does not show
问题描述
告诉我,我做错了什么?
我试图填补了表。
但数据中不出现的ListView
例如,在活动其工作正常。
正是这种code没有尝试过,但是从它的工作完美复制。
中的数据是准确的,检查调试器。
公共类TabFilter扩展片段{ 私人静态字符串TAB_FILTER_LOG =TabFilter:
私人的ArrayList<&HashMap的LT;弦乐,对象>> filterListData;
私人的ListView filterListView; 公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
setHasOptionsMenu(真); 查看查看= inflater.inflate(R.layout.tab_filter,集装箱,FALSE); filterListView =(ListView控件)view.findViewById(R.id.filtersListView);
filterListData =新的ArrayList<&HashMap的LT;弦乐,对象>>(); fillListView(); SimpleAdapter filtersListAdapter =新SimpleAdapter(getActivity()。getBaseContext(),filterListData,R.layout.filters_list_view_row,新的String [] {名,
nummer},新的INT [] {R.id.filterNameTv,R.id.filterInfoTv}); filterListView.setAdapter(filtersListAdapter);
filterListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); // filterListView.setOnItemClickListener(本); 如果(集装箱== NULL){
返回null;
}
返回(的LinearLayout)inflater.inflate(R.layout.tab_filter,集装箱,FALSE);
} 私人的HashMap<弦乐,对象> fillListView(){
HashMap的<弦乐,对象> fillMap = NULL; SQLiteDatabase分贝= DatabaseHelper.getInstance(getActivity()getBaseContext())getWritableDatabase();
光标光标= db.query(过滤器,NULL,NULL,NULL,NULL,NULL,NULL); 如果(cursor.moveToFirst()){ INT nameColIndex = cursor.getColumnIndex(名称);
INT nummerColIndex = cursor.getColumnIndex(nummer); 做{
fillMap =新的HashMap<弦乐,对象>(); fillMap.put(名,cursor.getString(nameColIndex));
fillMap.put(nummer,cursor.getString(nummerColIndex));
filterListData.add(fillMap); }而(cursor.moveToNext()); cursor.close();
db.close();
}其他{
Toast.makeText(getActivity()getBaseContext(),0行,Toast.LENGTH_LONG).show();
}
返回fillMap;
}
}
tab_filter.xm
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT> < ListView控件
机器人:ID =@ + ID / filtersListView
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
< /&的ListView GT;< / LinearLayout中>
filters_list_view_row.xml
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:背景=@色/蓝紫> <的TextView
机器人:ID =@ + ID / filterNameTv
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:比重=中心
机器人:背景=@色/浅灰/> <的TextView
机器人:ID =@ + ID / filterInfoTv
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:背景=@彩色/天蓝/>< / LinearLayout中>
您需要在onCreateView返回视图对象()。请检查您的onCreateView()要在其中创建的第一线视图对象和推崇的第一个视图对象创建的ListView对象。最后,你正在膨胀的另一个LinearLayout中并将其返回。那么,你是渐渐空虚屏幕。
替换您的低于code:
如果(集装箱== NULL){
返回null;
}
返回(的LinearLayout)inflater.inflate(R.layout.tab_filter,集装箱,FALSE);
本:
返回查看;
Tell me what I did wrong?
I'm trying to fill the sheet. But the data does not appear in ListView.
For example, in Activity it is working. It is this code not tried it, but copied from where it worked perfectly.
The data is accurate, checked debugger.
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>
You need to return the view object in onCreateView(). Please check your onCreateView() where you are creating View object in the first line and creating listview object with respected to the first view Object. Finally you are inflating another LinearLayout and returning it. So you are getting empty screen.
Replace the below code of your:
if(container == null) {
return null;
}
return (LinearLayout)inflater.inflate(R.layout.tab_filter, container, false);
with this:
return view;
这篇关于Android的 - 在的ListView片段不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!