为什么ListView的重复每6个项目? [英] Why did the ListView repeated every 6th item?

查看:145
本文介绍了为什么ListView的重复每6个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由自定义适配器与特定的布局填补一个ListView。该适配器映射到包含每个ListView控件元素的数据的具体内容是一个HashMap。

在HashMap中的数据是正确的,但在ListView重复绘制每一次6'th相同6'th元素,直到它到达地图??

的结束

我的显示允许显示5个项目,如果你sroll豆蔻这6个项目。

下面是适配器的code时,ListActivity的相关code和ListView控件的布局文件。

请帮忙,我不知道为什么会这样。

 包de.View;进口的java.util.ArrayList;
进口的java.util.Map;进口de.carSync.R;
进口de.Common.Date_Conversion;
进口de.Common.GUI_Output;
进口de.Model.DriversLog.Fahrt;
进口de.Model.DriversLog.Geladene_Fahrten;进口android.content.Context;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.TextView;公共类Fahrten_List_Adapter延伸BaseAdapter {    私有静态字符串标记=Fahrten_List_Adapter;    私人地图<整数,Fahrt> fahrten_Liste;    私人最终LayoutInflater mLayoutInflater;    INT zeilen_Layout;    公共Fahrten_List_Adapter(上下文CTX,地图<整数,Fahrt> f_l,诠释zeilen_Layout){
        this.zeilen_Layout = zeilen_Layout;
        fahrten_Liste = f_l;
        mLayoutInflater =(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }    @覆盖
    公众诠释的getCount(){
        返回fahrten_Liste.size();
    }    @覆盖
    公共对象的getItem(INT POS){
        返回fahrten_Liste.get(POS)
    }    @覆盖
    众长getItemId(INT POS){
        返回0;
    }    公共无效清除(){
        fahrten_Liste.clear();
    }    @覆盖
    公共查看getView(INT POS,查看convertView,父母的ViewGroup){        如果(convertView == NULL){
            convertView = mLayoutInflater.inflate(zeilen_Layout,NULL);            Log.v(TAG,getView:+ POS)            ((的TextView)convertView.findViewById(R.id.FahrtID))的setText((字符串)((Fahrt)的getItem(POS))get_Fahrt_ID());            ((的TextView)convertView.findViewById(R.id.Kennzeichen))的setText(。(字符串)((Fahrt)的getItem(POS))get_Kennzeichen());            ((的TextView)convertView.findViewById(R.id.Abfahrts_Strasse))的setText(。(字符串)((Fahrt)的getItem(POS))get_Abfahrts_Strasse()的toString());            ((的TextView)convertView.findViewById(R.id.Ankunfts_Strasse))的setText(。(字符串)((Fahrt)的getItem(POS))get_Ankunfts_Strasse()的toString());            ((的TextView)convertView.findViewById(R.id.Fahrttyp))的setText((字符串)GUI_Output.get_Fahrttyp((对象)((Fahrt)的getItem(POS))get_Fahrttyp()));            ((的TextView)convertView.findViewById(R.id.Abfahrts_Stadt))的setText(。(字符串)((Fahrt)的getItem(POS))get_Abfahrts_Stadt()的toString());            ((的TextView)convertView.findViewById(R.id.Ankunfts_Stadt))的setText(。(字符串)((Fahrt)的getItem(POS))get_Ankunfts_Stadt()的toString());            ((的TextView)convertView.findViewById(R.id.Dauer))的setText(。(字符串)((Fahrt)的getItem(POS))get_Fahrt_Dauer()的toString());            ((的TextView)convertView.findViewById(R.id.Abfahrts_Zeit2))的setText((字符串)Date_Conversion.get_GUI_Date(((Fahrt)的getItem(POS))get_Abfahrtszeit_Date()));            ((的TextView)convertView.findViewById(R.id.Ankunfts_Zeit2))的setText((字符串)Date_Conversion.get_GUI_Date(((Fahrt)的getItem(POS))get_Ankunftszeit_Date()));            ((的TextView)convertView.findViewById(R.id.Entfernung))的setText(。(字符串)((Fahrt)的getItem(POS))get_Fahrt_Entferung()的toString());            (。(字符串)((Fahrt)的getItem(POS))get_Abfahrt_km()的toString())((的TextView)convertView.findViewById(R.id.Abfahrts_Km))的setText;            (。(字符串)((Fahrt)的getItem(POS))get_Ankunfts_km()的toString())((的TextView)convertView.findViewById(R.id.Ankunfts_Km))的setText;        //}
    }
        返回convertView;
    }}


 包de.View;进口java.io.IOException异常;
进口java.io.UnsupportedEncodingException;
进口java.net.URISyntaxException;
进口java.util.Stack中;进口org.json.JSONException;进口de.carSync.R;
进口de.Controler.Drivers_Log_Controller;
进口de.Controler.Global_Controller;
进口android.app.Activity;
进口android.app.ListActivity;
进口android.content.Context;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.KeyEvent;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.View.OnClickListener;
进口android.widget.AbsListView;
进口android.widget.Button;
进口android.widget.ListAdapter;
进口android.widget.ListView;
进口android.widget.Toast;公共类Drivers_Log_View扩展ListActivity实现AbsListView.OnScrollListener {    Drivers_Log_Controller d_l_c;    Fahrten_List_Adapter f_l_a_ohne_Auswahl;
    ListView的view_ohne_auswahl;
    静态字符串标记=Drivers_Log_View;    / **当第一次创建活动调用。 * /
    @燮pressWarnings(未登记)
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        this.d_l_c =(Drivers_Log_Controller)Global_Controller.get_Controller_Map()获得(实际d_l_c);        this.setContentView(R.layout.list_view);
        尝试{
            d_l_c.lade_Standard_Fahrten(NULL);
        }赶上(JSONException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(的URISyntaxException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
        Log.v(TAG,现在创建Fahrten_List_Adapter);
        f_l_a_ohne_Auswahl =新Fahrten_List_Adapter(这一点,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(本);        // addListeners();    }    公共无效的init(){
        //
    }    公共Drivers_Log_Controller get_D_L_C(){
        返回this.d_l_c;
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        MenuInflater吹气= getMenuInflater();
        inflater.inflate(R.menu.fahrtenbuch_menue_bar,菜单);
        Log.v(TAG,难道我到这里来);
        返回true;
    }    @覆盖
    公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
        Log.v(TAG的ListView位置:+位置);
        super.onListItemClick(L,V,位置ID);
        Global_Controller.start_Fahrt(位置);
    }    @燮pressWarnings(未登记)
    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){        案例R.id.fahrtenbuch:{            尝试{
                d_l_c.lade_Standard_Fahrten(NULL);
            }赶上(JSONException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(的URISyntaxException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            this.setListAdapter(d_l_c.get_List_Adapter());        }
            打破;        案例R.id.unv_Fahrten:{            尝试{
                d_l_c.lade_Unvollstaendige_Fahrten(NULL);
            }赶上(JSONException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            this.setListAdapter(d_l_c.get_List_Adapter());        }
            打破;        案例R.id.filter:{
            Global_Controller.start_Filter();
            打破;
        }        案例R.id.zusammenfassen:{
            Log.v(TAG,菜单--- Zusammenfassen);
            Global_Controller.start_Zusammenfassen();
        }
            打破;        }
        返回true;
    }    @覆盖
    公共无效onScroll(AbsListView为arg0,诠释firstVisible,INT visibleCount,
            INT TOTALCOUNT){
        Log.v(TAG,firstVisible:+ firstVisible);
        Log.v(TAG,visibleCount:+ visibleCount);
        Log.v(TAG,TOTALCOUNT:+ TOTALCOUNT);        布尔endReached = firstVisible + visibleCount> = TOTALCOUNT;        如果(endReached){
            尝试{
                Log.v(TAGEndReached);                d_l_c.nachladen_Fahrten(TOTALCOUNT);            }赶上(例外五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
        }    }    @覆盖
    公共无效onScrollStateChanged(AbsListView为arg0,ARG1 INT){
        // TODO自动生成方法存根    }    @覆盖
    公共无效onResume(){
        super.onResume();
        this.setListAdapter(d_l_c.get_List_Adapter());
        Log.v(TAGOnResume);
    }}


 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android的android:layout_width =match_parent机器人:layout_height =match_parent机器人:方向=垂直>
  < ListView的机器人:ID =@机器人:ID /列表的android:layout_width =match_parent机器人:layout_height =match_parent机器人:背景=#FFFFFF机器人:分=#000000机器人:dividerHeight =的3px机器人:cacheColorHint =#FFFFFF/>
  < TextView的机器人:ID =@机器人:ID /空的android:文本=尼克斯哒......的android:layout_width =match_parent机器人:layout_height =match_parent机器人:背景=#FFFFFF机器人:分=#000000机器人:dividerHeight =的3px机器人:cacheColorHint =#FFFFFF/>
  < / LinearLayout中>


解决方案

  @覆盖
公共查看getView(INT POS,查看convertView,父母的ViewGroup){    如果(convertView == NULL){
        convertView = mLayoutInflater.inflate(zeilen_Layout,NULL);        //赋值code
    }
    返回convertView;
}

这是发生的原因是因为你是如何定义这个方法(这是 getView())。它只修改convertView对象,如果是,但项目的第一页填满后,Android开始的回收的视图对象。这意味着,即是被传递的第七位的不为空的 - 它是滚动到屏幕$ P $顶部pviously对象(第一项)

有条件的原因,您刚刚返回的内容传递到方法,因此它似乎一遍又一遍地重复同样的6个项目。

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>

解决方案

@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.

这篇关于为什么ListView的重复每6个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆