嗨,我想为附加到 CustomBaseAdapter 的列表视图的每一行设置不同的图像。我已经有一些代码,但我在适配器的第 49 行使用 NullPointer 强制关闭。
convertView = mInflater.inflate(R.layout.test, null);
有人可以帮忙解决这个问题吗?
先感谢您。
代码主要活动:
private static int icons[] = {R.drawable.ic_dialog_time, R.drawable.eur};
...
ArrayList<SearchResults> searchResults = GetSearchResults();
kp.setAdapter(new MyCustomBaseAdapter(AndroidLogin.this, searchResults, icons));
解决方案代码CustomBaseAdapter:
public class MyCustomBaseAdapter extends BaseAdapter {
public int images[];
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public static class ViewHolder {
ImageView imgViewLogo;
TextView txtNaam;
TextView txtWaarde;
}
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results, int[] images) {
super();
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.images = images;
searchArrayList = results;
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.test, null);
holder.imgViewLogo =(ImageView)convertView.findViewById(R.id.imgIcon);
holder.txtNaam = (TextView) convertView.findViewById(R.id.naam);
holder.txtWaarde = (TextView) convertView.findViewById(R.id.waarde);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.imgViewLogo.setImageResource(images[position%2]);
holder.txtNaam.setText(searchArrayList.get(position).getNaam());
holder.txtWaarde.setText(searchArrayList.get(position).getWaarde());
return convertView;
}
}
编辑 Logcat:
11-19 17:27:06.894: E/AndroidRuntime(1103): FATAL EXCEPTION: main
11-19 17:27:06.894: E/AndroidRuntime(1103): java.lang.ArrayIndexOutOfBoundsException
11-19 17:27:06.894: E/AndroidRuntime(1103): at com.sencide.MyCustomBaseAdapter.getView(MyCustomBaseAdapter.java:63)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.AbsListView.obtainView(AbsListView.java:1409)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.ListView.onMeasure(ListView.java:1127)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.View.measure(View.java:8313)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.TableRow.getColumnsWidths(TableRow.java:308)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.TableLayout.findLargestCells(TableLayout.java:500)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.TableLayout.measureVertical(TableLayout.java:465)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.TableLayout.onMeasure(TableLayout.java:431)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.View.measure(View.java:8313)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.LinearLayout.onMeasure (LinearLayout.java:309)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.View.measure(View.java:8313)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.View.measure(View.java:8313)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.View.measure(View.java:8313)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.os.Looper.loop(Looper.java:123)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-19 17:27:06.894: E/AndroidRuntime(1103): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 17:27:06.894: E/AndroidRuntime(1103): at java.lang.reflect.Method.invoke(Method.java:507)
11-19 17:27:06.894: E/AndroidRuntime(1103): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-19 17:27:06.894: E/AndroidRuntime(1103): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-19 17:27:06.894: E/AndroidRuntime(1103): at dalvik.system.NativeStart.main(Native Method)
列表.xml:
<ImageView
android:id="@+id/imgIcon"
android:icon="@+drawable/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />