0

スピナーを含む ListView を実装したいと思って数日が経ちました。私は本当にAndroidに慣れていないので、フォーラムからフォーラムに移動して、何かを機能させることができました. 私が抱えている唯一の問題は、実際のデバイスを下にスクロールするときのスピナーです。それらの値が失われたり、奇妙な動作をしたりします(リセットされる場合もあれば、最後の要素が最初の要素の値を取る場合もあります..など) Androidはビューをリサイクルすることがわかったので、この概念を使用しようとしています。正しいアプローチを教えてください。または、私が行っている間違ったことを教えてください。私が得るエラーは次のとおりです。 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"/> 

public class MyCustomeArrayAdapter extends 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

Web を検索して、最終的に解決策を見つけました。次のように、コンストラクターでハッシュマップを宣言しました。

次に、 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 に答える