在片段中为 ListView 设置适配器
Set Adapter In Fragment for ListView
我是 Android 的新手。试图将以前在我的 Main Activity 中控制的列表视图获取到一个片段。
我的 MainActivity
扩展了 FragmentActivity
。我得到一个 null 指针,用于设置适配器和包含 populateList()
方法的行。
不确定解决方案是否与 getActivity() and/or 我一直在阅读的 setListAdapter()
选项有关。我知道它正在从保存中查找项目,但我不确定如何解决此问题。任何见解将不胜感激。
附加信息:
我正在使用 android.support.v4.app.FragmentActivity
。 FragmentS
在 public class TabbedActivity extends Fragment {
之内
public static class FragmentS extends Fragment {
public FragmentS() {
}
List<LiftSave> LiftSaves = new ArrayList<LiftSave>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listview_s,
container, false);
DatabaseHandler dbHandler;
dbHandler = new DatabaseHandler (getActivity().getApplicationContext());
if (dbHandler.getLiftSavesCount() != 0)
LiftSaves.addAll(dbHandler.getAllLiftSaves());
populateList();
return rootView;
}
private void populateList() {
ListView saveListView = (ListView)getActivity().findViewById(R.id.saveListView);
ArrayAdapter<LiftSave> saveAdapter;
saveAdapter = new SaveListAdapter();
saveListView.setAdapter(saveAdapter);
}
public class SaveListAdapter extends ArrayAdapter<LiftSave> {
public SaveListAdapter() {
super(getActivity(), R.layout.listview_item, LiftSaves);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getActivity().getLayoutInflater().inflate(R.layout.listview_item, parent, false);
LiftSave currentLiftSave = LiftSaves.get(position);
TextView liftName = (TextView) view.findViewById(R.id.liftName);
liftName.setText(currentLiftSave.getLiftName());
TextView maxValue = (TextView) view.findViewById(R.id.maxValue);
maxValue.setText(currentLiftSave.getMaxValue());
TextView weightAndReps = (TextView) view.findViewById(R.id.weightAndReps);
weightAndReps.setText(currentLiftSave.getRepsAndWeight());
TextView liftNotes = (TextView) view.findViewById(R.id.liftNotes);
liftNotes.setText(currentLiftSave.getLiftNotes());
TextView date = (TextView) view.findViewById(R.id.todayDate);
date.setText(currentLiftSave.getTodayDate());
return view;
}
}}
XML:
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TabbedActivity$FragmentS"
android:background="@android:color/holo_blue_dark">
<LinearLayout
android:id="@+id/tabSaveList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Saved Maxes"
android:id="@+id/textView"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textColor="#fffaf4a1"
android:textStyle="bold" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/saveListView" />
</LinearLayout>
</RelativeLayout>
我认为问题在于,您在 Activity 中搜索 ListView 而不是在 onCreateView()
中检索到的 rootView
也许试试这个:
public static class FragmentS extends Fragment {
private ListView saveListView;
private List<LiftSave> LiftSaves = new ArrayList<LiftSave>();
public FragmentS() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listview_s,
container, false);
saveListView = (ListView) rootView.findViewById(R.id.saveListView);
DatabaseHandler dbHandler;
dbHandler = new DatabaseHandler (getActivity().getApplicationContext());
if (dbHandler.getLiftSavesCount() != 0)
LiftSaves.addAll(dbHandler.getAllLiftSaves());
populateList();
return rootView;
}
private void populateList() {
ArrayAdapter<LiftSave> saveAdapter = new SaveListAdapter();
saveListView.setAdapter(saveAdapter);
}
...
我是 Android 的新手。试图将以前在我的 Main Activity 中控制的列表视图获取到一个片段。
我的 MainActivity
扩展了 FragmentActivity
。我得到一个 null 指针,用于设置适配器和包含 populateList()
方法的行。
不确定解决方案是否与 getActivity() and/or 我一直在阅读的 setListAdapter()
选项有关。我知道它正在从保存中查找项目,但我不确定如何解决此问题。任何见解将不胜感激。
附加信息:
我正在使用 android.support.v4.app.FragmentActivity
。 FragmentS
在 public class TabbedActivity extends Fragment {
public static class FragmentS extends Fragment {
public FragmentS() {
}
List<LiftSave> LiftSaves = new ArrayList<LiftSave>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listview_s,
container, false);
DatabaseHandler dbHandler;
dbHandler = new DatabaseHandler (getActivity().getApplicationContext());
if (dbHandler.getLiftSavesCount() != 0)
LiftSaves.addAll(dbHandler.getAllLiftSaves());
populateList();
return rootView;
}
private void populateList() {
ListView saveListView = (ListView)getActivity().findViewById(R.id.saveListView);
ArrayAdapter<LiftSave> saveAdapter;
saveAdapter = new SaveListAdapter();
saveListView.setAdapter(saveAdapter);
}
public class SaveListAdapter extends ArrayAdapter<LiftSave> {
public SaveListAdapter() {
super(getActivity(), R.layout.listview_item, LiftSaves);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getActivity().getLayoutInflater().inflate(R.layout.listview_item, parent, false);
LiftSave currentLiftSave = LiftSaves.get(position);
TextView liftName = (TextView) view.findViewById(R.id.liftName);
liftName.setText(currentLiftSave.getLiftName());
TextView maxValue = (TextView) view.findViewById(R.id.maxValue);
maxValue.setText(currentLiftSave.getMaxValue());
TextView weightAndReps = (TextView) view.findViewById(R.id.weightAndReps);
weightAndReps.setText(currentLiftSave.getRepsAndWeight());
TextView liftNotes = (TextView) view.findViewById(R.id.liftNotes);
liftNotes.setText(currentLiftSave.getLiftNotes());
TextView date = (TextView) view.findViewById(R.id.todayDate);
date.setText(currentLiftSave.getTodayDate());
return view;
}
}}
XML:
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TabbedActivity$FragmentS"
android:background="@android:color/holo_blue_dark">
<LinearLayout
android:id="@+id/tabSaveList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Saved Maxes"
android:id="@+id/textView"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textColor="#fffaf4a1"
android:textStyle="bold" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/saveListView" />
</LinearLayout>
</RelativeLayout>
我认为问题在于,您在 Activity 中搜索 ListView 而不是在 onCreateView()
中检索到的 rootView也许试试这个:
public static class FragmentS extends Fragment {
private ListView saveListView;
private List<LiftSave> LiftSaves = new ArrayList<LiftSave>();
public FragmentS() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listview_s,
container, false);
saveListView = (ListView) rootView.findViewById(R.id.saveListView);
DatabaseHandler dbHandler;
dbHandler = new DatabaseHandler (getActivity().getApplicationContext());
if (dbHandler.getLiftSavesCount() != 0)
LiftSaves.addAll(dbHandler.getAllLiftSaves());
populateList();
return rootView;
}
private void populateList() {
ArrayAdapter<LiftSave> saveAdapter = new SaveListAdapter();
saveListView.setAdapter(saveAdapter);
}
...