为什么ListView每隔6个项重复一次?

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

hashMap中的数据是正确的,但ListView重复绘制每6个相同的第6个元素,直到它到达Map的末尾?

我的显示器允许显示5个项目,如果你滚动一个litte它是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 fahrten_Liste; private final LayoutInflater mLayoutInflater; int zeilen_Layout; public Fahrten_List_Adapter(Context ctx, Map 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"); } } 

      

 @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将开始回收 View对象。 这意味着即传入的第七个不为空 – 它是先前从屏幕顶部滚动的对象(第一个项目)。

条件导致您只返回传入方法的内容,因此它似乎一遍又一遍地重复相同的6个项目。

修改列表适配器的getView如下:

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

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

任何xml内的正常列表视图

    

适配器类中需要的listview的项目xml似乎是在听这个

在此处输入图像描述

                      

我称之为Listview和Adapter的Activity

 public class YoklamaAlActivity extends Activity { private ListView lv_yoklama; private List 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 talabList = getAlltalebe(); YoklamaAlAdapter yoklamaAdapter = new YoklamaAlAdapter(YoklamaAlActivity.this, talabList); lv_yoklama.setAdapter(yoklamaAdapter); } public List getAlltalebe() { List 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; } } 

最后是Adapter类

 /** * Created by Samir on 24.1.2017. */ public class YoklamaAlAdapter extends BaseAdapter { Context context; List talebeList; ViewHolder holder; ArrayList checked; int displaySize; public YoklamaAlAdapter(Context contextt, List 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()); } } 

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