Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

android - Why did the ListView repeated every 6th item?

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
@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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...