0

我想实现一个包含 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="horizo​​ntal" >

<!-- 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;
}

}

4

2 回答 2

0

终于通过网上搜索找到了解决方法:我在构造函数中声明了一个hashmap如下: HashMap(Integer, Integer> selectedItems = new HashMap(Integer, Integer>();

然后我改变了我的 getView() 方法如下:

@Override public View getView(final int position, View convertView, ViewGroup parent) {

   //  View row = super.getView(position, convertView, parent);
   View row = inflater.inflate(layoutResourceId, null);   
   hm = localList.get(position);
   ((TextView)row.findViewById(R.id.name)).setText(hm.get(TAG_NOM));
   ((TextView)row.findViewById(R.id.pid)).setText(hm.get(TAG_PID));
   if (selectedItems.get(position) != null)
   {
    ((Spinner)row.findViewById(R.id.presence)).setSelection(selectedItems.get(position));
   }

   ((Spinner)row.findViewById(R.id.presence)).setOnItemSelectedListener(new OnItemSelectedListener(){
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int SpinPosition, long id) {           
        selectedItems.put(position, SpinPosition);          
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }   
});

   return row;
}
于 2013-04-30T00:42:09.750 回答
0

自旋标签没有默认值。您可以将视图保存在地图中,并为每个位置创建唯一视图,而不是回收其他视图。

     //member vairous
     Map mViews = new HashMap<Integer, View>(); 

     @Override public View getView(final int position, View convertView, ViewGroup parent){
         View view = mViews.get(position);

         if(view == null)
            {                   
                view = inflater.inflate(layoutResourceId, null);

                holder = new SpinnerHolder();

                holder.name = (TextView)view.findViewById(R.id.name);
                holder.pid = (TextView)view.findViewById(R.id.pid);
                holder.Spin = (Spinner)view.findViewById(R.id.presence);
                //set the default value.
                holder.Spin.setTag(0);
                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
                    }   
                });
                view.setTag(holder);
                mViwes.put(position, view);
            }
            else
            {
                holder = (SpinnerHolder)view.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 view;
       }
于 2013-04-28T01:14:43.747 回答