为什么ListView重复每个第六项?

我有一个ListView由自定义适配器与特定布局填充。 适配器映射到HashMap,其中包含每个ListView元素的数据的特定元素。

哈希映射中的数据是正确的,但是ListView重复再次每6'次绘制相同的第6个元素,直到它到达Map的末尾?

我的显示器允许显示5个项目,如果你发送一个白色的6个项目。

这里是适配器的代码,ListActivity的相关代码和ListView的布局文件。

请帮助,我不知道为什么会发生这种情况。

package de.View; import java.util.ArrayList; import java.util.Map; import de.carSync.R; import de.Common.Date_Conversion; import de.Common.GUI_Output; import de.Model.DriversLog.Fahrt; import de.Model.DriversLog.Geladene_Fahrten; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class Fahrten_List_Adapter extends BaseAdapter{ private static String TAG = "Fahrten_List_Adapter"; private Map<Integer,Fahrt> fahrten_Liste; private final LayoutInflater mLayoutInflater; int zeilen_Layout; public Fahrten_List_Adapter(Context ctx, Map<Integer,Fahrt> f_l, int zeilen_Layout){ this.zeilen_Layout = zeilen_Layout; fahrten_Liste = f_l; mLayoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return fahrten_Liste.size(); } @Override public Object getItem(int pos) { return fahrten_Liste.get(pos); } @Override public long getItemId(int pos) { return 0; } public void clear(){ fahrten_Liste.clear(); } @Override public View getView(int pos, View convertView, ViewGroup parent) { if(convertView == null){ convertView = mLayoutInflater.inflate(zeilen_Layout, null); Log.v(TAG,"getView :" + pos); ((TextView) convertView.findViewById(R.id.FahrtID)).setText( (String) ((Fahrt) getItem(pos)).get_Fahrt_ID() ); ((TextView) convertView.findViewById(R.id.Kennzeichen)).setText( (String) ((Fahrt) getItem(pos)).get_Kennzeichen() ); ((TextView) convertView.findViewById(R.id.Abfahrts_Strasse)).setText( (String) ((Fahrt) getItem(pos)).get_Abfahrts_Strasse().toString() ); ((TextView) convertView.findViewById(R.id.Ankunfts_Strasse)).setText( (String) ((Fahrt) getItem(pos)).get_Ankunfts_Strasse().toString() ); ((TextView) convertView.findViewById(R.id.Fahrttyp)).setText( (String) GUI_Output.get_Fahrttyp((Object) ((Fahrt) getItem(pos)).get_Fahrttyp() )); ((TextView) convertView.findViewById(R.id.Abfahrts_Stadt)).setText( (String) ((Fahrt) getItem(pos)).get_Abfahrts_Stadt().toString() ); ((TextView) convertView.findViewById(R.id.Ankunfts_Stadt)).setText( (String) ((Fahrt) getItem(pos)).get_Ankunfts_Stadt().toString() ); ((TextView) convertView.findViewById(R.id.Dauer)).setText( (String) ((Fahrt) getItem(pos)).get_Fahrt_Dauer().toString() ); ((TextView) convertView.findViewById(R.id.Abfahrts_Zeit2)).setText( (String) Date_Conversion.get_GUI_Date( ((Fahrt) getItem(pos)).get_Abfahrtszeit_Date() )); ((TextView) convertView.findViewById(R.id.Ankunfts_Zeit2)).setText( (String) Date_Conversion.get_GUI_Date( ((Fahrt) getItem(pos)).get_Ankunftszeit_Date() )); ((TextView) convertView.findViewById(R.id.Entfernung)).setText( (String) ((Fahrt) getItem(pos)).get_Fahrt_Entferung().toString() ); ((TextView) convertView.findViewById(R.id.Abfahrts_Km)).setText( (String) ((Fahrt) getItem(pos)).get_Abfahrt_km().toString() ); ((TextView) convertView.findViewById(R.id.Ankunfts_Km)).setText( (String) ((Fahrt) getItem(pos)).get_Ankunfts_km().toString() ); //} } return convertView; } } 

 package de.View; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; import java.util.Stack; import org.json.JSONException; import de.carSync.R; import de.Controler.Drivers_Log_Controller; import de.Controler.Global_Controller; import android.app.Activity; import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.AbsListView; import android.widget.Button; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.Toast; public class Drivers_Log_View extends ListActivity implements AbsListView.OnScrollListener { Drivers_Log_Controller d_l_c; Fahrten_List_Adapter f_l_a_ohne_Auswahl; ListView view_ohne_auswahl; static String TAG = "Drivers_Log_View"; /** Called when the activity is first created. */ @SuppressWarnings("unchecked") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.d_l_c = (Drivers_Log_Controller) Global_Controller.get_Controller_Map().get("actual d_l_c"); this.setContentView(R.layout.list_view); try { d_l_c.lade_Standard_Fahrten(null); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.v(TAG, "now creating Fahrten_List_Adapter"); f_l_a_ohne_Auswahl = new Fahrten_List_Adapter(this, d_l_c.get_Fahrten(), R.layout.fahrt_linear_ohne_auswahl); this.setListAdapter(f_l_a_ohne_Auswahl); d_l_c.listAdapter = this.getListAdapter(); ListView lv = this.getListView(); lv.setOnScrollListener(this); //addListeners(); } public void init() { // } public Drivers_Log_Controller get_D_L_C() { return this.d_l_c; } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.fahrtenbuch_menue_bar, menu); Log.v(TAG, "Do I get here"); return true; } @Override public void onListItemClick(ListView l, View v, int position, long id) { Log.v(TAG, "ListView Position :" + position); super.onListItemClick(l, v, position, id); Global_Controller.start_Fahrt(position); } @SuppressWarnings("unchecked") @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.fahrtenbuch: { try { d_l_c.lade_Standard_Fahrten(null); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.setListAdapter(d_l_c.get_List_Adapter()); } break; case R.id.unv_Fahrten: { try { d_l_c.lade_Unvollstaendige_Fahrten(null); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.setListAdapter(d_l_c.get_List_Adapter()); } break; case R.id.filter: { Global_Controller.start_Filter(); break; } case R.id.zusammenfassen: { Log.v(TAG, "Menu --- Zusammenfassen"); Global_Controller.start_Zusammenfassen(); } break; } return true; } @Override public void onScroll(AbsListView arg0, int firstVisible, int visibleCount, int totalCount) { Log.v(TAG, "firstVisible :" + firstVisible); Log.v(TAG, "visibleCount :" + visibleCount); Log.v(TAG, "totalCount :" + totalCount); boolean endReached = firstVisible + visibleCount >= totalCount; if (endReached) { try { Log.v(TAG, "EndReached"); d_l_c.nachladen_Fahrten(totalCount); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Override public void onScrollStateChanged(AbsListView arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onResume() { super.onResume(); this.setListAdapter(d_l_c.get_List_Adapter()); Log.v(TAG, "OnResume"); } } 

 <?xml version="1.0" encoding="utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:divider="#000000" android:dividerHeight="3px" android:cacheColorHint="#FFFFFF" /> <TextView android:id="@android:id/empty" android:text="nix da...." android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:divider="#000000" android:dividerHeight="3px" android:cacheColorHint="#FFFFFF" /> </LinearLayout> 

Solutions Collecting From Web of "为什么ListView重复每个第六项?"

 @Override public View getView(int pos, View convertView, ViewGroup parent) { if(convertView == null){ convertView = mLayoutInflater.inflate(zeilen_Layout, null); //Assignment code } return convertView; } 

这是因为你已经定义了这个方法(这是getView() )。 它只修改convertView对象,如果它为null ,但在第一页的项目填充后,Android将开始回收视图对象。 这意味着,即第七个被传入的内容不为空 – 它是以前滚动屏幕顶部的对象(第一项)。

条件导致你只是返回传入的方法,所以它似乎一遍又一遍地重复相同的6项。

修改你的列表适配器的查看如下:

 ViewHolder viewHolder = new ViewHolder(); @Override public View getView(int pos, View convertView, ViewGroup parent) { if(convertView == null){ convertView = mLayoutInflater.inflate(zeilen_Layout, null); viewHolder.txtTestText = (TextView) findViewById(R.id.txt_test_item); // Main code goes here convertView.setTag(viewHolder); // this function remember the the view you have inflated }else{ viewHolder = (ViewHolder) convertView.getTag(); // return last set view of ith item } // set data to view here viewHolder.txtTestText.setText(listItem.get(postion).getSelectedItem()); return convertView; } private class ViewHolder { TextView txtTestText; } 

这是我的解决scheme在列表视图中的多重检查

一个正常的任何XML内的列表视图

 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ListView android:id="@+id/lv_lay_yoklamaal" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout> 

适配器类中需要的listview的项目xml看起来就是这个

在这里输入图像说明

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginTop="2dp" android:orientation="horizontal" android:paddingLeft="10dp"> <LinearLayout android:layout_width="50dp" android:layout_height="match_parent" android:background="@drawable/cw_full_oval" android:gravity="center"> <LinearLayout android:layout_width="35dp" android:layout_height="35dp" android:gravity="center"> <TextView android:id="@+id/txt_item_yoklamaal_id" android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/cw_full_oval" android:gravity="center" android:text="1" android:textSize="25dp" android:textStyle="bold"> </TextView> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txt_item_yoklamaal_name" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center|start" android:paddingLeft="15dp" android:text="Ahmet Kemal Bali"> </TextView> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" android:paddingLeft="8dp"> <CheckBox android:id="@+id/rb_item_yoklamaal_var" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="Var" /> <CheckBox android:id="@+id/rb_item_yoklamaal_yok" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="Yok" /> <CheckBox android:id="@+id/rb_item_yoklamaal_izinli" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="İzinli" /> <CheckBox android:id="@+id/rb_item_yoklamaal_gorevli" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="Görevli" /> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> 

我打电话ListView和适配器的活动

 public class YoklamaAlActivity extends Activity { private ListView lv_yoklama; private List<TalebeModel> alltalebe; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lay_yoklamaal); lv_yoklama = (ListView) findViewById(R.id.lv_lay_yoklamaal); List<TalebeModel> talabList = getAlltalebe(); YoklamaAlAdapter yoklamaAdapter = new YoklamaAlAdapter(YoklamaAlActivity.this, talabList); lv_yoklama.setAdapter(yoklamaAdapter); } public List<TalebeModel> getAlltalebe() { List<TalebeModel> modelList = new ArrayList<>(); modelList.add(new TalebeModel(1, "Samir Samedov",false,false,false,false)); modelList.add(new TalebeModel(2, "Ahmet Kemal Bali",false,false,false,false)); modelList.add(new TalebeModel(3, "Nurullah Atik",false,false,false,false)); modelList.add(new TalebeModel(4, "Samir Samedov",false,false,false,false)); modelList.add(new TalebeModel(5, "Ahmet Kemal Bali",false,false,false,false)); modelList.add(new TalebeModel(6, "Nurullah Atik",false,false,false,false)); modelList.add(new TalebeModel(7, "Samir Samedov",false,false,false,false)); modelList.add(new TalebeModel(8, "Ahmet Kemal Bali",false,false,false,false)); modelList.add(new TalebeModel(9, "Nurullah Atik",false,false,false,false)); modelList.add(new TalebeModel(7, "Samir Samedov",false,false,false,false)); modelList.add(new TalebeModel(8, "Ahmet Kemal Bali",false,false,false,false)); modelList.add(new TalebeModel(9, "Nurullah Atik",false,false,false,false)); modelList.add(new TalebeModel(7, "Samir Samedov",false,false,false,false)); modelList.add(new TalebeModel(8, "Ahmet Kemal Bali",false,false,false,false)); modelList.add(new TalebeModel(9, "Nurullah Atik",false,false,false,false)); return modelList; } } 

模型类(设置其字段getter和setter)

 public class TalebeModel { private int numara; private String ad; private boolean var; private boolean yok; private boolean izinli; private boolean gorevli; public TalebeModel() { } public TalebeModel(int numara, String ad) { this.numara = numara; this.ad = ad; } public TalebeModel(int numara, String ad, boolean var, boolean yok, boolean izinli, boolean gorevli) { this.numara = numara; this.ad = ad; this.var = var; this.yok = yok; this.izinli = izinli; this.gorevli = gorevli; } } 

和finnaly适配器类

 /** * Created by Samir on 24.1.2017. */ public class YoklamaAlAdapter extends BaseAdapter { Context context; List<TalebeModel> talebeList; ViewHolder holder; ArrayList<Boolean> checked; int displaySize; public YoklamaAlAdapter(Context contextt, List<TalebeModel> talebeModels) { this.context = contextt; this.talebeList = talebeModels; displaySize = 10; } private class ViewHolder { TextView mName; TextView mId; TextView mVar; TextView mYok; ImageView mIzinli; TextView mGorevli; LinearLayout lin_lay_etkinlik; CheckBox rbVar; CheckBox rbYok; CheckBox rbIzinli; CheckBox rbGorevli; } @Override public int getCount() { return talebeList.size(); } /* @Override public int getViewTypeCount() { return getCount(); }*/ @Override public TalebeModel getItem(int i) { return talebeList.get(i); } @Override public long getItemId(int i) { return 0; } @Override public View getView(final int i, View view, ViewGroup viewGroup) { holder = new ViewHolder(); final TalebeModel currentTalebe; LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); if (view == null) { view = mInflater.inflate(R.layout.item_yoklamaal, null); holder = new ViewHolder(); holder.mName = (TextView) view.findViewById(R.id.txt_item_yoklamaal_name); holder.mId = (TextView) view.findViewById(R.id.txt_item_yoklamaal_id); holder.rbVar = (CheckBox) view.findViewById(R.id.rb_item_yoklamaal_var); holder.rbYok = (CheckBox) view.findViewById(R.id.rb_item_yoklamaal_yok); holder.rbIzinli = (CheckBox) view.findViewById(R.id.rb_item_yoklamaal_izinli); holder.rbGorevli = (CheckBox) view.findViewById(R.id.rb_item_yoklamaal_gorevli); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } currentTalebe = getItem(i); holder.mName.setText(currentTalebe.getAd()); holder.mId.setText("" + currentTalebe.getNumara()); holder.rbVar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { holder.rbYok.clearFocus(); if (currentTalebe.isVar()) { currentTalebe.setVar(false); // holder.rbVar.setChecked(false); //Toast.makeText(context, "Var a tıkladın", Toast.LENGTH_SHORT).show(); holder.rbYok.setChecked(false); holder.rbIzinli.setChecked(false); holder.rbGorevli.setChecked(false); } else { //holder.rbVar.setChecked(true); currentTalebe.setVar(true); holder.rbYok.setChecked(false); holder.rbIzinli.setChecked(true); holder.rbGorevli.setChecked(true); } currentTalebe.setYok(false); currentTalebe.setIzinli(false); currentTalebe.setGorevli(false); } }); setCheckboxesChecked(currentTalebe); holder.rbYok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (currentTalebe.isYok()) { currentTalebe.setYok(false); } else { currentTalebe.setYok(true); } currentTalebe.setVar(false); currentTalebe.setIzinli(false); currentTalebe.setGorevli(false); } }); setCheckboxesChecked(currentTalebe); holder.rbGorevli.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (currentTalebe.isGorevli()) { currentTalebe.setGorevli(false); } else { currentTalebe.setGorevli(true); } currentTalebe.setVar(false); currentTalebe.setYok(false); currentTalebe.setIzinli(false); } }); setCheckboxesChecked(currentTalebe); holder.rbIzinli.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (currentTalebe.isIzinli()) { currentTalebe.setIzinli(false); } else { currentTalebe.setIzinli(true); } currentTalebe.setVar(false); currentTalebe.setYok(false); currentTalebe.setGorevli(false); } }); setCheckboxesChecked(currentTalebe); return view; } /*@Override public boolean isEnabled(int position) { return mListItem[position].isEnabled(); }*/ private void setCheckboxesChecked(TalebeModel currentTalebe) { holder.rbVar.setChecked(currentTalebe.isVar()); holder.rbYok.setChecked(currentTalebe.isYok()); holder.rbIzinli.setChecked(currentTalebe.isIzinli()); holder.rbGorevli.setChecked(currentTalebe.isGorevli()); } } 

这真是头疼,没有人给出任何合理的解决scheme,这就是为什么我分享了我所有的这个问题的工作