我創建的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條線路進行驗證。每行包含一個標籤,一個帶有不同選擇的微調框和一個註釋欄。然後我必須檢索每一行的每個答案。
你確定你正在做一個正確的事情,把一個微調內的getView()?可能你想要像OnItemClick這樣的東西,然後重建你的微調? – 2012-01-11 10:48:55
好吧,你可能是正確的,我會看看如何做到這一點...... tk你 – 2012-01-11 10:55:52
這是另一篇文章關於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