我想实现一个包含 Spinner 的 ListView 已经有几天了。我对android真的很陌生,所以从一个论坛到另一个论坛,我设法使一些工作。我唯一的问题是在我的真实设备上向下滚动时微调器,它们的值丢失或行为怪异(有时它们被重置,有时最后一个元素取第一个元素的值..等等)当我读到我发现,android 回收视图,所以我正在尝试使用这个概念。您能否告诉我正确的方法,或者告诉我我做错了什么。我得到的错误如下: 04-27 19:43:09.649: E/AndroidRuntime(7530): java.lang.NullPointerException 04-27 19:43:09.649: E/AndroidRuntime(7530): at com.example。 mysqlconnection.MyCustomeArrayAdapter.getView(MyCustomeArrayAdapter.java:106)
其中第 106 行对应于: holder.Spin.setSelection((Integer) (holder.Spin.getTag()));
非常感谢。
list_item.xml android:orientation="horizontal" >
<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="@+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:textIsSelectable="true"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17sp"
android:textStyle="bold"/>
<Spinner
android:id="@+id/presence"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/presence_list"
android:prompt="@string/presence_prompt"/>
公共类 MyCustomeArrayAdapter 扩展 ArrayAdapter> {
private static final String TAG_PID = "EnfantId";
private static final String TAG_NOM = "Prenom";
HashMap<String, String> hm = new HashMap<String, String>();
SpinnerContent data[] = null;
int[] anArray;
Context context;
int layoutResourceId;
ArrayList<HashMap<String, String>> localList;
LayoutInflater inflater;
public MyCustomeArrayAdapter(Context pContext, int layoutResourceId, ArrayList<HashMap<String, String>> list) {
super(pContext, layoutResourceId, list);
this.context = pContext;
this.layoutResourceId = layoutResourceId;
this.localList = new ArrayList<HashMap<String, String>>();
this.localList.addAll(list);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
data = new SpinnerContent[20];
anArray = new int[20];
}
@Override public View getView(final int position, View convertView, ViewGroup parent) {
View row = null;
final SpinnerHolder holder;
if(convertView == null)
{
row = inflater.inflate(layoutResourceId, null);
holder = new SpinnerHolder();
holder.name = (TextView)row.findViewById(R.id.name);
holder.pid = (TextView)row.findViewById(R.id.pid);
holder.Spin = (Spinner)row.findViewById(R.id.presence);
holder.Spin.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int SpinPosition, long id) {
holder.Spin.setTag(SpinPosition);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
row.setTag(holder);
}
else
{
row = convertView;
holder = (SpinnerHolder)row.getTag();
}
hm = localList.get(position);
holder.pid.setText(hm.get(TAG_PID));
holder.name.setText(hm.get(TAG_NOM));
holder.Spin.setSelection((Integer) (holder.Spin.getTag()));
return row;
}
private class SpinnerHolder
{
TextView name;
TextView pid;
Spinner Spin;
}
}