2012-01-11 8 views
0

を再初期化されて、私はアンドロイドで、アダプタを作成しました:私はEditView内のリストまたはアクセスにスクロールしたときにスピナーが再初期化されるようにAndroidのスピナーが時々

public class PointVerificationAdapter extends BaseAdapter { 
    List<PointVerification> mObjects; 
    Context mContext; 
    LayoutInflater mInflater; 
    Dao<ChoixPointVerification, Integer> mChoixPointVerificationDao; 
    HashMap<Integer, ReponsePointVerification> mReponses; 

public PointVerificationAdapter(Context context, 
     Dao<ChoixPointVerification, Integer> choixPointVerificationDao, 
     List<PointVerification> ListePointsVerification, 
     HashMap<Integer, ReponsePointVerification> listeReponsesPointsVerification) { 
    mInflater = LayoutInflater.from(context); 
    this.mContext = context; 
    this.mObjects = ListePointsVerification; 
    this.mChoixPointVerificationDao = choixPointVerificationDao; 
    this.mReponses = listeReponsesPointsVerification; 
} 

@Override 
public int getCount() { 
    return mObjects.size(); 
} 

@Override 
public PointVerification getItem(int position) { 
    return mObjects.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

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

    if (null == convertView) { 
     row = mInflater.inflate(R.layout.intervention_reponses_list, null); 
    } else { 
     row = convertView; 
    } 

    // affichage du nom du point de vérification 
    TextView tv = (TextView) row.findViewById(R.id.tvNom); 
    tv.setText(getItem(position).nom); 

    Integer idChoixPointVerification = null; 
    Integer idPointVerification = getItem(position).id; 
    if (mReponses != null && mReponses.containsKey(idPointVerification)) { 
     // affichage du commentaire 
     if (mReponses.get(idPointVerification).commentaire != null) 
     { 
      EditText edCommentaire = (EditText) row.findViewById(R.id.edCommentaire); 
      edCommentaire.setText(mReponses.get(idPointVerification).commentaire); 
     } 

     idChoixPointVerification = mReponses.get(idPointVerification).idChoixPointVerification; 
    } 



    // affichage de la liste déroulante 
    Spinner spi = (Spinner) row.findViewById(R.id.spiListeChoix); 
    ChoixPointVerificationDal choixPointVerificationDal = new ChoixPointVerificationDal(); 
    List<ChoixPointVerification> listeChoixPointVerification; 
    try { 
     listeChoixPointVerification = choixPointVerificationDal 
       .GetByIdPointVerification(mChoixPointVerificationDao, 
         getItem(position).id); 

     List<String> pointVerifications = new ArrayList<String>(); 
     for(ChoixPointVerification choixPointVerification: listeChoixPointVerification) 
     { 
      pointVerifications.add(choixPointVerification.nom); 
     } 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_dropdown_item, 
       pointVerifications); 

     spi.setAdapter(adapter); 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    return row; 
} 

は時々、getViewメソッドがコールバックされ、私はユーザーの選択肢を失う。これには解決策がありますか?

編集

よく私の気持ちは本当にgetViewメソッドとは非常に頻繁に呼び出されるということであり、私はそれがその機能に入りスピナー毎回REINITべきではありません。しかし、このコードの最初の実行であれば、どうすれば検出できますか?私がしたい場合は... :(

編集2

を私は、IDに位置がこの

 spi.setOnItemSelectedListener(new OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
       varPosition = position; 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parentView) { 
       // your code here 
      } 

     }); 

のようないろいろ書いて選ば店を持っていたが、私はそれは良い方法だとは思いません?。私は現時点では技術がどのようなものですOnItemClickListenerとスピナーを作成すること:

public class PointVerificationAdapter extends BaseAdapter { 
List<PointVerification> mObjects; 
Context mContext; 
LayoutInflater mInflater; 
Dao<ChoixPointVerification, Integer> mChoixPointVerificationDao; 
HashMap<Integer, ReponsePointVerification> mReponses; 
Integer mPositionSelectionne; 

public PointVerificationAdapter(
     Context context, 
     Dao<ChoixPointVerification, Integer> choixPointVerificationDao, 
     List<PointVerification> ListePointsVerification, 
     HashMap<Integer, ReponsePointVerification> listeReponsesPointsVerification) { 
    mInflater = LayoutInflater.from(context); 
    this.mContext = context; 
    this.mObjects = ListePointsVerification; 
    this.mChoixPointVerificationDao = choixPointVerificationDao; 
    this.mReponses = listeReponsesPointsVerification; 
} 

@Override 
public int getCount() { 
    return mObjects.size(); 
} 

@Override 
public PointVerification getItem(int position) { 
    return mObjects.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

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

    if (null == convertView) { 
     row = mInflater.inflate(R.layout.intervention_reponses_list, null); 
    } else { 
     row = convertView; 
    } 

    // affichage du nom du point de vérification 
    TextView tv = (TextView) row.findViewById(R.id.tvNom); 
    tv.setText(getItem(position).nom); 

    Integer idPointVerification = getItem(position).id; 
    if (mReponses != null && mReponses.containsKey(idPointVerification)) { 
     // affichage du commentaire 
     if (mReponses.get(idPointVerification).commentaire != null) { 
      EditText edCommentaire = (EditText) row 
        .findViewById(R.id.edCommentaire); 
      edCommentaire 
        .setText(mReponses.get(idPointVerification).commentaire); 
     } 

    } 

    // affichage de la liste déroulante 
    Spinner spi = (Spinner) row.findViewById(R.id.spiListeChoix); 
    spi.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      View row = mInflater.inflate(R.layout.intervention_reponses_list, null); 

      ChoixPointVerificationDal choixPointVerificationDal = new ChoixPointVerificationDal(); 
      List<ChoixPointVerification> listeChoixPointVerification; 
      try { 
       listeChoixPointVerification = choixPointVerificationDal 
         .GetByIdPointVerification(mChoixPointVerificationDao, 
           getItem(position).id); 

       ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, 
         android.R.layout.simple_spinner_dropdown_item, 
         pointVerifications); 

       spi.setAdapter(adapter); 

      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 



    return row; 

} 

エドその3

リストビュー

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

    <ListView 
     android:id="@+id/lstPointsVerification" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" > 
    </ListView> 

</LinearLayout> 

そして、リストビュー

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
    android:id="@+id/tvNom" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
android:layout_weight="1" 
    style="@style/ListePrincipal" 
    /> 

    <Spinner 
     android:id="@+id/spiListeChoix" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 


    <EditText 
    android:id="@+id/edCommentaire" 
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    style="@style/ListePrincipal" 
    /> 
</LinearLayout> 

の行ごとにリストビューを含むアクティビティのレイアウト私の目的はただverificateにポイントのリストを作成することですユーザーによって。彼は検証するために約20行を持っています。各行には、ラベル、差の選択肢を含むスピナー、およびコメントフィールドが含まれています。私は、各行で各回答を取得しなければなりません。

+0

getView()の内部にスピナーを挿入して正しいことをしていますか?あなたは、OnItemClickのようなものを望むか​​もしれないし、あなたのスピナーを再構築しますか? –

+0

もあなたはおそらく正しい、私はそれを行う方法を見ていきます... TKあなたはここでgetViewメソッド()---アダプター#のgetViewメソッドのみが呼び出されたの呼び出しの数については、別のポストからの答えは –

+0

AdapterViewビューを必要とするとき。 getViewの呼び出し頻度や回数については、何も仮定しないでください。 getViewが行う必要があるのは、必要なビューをできるだけ早く返すことだけです。ここから取得http://stackoverflow.com/questions/3287563/doubts-with-base-adapter-class-and-its-functionality。 ----なぜコードの最初の実行をチェックしたいのですか?おそらく、アダプタを作成してからitemclicksを作成した後に、あなたの欠陥についてより多くの洞察を与えるべきでしょう。 –

答えて

1

次に、リストビューのOnItemClickの例を示します。別の方法ではなく、別の場所でスピナーの作成 を移動する必要があります。むしろ別の 方法に - それは ここ

ListView mainListview = new ListView(this); 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_single_choice, new String[]{"Search","Options"}); 
      mainListview.setAdapter(adapter); /// your adapter here 
      mainListview.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
         long arg3) { 

     ///doYourSpinnerStuff(arg1) here 

        } 

          }); 

以下のコードのこの全体の作品は別の場所に移動する必要が行われているように次にによってitemClick時にそれを呼び出します。次に、スピンナーを作成して、それをクリックすると入力します。 スケッチを表示できますか?あなたのアクティビティにビューを配置する方法をスクリーンショットまたはレイアウトで表示できますか?

// affichage de la liste déroulante 
    Spinner spi = (Spinner) row.findViewById(R.id.spiListeChoix); 
    ChoixPointVerificationDal choixPointVerificationDal = new ChoixPointVerificationDal(); 
    List<ChoixPointVerification> listeChoixPointVerification; 
    try { 
     listeChoixPointVerification = choixPointVerificationDal 
       .GetByIdPointVerification(mChoixPointVerificationDao, 
         getItem(position).id); 

     List<String> pointVerifications = new ArrayList<String>(); 
     for(ChoixPointVerification choixPointVerification: listeChoixPointVerification) 
     { 
      pointVerifications.add(choixPointVerification.nom); 
     } 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_dropdown_item, 
       pointVerifications); 

     spi.setAdapter(adapter); 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
+0

ユーザーがlistviewのスピナーをクリックすると、listviewのonitemclickが呼び出されますか?むしろ複雑に聞こえる。 –

+0

Noo。あなたのPointVerificationAdapterはリストビューまたは別のスピナーのために作成されますか?したがって、基本的に、このリストビューまたはスピンナーのいずれかを作成すると、それらのOnItemClickを設定し、そのクリックに基づいて追加のスピナーを作成します。私はあなたが正しいと思います... –

+0

PointVerificationAdapterはSpinner用です。私はListViewのための別のアダプタがあります。私は少し失われている、あなたはどのようにonitemclickにスピナーを作成しますか?質問を編集して自分の仕事の状態を再確認します。本当に助けてくれてありがとう。 –