2012-01-11 13 views
0

我創建的Android適配器:Android的微調在ListView有時重新初始化

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

有時候當我在列表或EditView中訪問滾動時,GetView叫回來,微調將被重新初始化我失去了用戶的選擇。有沒有解決方案?

編輯

以及我的感覺是GetView和真的叫很多時候,我不應該重新初始化微調每次它進入該功能。但是,我如何檢測它是否是此代碼的第一次運行?我有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 
      } 

     }); 

的位置,但我不認爲這是個好辦法... :(

編輯2

如果我想創建具有OnItemClickListener一個微調是什麼TECHNIC目前,我這樣做:?

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> 

而且對於ListView

<?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> 

我的目標的每一行ListView控件的佈局只是爲了創造點的列表verificate由用戶。他將有大約20條線路進行驗證。每行包含一個標籤,一個帶有不同選擇的微調框和一個註釋欄。然後我必須檢索每一行的每個答案。

+0

你確定你正在做一個正確的事情,把一個微調內的getView()?可能你想要像OnItemClick這樣的東西,然後重建你的微調? – 2012-01-11 10:48:55

+0

好吧,你可能是正確的,我會看看如何做到這一點...... tk你 – 2012-01-11 10:55:52

+0

這是另一篇文章關於getView()的調用次數的答案---只有當AdapterView需要視圖時才調用Adapter#getView 。您不應該對getView被調用的次數或次數做出任何假設。 getView應該做的唯一事情就是儘快返回所需的視圖。採取從這裏http://stackoverflow.com/questions/3287563/doubts-with-base-adapter-class-and-its-functionality。 ----爲什麼要檢查代碼的第一次運行?也許你應該在創建適配器後再對你的缺陷給予更多的瞭解,然後itemclicks .... – 2012-01-11 10:58:05

回答

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的onitemclick將被稱爲?這聽起來很複雜 – 2012-01-11 11:23:12

+0

Noo。您的PointVerificationAdapter是爲listview還是另一個微調器創建的嗎?所以基本上,當這個listview或微調創建的任何一個你爲他們設置OnItemClick和基於該點擊你創建你的額外微調。我希望我能幫你... – 2012-01-11 11:26:22

+0

PointVerificationAdapter適用於Spinner。我有另一個適配器的ListView。我有點失落,你是如何將微調創建到onitemclick的?我將編輯這個問題來重申我的工作狀態。感謝你的幫助,真的。 – 2012-01-11 13:31:47