0
我創建一個客戶CompulsoryFragmentAdapter擴展BaseAdapter,但我發現getview總是被調用,我不知道爲什麼?getView()方法在適配器中調用mulite時間
的getview代碼:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LogUtils.i("CompulsoryFragmentAdapter", "position->"+position);
// TODO Auto-generated method stub
viewHolder holder=null;
if(convertView==null){
holder=new viewHolder();
convertView=View.inflate(mSoftContext.get(), R.layout.course_list_compulsory_item, null);
holder.item_ImgSrc=(ImageView) convertView.findViewById(R.id.compulsory_item_icon);
holder.item_Title=(TextView) convertView.findViewById(R.id.compulsory_item_title);
holder.item_startTime=(TextView) convertView.findViewById(R.id.compulsory_item_startTime);
holder.item_endTime=(TextView) convertView.findViewById(R.id.compulsory_item_stopTime);
holder.item_residueTime=(TextView) convertView.findViewById(R.id.compulsory_item_residueTime);
holder.item_StudyProgress=(TextView) convertView.findViewById(R.id.compulsory_item_studyprogress);
holder.item_progressBar=(ProgressBar) convertView.findViewById(R.id.compulsory_item_progressbar);
convertView.setTag(holder);
}else{
holder=(viewHolder) convertView.getTag();
}
CompulsoryContentItemEntity item=indexItems.get(position);
int progress=(int)(CommonUtil.floatRound(item.getProgress(), 2, BigDecimal.ROUND_DOWN)*100);
holder.item_Title.setText(item.getName());
holder.item_startTime.setText(DateUtils.datetoStr(item.getStartTime()));
holder.item_endTime.setText(DateUtils.datetoStr(item.getEndTime()));
holder.item_residueTime.setText(String.format("剩%s天", item.getIntervalDays()));
holder.item_progressBar.setProgress(progress);
holder.item_StudyProgress.setText(progress+"%");
if(item.getImage().equals("")||item.getImage()==null){
holder.item_ImgSrc.setImageResource(R.drawable.compulsory_item_img);
}else{
}
return convertView;
}
private class viewHolder{
ImageView item_ImgSrc;
TextView item_StudyProgress;
TextView item_Title;
TextView item_startTime;
TextView item_endTime;
TextView item_residueTime;
ProgressBar item_progressBar;
}
listview.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/compulsory_bg"
android:orientation="vertical" >
<com.eceibs.xlistview.XListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
/>
</LinearLayout>
項目XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/compulsory_bg"
android:paddingBottom="4dp"
android:paddingTop="4dp" >
但結果是:
position->0
position->1
position->2
position->0
position->1
position->2
position->0
position->1
position->2
position->0
position->1
position->2
...
我沒有碰ListView
或其他地方,爲什麼它顯示的結果是這樣?
是的,它會調用多次顯示listview的項目.. – 2014-09-30 04:56:27
可能重複的http://stackoverflow.com/questions/18376708/getview-called-multiple-times-in-android – yashhy 2014-09-30 04:59:09
你可以發佈你的適配器其餘部分。 – k0sh 2014-09-30 05:02:08