Я пишу код для добавления ListView в фрагмент.
И данные ListView извлекаются из другого класса (с использованием ArrayList).
Но я не могу добавить ListView в свой фрагмент.Как добавить 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>
Это сообщение было опубликовано как ответ, но оно не пытается ответить на вопрос. Это должно быть редактирование, комментарий, другой вопрос или вообще исключить. –