Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android listview中的行不保留外观,并重复显示_Android_Listview - Fatal编程技术网

Android listview中的行不保留外观,并重复显示

Android listview中的行不保留外观,并重复显示,android,listview,Android,Listview,我有一个列表,当您单击右侧的行时,带有图标的布局会被其他行更改。这很好,但是当我在列表中滚动时,我注意到有行在没有单击的情况下具有相同的图标。此外,如果我快速滚动,这些相同的图标将丢失,并且构建方式不同。我认为这是关于细胞的再利用,但我找不到解决办法。我想要的是,图像仅在单击A的行中保持活动状态 谢谢 适配器类: public class ListadoVotantesArrayAdapter extends ArrayAdapter<Votante> { private

我有一个列表,当您单击右侧的行时,带有图标的布局会被其他行更改。这很好,但是当我在列表中滚动时,我注意到有行在没有单击的情况下具有相同的图标。此外,如果我快速滚动,这些相同的图标将丢失,并且构建方式不同。我认为这是关于细胞的再利用,但我找不到解决办法。我想要的是,图像仅在单击A的行中保持活动状态

谢谢

适配器
类:

public class ListadoVotantesArrayAdapter extends ArrayAdapter<Votante> {

    private Context context;
    private LayoutInflater inflater;
    private ArrayList<Votante> listaVotantes;
    private int rowLayout;
    private View mConverView;

    public ListadoVotantesArrayAdapter(Context context, int resource, ArrayList<Votante> objects) {
        super(context, resource,objects);
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.listaVotantes = objects;
        this.rowLayout = resource;
    }

    public int getCount(){
        return this.listaVotantes.size();
    }

    public Votante getVotante(int position){
        return this.listaVotantes.get(position);
    }

    private static class ViewHolder {
        public TextView lblName;
        public TextView lblLastName;
        public TextView lblDni;
        public TextView lblVoterNumber;
        public RelativeLayout lytVoted;
        public RelativeLayout lytCanVote;
        public RelativeLayout lytUndo;
    }


    public View getView(int position, View converView, ViewGroup parent) {

        final ViewHolder viewHolder;
        mConverView = converView;
        if (mConverView == null){
            viewHolder = new ViewHolder();

            this.mConverView = inflater.inflate(this.rowLayout, parent, false);

            if (mConverView != null){
                viewHolder.lblName = (TextView) mConverView.findViewById(R.id.lblVoterName);
                viewHolder.lblLastName = (TextView) mConverView.findViewById(R.id.lblVoterLastName);
                viewHolder.lblDni = (TextView) mConverView.findViewById(R.id.lblDni);
                viewHolder.lblVoterNumber = (TextView) mConverView.findViewById(R.id.lblNumber);
                viewHolder.lytVoted = (RelativeLayout) mConverView.findViewById(R.id.lytVoted);
                viewHolder.lytCanVote = (RelativeLayout) mConverView.findViewById(R.id.lytCanVote);
                viewHolder.lytUndo = (RelativeLayout) mConverView.findViewById(R.id.lytUndo);
                mConverView.setTag(viewHolder);
            }
        }else{
            viewHolder = (ViewHolder) mConverView.getTag();
        }

        Votante votante = getVotante(position);

        viewHolder.lblName.setText(votante.getNombre());
        viewHolder.lblLastName.setText(votante.getApellidos());
        viewHolder.lblDni.setText(votante.getDni());
        viewHolder.lblVoterNumber.setText(""+votante.getNumVotante());

        if (votante.getVotado()){
            viewHolder.lytVoted.setVisibility(View.VISIBLE);
            viewHolder.lytCanVote.setVisibility(View.GONE);
        }else{
            viewHolder.lytVoted.setVisibility(View.GONE);
            viewHolder.lytCanVote.setVisibility(View.VISIBLE);
        }
        mConverView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHolder.lytUndo.setVisibility(View.VISIBLE);
                notifyDataSetChanged();
            }
        });
        return mConverView;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lyt_voter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="1dp">

    <RelativeLayout
        android:id="@+id/lytVoterNumber"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:background="@color/lightBlueItemList">


        <TextView
            android:id="@+id/lblNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="1999"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/lytVoterData"
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/lytCanVote"
        android:layout_toRightOf="@+id/lytVoterNumber"
        android:layout_toStartOf="@+id/lytCanVote"
        android:background="@color/whiteItemList"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/lblVoterLastName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Suarez Garcia de la Serna"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/lblVoterName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="José Carlos"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/lblDni"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="44950962S"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/lytCanVote"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentRight="true"
        android:background="@color/yellowItemList"
        android:minHeight="30dp"
        android:minWidth="30dp">

        <ImageView
            android:id="@+id/imgVote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/flecha" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/lytVoted"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentRight="true"
        android:background="@color/grrenAcceptList"
        android:minHeight="30dp"
        android:minWidth="30dp"
        android:visibility="gone">

        <ImageView
            android:id="@+id/imgAccept"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/aceptar"
             />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:minHeight="30dp"
        android:minWidth="30dp"
        android:background="@color/redD3"
        android:id="@+id/lytUndo"
        android:layout_alignParentRight="true"
        android:visibility="gone">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imgUndo"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/undo"/>
    </RelativeLayout>


</RelativeLayout>
公共类ListadoVotantesArrayAdapter扩展了ArrayAdapter{
私人语境;
私人充气机;
私人ArrayList listaVotantes;
私人室内布局;
私有视图mConverView;
public ListadoVotantesArrayAdapter(上下文上下文、int资源、ArrayList对象){
超级(上下文、资源、对象);
this.context=上下文;
this.inflater=layoutiner.from(上下文);
this.listaVotantes=对象;
this.rowLayout=资源;
}
public int getCount(){
返回此.listaVotantes.size();
}
公共Votante getVotante(内部位置){
返回此.listaVotantes.get(位置);
}
私有静态类视图持有者{
公共文本视图lblName;
公共文本视图lblLastName;
公共文本视图lblDni;
公共文本视图lblVoterNumber;
公共相对论;
公共关系律师事务所;
公共关系专家;
}
公共视图getView(内部位置、视图对流视图、视图组父视图){
最终持票人;
mConverView=对流视图;
if(mConverView==null){
viewHolder=新的viewHolder();
this.mConverView=充气机.充气(this.rowLayout,父项,false);
如果(mConverView!=null){
viewHolder.lblName=(TextView)mConverView.findViewById(R.id.lblVoterName);
viewHolder.lblLastName=(TextView)mConverView.findViewById(R.id.lblVoterLastName);
viewHolder.lblDni=(TextView)mConverView.findViewById(R.id.lblDni);
viewHolder.lblVoterNumber=(TextView)mConverView.findViewById(R.id.lblNumber);
viewHolder.lytVoted=(RelativeLayout)mConverView.findViewById(R.id.lytVoted);
viewHolder.lytcavote=(RelativeLayout)mConverView.findViewById(R.id.lytcavote);
viewHolder.lytUndo=(RelativeLayout)mConverView.findviewbyd(R.id.lytUndo);
mConverView.setTag(viewHolder);
}
}否则{
viewHolder=(viewHolder)mConverView.getTag();
}
Votante-Votante=getVotante(位置);
viewHolder.lblName.setText(votante.getNombre());
viewHolder.lblLastName.setText(votante.getApellidos());
viewHolder.lblDni.setText(votante.getDni());
viewHolder.lblVoterNumber.setText(“+votante.getNumVotante());
if(votante.getVotado()){
viewHolder.lytVoted.setVisibility(View.VISIBLE);
viewHolder.lytcavote.setVisibility(View.GONE);
}否则{
viewHolder.lytVoted.setVisibility(View.GONE);
viewHolder.lytcavote.setVisibility(View.VISIBLE);
}
mConverView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
viewHolder.lytUndo.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
});
返回mConverView;
}
}
布局行:

public class ListadoVotantesArrayAdapter extends ArrayAdapter<Votante> {

    private Context context;
    private LayoutInflater inflater;
    private ArrayList<Votante> listaVotantes;
    private int rowLayout;
    private View mConverView;

    public ListadoVotantesArrayAdapter(Context context, int resource, ArrayList<Votante> objects) {
        super(context, resource,objects);
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.listaVotantes = objects;
        this.rowLayout = resource;
    }

    public int getCount(){
        return this.listaVotantes.size();
    }

    public Votante getVotante(int position){
        return this.listaVotantes.get(position);
    }

    private static class ViewHolder {
        public TextView lblName;
        public TextView lblLastName;
        public TextView lblDni;
        public TextView lblVoterNumber;
        public RelativeLayout lytVoted;
        public RelativeLayout lytCanVote;
        public RelativeLayout lytUndo;
    }


    public View getView(int position, View converView, ViewGroup parent) {

        final ViewHolder viewHolder;
        mConverView = converView;
        if (mConverView == null){
            viewHolder = new ViewHolder();

            this.mConverView = inflater.inflate(this.rowLayout, parent, false);

            if (mConverView != null){
                viewHolder.lblName = (TextView) mConverView.findViewById(R.id.lblVoterName);
                viewHolder.lblLastName = (TextView) mConverView.findViewById(R.id.lblVoterLastName);
                viewHolder.lblDni = (TextView) mConverView.findViewById(R.id.lblDni);
                viewHolder.lblVoterNumber = (TextView) mConverView.findViewById(R.id.lblNumber);
                viewHolder.lytVoted = (RelativeLayout) mConverView.findViewById(R.id.lytVoted);
                viewHolder.lytCanVote = (RelativeLayout) mConverView.findViewById(R.id.lytCanVote);
                viewHolder.lytUndo = (RelativeLayout) mConverView.findViewById(R.id.lytUndo);
                mConverView.setTag(viewHolder);
            }
        }else{
            viewHolder = (ViewHolder) mConverView.getTag();
        }

        Votante votante = getVotante(position);

        viewHolder.lblName.setText(votante.getNombre());
        viewHolder.lblLastName.setText(votante.getApellidos());
        viewHolder.lblDni.setText(votante.getDni());
        viewHolder.lblVoterNumber.setText(""+votante.getNumVotante());

        if (votante.getVotado()){
            viewHolder.lytVoted.setVisibility(View.VISIBLE);
            viewHolder.lytCanVote.setVisibility(View.GONE);
        }else{
            viewHolder.lytVoted.setVisibility(View.GONE);
            viewHolder.lytCanVote.setVisibility(View.VISIBLE);
        }
        mConverView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHolder.lytUndo.setVisibility(View.VISIBLE);
                notifyDataSetChanged();
            }
        });
        return mConverView;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lyt_voter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="1dp">

    <RelativeLayout
        android:id="@+id/lytVoterNumber"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:background="@color/lightBlueItemList">


        <TextView
            android:id="@+id/lblNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="1999"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/lytVoterData"
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/lytCanVote"
        android:layout_toRightOf="@+id/lytVoterNumber"
        android:layout_toStartOf="@+id/lytCanVote"
        android:background="@color/whiteItemList"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/lblVoterLastName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Suarez Garcia de la Serna"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/lblVoterName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="José Carlos"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/lblDni"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="44950962S"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/lytCanVote"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentRight="true"
        android:background="@color/yellowItemList"
        android:minHeight="30dp"
        android:minWidth="30dp">

        <ImageView
            android:id="@+id/imgVote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/flecha" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/lytVoted"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentRight="true"
        android:background="@color/grrenAcceptList"
        android:minHeight="30dp"
        android:minWidth="30dp"
        android:visibility="gone">

        <ImageView
            android:id="@+id/imgAccept"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/aceptar"
             />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:minHeight="30dp"
        android:minWidth="30dp"
        android:background="@color/redD3"
        android:id="@+id/lytUndo"
        android:layout_alignParentRight="true"
        android:visibility="gone">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imgUndo"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/undo"/>
    </RelativeLayout>


</RelativeLayout>

您需要遵循以下想法

1) 这行代码
mConverView=converView没有意义。直接使用converView(从getView参数查看)。
从适配器中删除
mConvertView

2) 添加一些机制,可以告诉您单击了哪一行并保存该变量

示例:-具有大小行的布尔数组。大小。把它们放在onClick里

Boolean[] array = new Boolean[getSize()];
在getview中已有的onClick中设置此选项

public void onClick(View v) {
     array[position] = !array[position];
     viewHolder.lytUndo.setVisibility(View.VISIBLE);
     //You do not need to call notifyDatasetChange.
3) 在
getView()
中,当您将数据设置到行项目中/或在单击
onClick
之前。有一张像下面这样的支票

if(array[position])// this will tell you if you need to show or hid lytUndo thing
     viewHolder.lytUndo.setVisibility(View.VISIBLE); // Show it
else 
     viewHolder.lytUndo.setVisibility(View.GONE); // hide it or do whatever you want
您可能需要将一些参数设置为final

我已经尽可能简单地写了这篇文章,请用你的结果进行评论