片段中的列表视图不起作用 [英] List View in Fragment not working
本文介绍了片段中的列表视图不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的片段代码:
public class LeftFragment extends ListFragment {
ArrayList<String> data;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.left_fragment, container, false);
data = new ArrayList<String>();
data.add("1. dell");
data.add("2. samsung");
data.add("3. apple");
data.add("4. hp");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, data);
setListAdapter(adapter);
return view;
}
这里是我的这个片段的xml:
and heres my xml for this fragment:
<?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="#FF0000" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Heres the list :"/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp" />
</LinearLayout>
和
logcat file says :Binary XML file line #5:error inflating class fragment !!
我的项目有:左右两个片段.左边的应该包含列表视图,
what my project has is: two fragments left and right. left one should contain the List View,
但是我观察到的是,当我不在 xml 中使用ListView"时,片段工作正常!但是当我使用它时.. 左片段也占用了右片段的空间.
but wat i hav observed is fragments are working fine when i Dont use "ListView" in xml !! but as i am using it.. left fragment is occupying the space of right fragment as well.
请帮忙!谢谢!
推荐答案
试试这个:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.left_fragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
ArrayList<String> data;
data = new ArrayList<String>();
data.add("1. dell");
data.add("2. samsung");
data.add("3. apple");
data.add("4. hp");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, data);
setListAdapter(adapter);
}
这篇关于片段中的列表视图不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文