我正在尝试创建一个ListFragment
. 列表中的每一行都包含一个图像和文本。很像这样。我正在遵循使用ArrayAdapter
.
但是我在ArrayAdapter 的 getView() 中得到NullPointerException @LayoutInflater inflater = fragmentContext.getLayoutInflater(null);
代码 :
数组适配器类:
public class MyListAdapter extends ArrayAdapter {
Context context;
String[] items;
Fragment fragmentContext;
public MyListAdapter(Context c, int textViewResourceId, String[] listItmes,
Fragment fragmentContext) {
super(c, textViewResourceId, listItmes);
this.items = listItmes;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = fragmentContext.getLayoutInflater(null);
view.inflate(context, R.layout.my_row, null);
}
if (items != null) {
TextView tv_my_list_item_title = (TextView) view
.findViewById(R.id.tv_my_list_item_title);
if (tv_my_list_item_title != null) {
tv_my_list_item_title.setText(items[position]);
}
}
return view;
}
}
列表片段:
public class MyFragment extends ListFragment {
int mCurCheckPosition = 0;
String[] listItems = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mylayout, null);
listItems = getResources().getStringArray(R.array.my_element_list);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Populate list with our static array of titles.
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
// android.R.layout.simple_list_item_1, listItems);
MyListAdapter adapter = new MyListAdapter(getActivity(), R.layout.my_row, listItems, this);
setListAdapter(adapter);
if (savedInstanceState != null) { // Restore last state for checked position.
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
private void showDetails(int position) {
mCurCheckPosition = position;
Toast.makeText(getActivity(), listItems[position], Toast.LENGTH_SHORT).show();
}
}
我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
我的行:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="vertical"
android:padding="6dp" >
<ImageView
android:id="@+id/iv_mehr_list_item_icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv_mehr_list_item_title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center_vertical" />
</LinearLayout>