Why did the ListView repeated every 6th item?

2020-01-27 07:36发布

I have a ListView that is filled by a Custom Adapter with a specific Layout. The Adapter is mapped to a HashMap with the specific Elements that contains the Data for each ListView Element.

The Data in the hashMap is correct, but the ListView repeats to draw every 6'th the same 6'th Elements again, till it reaches the end of the Map??

My Display allows to display 5 items, if you sroll a litte it's 6 items.

Here is the Code of the Adapter, the relevant Code of the ListActivity and the layout file of the ListView.

Please help, I've no Idea why this happens.

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>

3条回答
▲ chillily
2楼-- · 2020-01-27 08:14

Modify you getView of list adapter as follow:

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;
}
查看更多
forever°为你锁心
3楼-- · 2020-01-27 08:24

Here is my Solution to Multi check in a listview

A normal listview inside any 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>

item xml of listview that needed in adapter class seem liste this

enter image description here

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

The Activity where I call Listview and Adapter

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

The Model Class ( set its field getter and setters)

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

And finnaly the Adapter class

/**
 * 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());
    }
}

This is really a headache and no one giving any logical solution, that is why I shared all my work with this issue

查看更多
乱世女痞
4楼-- · 2020-01-27 08:28
@Override
public View getView(int pos, View convertView, ViewGroup parent) {

    if(convertView == null){
        convertView = mLayoutInflater.inflate(zeilen_Layout, null);

        //Assignment code
    }
    return convertView;
}

The reason this is happening is because of how you've defined this method (which is getView()). It only modifies the convertView object if it is null, but after the first page of items is filled Android will start recycling the View objects. This means that i.e. the seventh one that gets passed in is not null - it is the object that scrolled off the top of the screen previously (the first item).

The conditional causes you just return what was passed in to the method so it appears to repeat the same 6 items over and over.

查看更多
登录 后发表回答