直接贴代码:
activity_main.xml
<?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"
android:padding="10dp">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
item.xml
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/iv"
android:text="商品" />
</RelativeLayout>
</LinearLayout>
这里注意:item.xml文件中根布局不能写为RelativeLayout,否则会报空指针异常
Commodity.java
public class Commodity {
private int icon;
private String name;
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Spinner spinner;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
List<Commodity> list = new ArrayList<>();
for (int i = 1; i <= 30; i ++) {
Commodity commodity = new Commodity();
commodity.setIcon(R.mipmap.ic_launcher);
commodity.setName("商品 " + i);
list.add(commodity);
}
adapter = new MyAdapter(this, list);
spinner.setAdapter(adapter);
}
private class MyAdapter extends BaseAdapter {
private Context mContext;
private List<Commodity> mList;
public MyAdapter(Context context, List<Commodity> list) {
this.mContext = context;
this.mList = list;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.item, null);
holder = new ViewHolder();
holder.iv = (ImageView) convertView.findViewById(R.id.iv);
holder.tv = (TextView) convertView.findViewById(R.id.tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.iv.setImageResource(mList.get(position).getIcon());
holder.tv.setText(mList.get(position).getName());
return convertView;
}
}
static class ViewHolder {
private ImageView iv;
private TextView tv;
}
}
将item.xml文件修改为如下代码运行一下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/iv"
android:text="商品" />
</RelativeLayout>
异常信息如下:
03-17 12:37:36.719 6241-6241/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:559)
at android.view.View.measure(View.java:15518)
at android.support.v7.internal.widget.ListViewCompat.measureHeightOfChildrenCompat(ListViewCompat.java:301)
at android.support.v7.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1200)
at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:584)
at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:766)
at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:424)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
03-17 12:37:40.603 410-837/? E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
03-17 12:37:40.603 410-837/? E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!