【问题标题】:How to add a ListView to a Fragment如何将 ListView 添加到 Fragment
【发布时间】:2015-08-25 05:50:50
【问题描述】:

我正在编写将 ListView 添加到 Fragment 的代码。
并且 ListView 数据是从另一个类中检索的(通过使用 ArrayList)。
但我无法将 ListView 添加到我的片段中。

这是我的代码。
小伙伴们,你能建议我怎么做吗?

public class StartFragment extends Fragment {


    DataSource dataSource;
    ArrayList<Employee> arrayList;
    ListView listView;
    CustomAdapter adapter;
    Context context;

    public StartFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
         return inflater.inflate(R.layout.fragment_start, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView = (ListView) getActivity().findViewById(R.id.listView);
        arrayList = dataSource.getAllEmployee();
        adapter = new CustomAdapter(context,arrayList);
        listView.setAdapter(adapter);
    }
}

下面是startFragment布局代码...

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.softmaker.gazi_opu.ourpresentation.StartFragment">

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

</FrameLayout>

这是 ListView 项目的自定义布局。

<?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"
    >


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:padding="20dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Name: "
            android:id="@+id/name_textView" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Designation: "
            android:id="@+id/designationtextView" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Salary: "
            android:id="@+id/salarytextView" />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: listview android-fragments android-listview fragment android-listfragment


    【解决方案1】:

    更改您的 this 中的代码

    listView = (ListView) getActivity().findViewById(R.id.listView);
    

    到这里

    listView = (ListView) getView().findViewById(R.id.listView);
    

    【讨论】:

      【解决方案2】:

      它不起作用。在这里,我正在使用 arraylist 从数据库中检索数据。我是 Fragment 的新手。是否有任何选项可以在片段中加载 arrylist。我为片段编写了这段代码,因为当我在活动中为 listview 编写代码时,它可以正常工作。但在片段中它不起作用。

      【讨论】:

      • 这是作为答案发布的,但它不会尝试回答问题。它可能应该是编辑、评论、另一个问题或完全删除。
      猜你喜欢
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      相关资源
      最近更新 更多