滚动时编辑列表重复数据中的文本

时间:2018-09-03 18:25:52

标签: android listview android-edittext

我遇到了这个问题:虽然列表视图回收了视图,但它会复制插入编辑文本中的数据,并且我只需要复制文本即可,这样我就可以获得正确的数量。我尝试不回收视图,但向下滚动时会删除这些值。

public class ArticleAdapter extends ArrayAdapter<Article>
{


    public  static List<Livraison_detail> MAJdb=new ArrayList<Livraison_detail>();;
    ViewHolder viewHolder;
    List<Article> articles=null;
    String []qtes;

    public ArticleAdapter(Context context, int resource, List<Article> objects)
    {
        super(context, resource, objects);
        articles=new ArrayList<>(objects);
        qtes=new String[articles.size()];
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        viewHolder= new ViewHolder();

        if(convertView==null){
            LayoutInflater infalter=LayoutInflater.from(getContext());
            convertView=infalter.inflate(R.layout.list_view_article,parent,false);


            viewHolder.designation = (TextView) convertView.findViewById(R.id.desi);
            viewHolder.quantité = (TextView) convertView.findViewById(R.id.qte_disp_2);
            viewHolder.prix = (TextView) convertView.findViewById(R.id.prix);
            viewHolder.qte=(EditText) convertView.findViewById(R.id.qte);
            viewHolder.cb=(CheckBox)convertView.findViewById(R.id.cbarticle);



            viewHolder.cb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CheckBox check=(CheckBox)view;
                    check.requestFocus();
                    if (check.isChecked())
                    {
                        Article a =articles.get((int)check.getTag());
                        Livraison_detail l=new Livraison_detail();
                        l.CODE_ARTICLE=a.CODE_ARTICLE;
                        l.PRIX_ACHAT=a.PRIX_VENTE;
                        try
                        {
                            if(qtes[(int)check.getTag()].equals(""))
                            {
                                throw new Exception();
                            }else {
                                if (Double.parseDouble(qtes[(int)check.getTag()])>a.QTE_CHARGE) throw  new QuantitéInsuffisanteException();
                                else{
                                    l.QTE_LIVRE=Double.parseDouble(qtes[(int)check.getTag()]);
                                    MAJdb.add(l);
                                }
                            }
                        }
                        catch (QuantitéInsuffisanteException e)
                        {
                            new AlertDialog.Builder(getContext())
                                    .setTitle("Quantité Insuffisante")
                                    .setMessage("la quantité saisie est superieur que celle du Stock il y a "+a.QTE_CHARGE+" artcile Disp")
                                    .setCancelable(true)
                                    .setIcon(R.drawable.alert)
                                    .setNeutralButton("Compris",null )
                                    .show();
                        }
                        catch (Exception e)
                        {
                            Toast.makeText(getContext(), "Il faut toucher le fin bouton du clavier puis re-cochez", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Article a2=articles.get((int)check.getTag());

                        for (Livraison_detail l:MAJdb) {
                            if (l.CODE_ARTICLE.equals(a2.CODE_ARTICLE))
                            {
                                MAJdb.remove(l);
                            }
                        }
                    }
                }
            });

            viewHolder.qte.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final int position = view.getId();
                    final EditText Caption = (EditText) view;
                    qtes[position]=Caption.getText().toString();
                    if(Caption.isFocused()) {Caption.clearFocus();}
                }
            });

            convertView.setTag(viewHolder);
            convertView.setTag(R.id.cbarticle, viewHolder.cb);
            convertView.setTag(R.id.qte, viewHolder.qte);
        }
        else {
            viewHolder =(ViewHolder)convertView.getTag();
        }
        viewHolder.qte.setTag(position);
        viewHolder.cb.setTag(position);
        viewHolder.qte.setId(position);



        Article article=articles.get(position);
        if(article !=null)
        {
            viewHolder.designation.setText(article.DESIGNATION);
            viewHolder.prix.setText(Double.toString(article.PRIX_VENTE));
            viewHolder.quantité.setText(Integer.toString(article.QTE_CHARGE));
        }


        return convertView;
    }



    private class ViewHolder
    {
        TextView designation;
        TextView quantité;
        TextView prix;
        EditText qte;
        CheckBox cb;
    }


}

2 个答案:

答案 0 :(得分:0)

很难编译代码来检查错误。但是看一下,我可以提出两个建议:

  1. 为每个位置存储单独的ViewHolder
  2. 请勿使用View.setTag()方法存储位置值;

    public static List<Livraison_detail> MAJdb = new ArrayList<Livraison_detail>();
    
    private SparseArray<ViewHolder> map = new SparseArray<>();
    List<Article> articles = null;
    String[] qtes;
    
    public ArticleAdapter(Context context, int resource, List<Article> objects) {
        super(context, resource, objects);
        articles = new ArrayList<>(objects);
        qtes = new String[articles.size()];
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
    
        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater infalter = LayoutInflater.from(getContext());
            convertView = infalter.inflate(R.layout.list_view_article, parent, false);
    
    
            viewHolder.designation = (TextView) convertView.findViewById(R.id.desi);
            viewHolder.quantité = (TextView) convertView.findViewById(R.id.qte_disp_2);
            viewHolder.prix = (TextView) convertView.findViewById(R.id.prix);
            viewHolder.qte = (EditText) convertView.findViewById(R.id.qte);
            viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cbarticle);
    
    
            viewHolder.cb.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            CheckBox check = (CheckBox) view;
                            check.requestFocus();
                            if (check.isChecked()) {
                                Article a = articles.get(position);
                                Livraison_detail l = new Livraison_detail();
                                l.CODE_ARTICLE = a.CODE_ARTICLE;
                                l.PRIX_ACHAT = a.PRIX_VENTE;
                                try {
                                    if (qtes[position].equals("")) {
                                        throw new Exception();
                                    } else {
                                        if (Double.parseDouble(qtes[position]) > a.QTE_CHARGE) {
                                            throw new QuantitéInsuffisanteException();
                                        } else {
                                            l.QTE_LIVRE = Double.parseDouble(qtes[position]);
                                            MAJdb.add(l);
                                        }
                                    }
                                } catch (QuantitéInsuffisanteException e) {
                                    new AlertDialog.Builder(getContext())
                                            .setTitle("Quantité Insuffisante")
                                            .setMessage("la quantité saisie est superieur que celle du Stock il y a " + a.QTE_CHARGE + " artcile Disp")
                                            .setCancelable(true)
                                            .setIcon(R.drawable.alert)
                                            .setNeutralButton("Compris", null)
                                            .show();
                                } catch (Exception e) {
                                    Toast.makeText(getContext(), "Il faut toucher le fin bouton du clavier puis re-cochez", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                Article a2 = articles.get(position);
    
                                for (Livraison_detail l : MAJdb) {
                                    if (l.CODE_ARTICLE.equals(a2.CODE_ARTICLE)) {
                                        MAJdb.remove(l);
                                    }
                                }
                            }
                        }
                    });
    
            viewHolder.qte.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            final int position = view.getId();
                            final EditText Caption = (EditText) view;
                            qtes[position] = Caption.getText().toString();
                            if (Caption.isFocused()) {
                                Caption.clearFocus();
                            }
                        }
                    });
    
            map.put(position, viewHolder);
            convertView.setTag(R.id.cbarticle, viewHolder.cb);
            convertView.setTag(R.id.qte, viewHolder.qte);
        } else {
            viewHolder = map.get(position);
        }
    
        viewHolder.qte.setTag(position);
        viewHolder.cb.setTag(position);
        viewHolder.qte.setId(position);
    
    
        Article article = articles.get(position);
        if (article != null) {
            viewHolder.designation.setText(article.DESIGNATION);
            viewHolder.prix.setText(Double.toString(article.PRIX_VENTE));
            viewHolder.quantité.setText(Integer.toString(article.QTE_CHARGE));
        }
    
    
        return convertView;
    }
    
    
    private class ViewHolder {
        TextView designation;
        TextView quantité;
        TextView prix;
        EditText qte;
        CheckBox cb;
    }
    

答案 1 :(得分:0)

尝试这个...我已经修改了您的getView()方法 希望对您有帮助

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    viewHolder= new ViewHolder();

    if(convertView==null){
        LayoutInflater infalter=LayoutInflater.from(getContext());
        convertView=infalter.inflate(R.layout.list_view_article,parent,false);


        viewHolder.designation = (TextView) convertView.findViewById(R.id.desi);
        viewHolder.quantité = (TextView) convertView.findViewById(R.id.qte_disp_2);
        viewHolder.prix = (TextView) convertView.findViewById(R.id.prix);
        viewHolder.qte=(EditText) convertView.findViewById(R.id.qte);
        viewHolder.cb=(CheckBox)convertView.findViewById(R.id.cbarticle);





        convertView.setTag(viewHolder);

    }
    else {
        viewHolder =(ViewHolder)convertView.getTag();
    }
    viewHolder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox check=(CheckBox)view;
                check.requestFocus();
                if (check.isChecked())
                {
                    Article a =articles.get((int)check.getTag());
                    Livraison_detail l=new Livraison_detail();
                    l.CODE_ARTICLE=a.CODE_ARTICLE;
                    l.PRIX_ACHAT=a.PRIX_VENTE;
                    try
                    {
                        if(qtes[(int)check.getTag()].equals(""))
                        {
                            throw new Exception();
                        }else {
                            if (Double.parseDouble(qtes[(int)check.getTag()])>a.QTE_CHARGE) throw  new QuantitéInsuffisanteException();
                            else{
                                l.QTE_LIVRE=Double.parseDouble(qtes[(int)check.getTag()]);
                                MAJdb.add(l);
                            }
                        }
                    }
                    catch (QuantitéInsuffisanteException e)
                    {
                        new AlertDialog.Builder(getContext())
                                .setTitle("Quantité Insuffisante")
                                .setMessage("la quantité saisie est superieur que celle du Stock il y a "+a.QTE_CHARGE+" artcile Disp")
                                .setCancelable(true)
                                .setIcon(R.drawable.alert)
                                .setNeutralButton("Compris",null )
                                .show();
                    }
                    catch (Exception e)
                    {
                        Toast.makeText(getContext(), "Il faut toucher le fin bouton du clavier puis re-cochez", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Article a2=articles.get((int)check.getTag());

                    for (Livraison_detail l:MAJdb) {
                        if (l.CODE_ARTICLE.equals(a2.CODE_ARTICLE))
                        {
                            MAJdb.remove(l);
                        }
                    }
                }
            }
        });

        viewHolder.qte.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final int position = view.getId();
                final EditText Caption = (EditText) view;
                qtes[position]=Caption.getText().toString();
                if(Caption.isFocused()) {Caption.clearFocus();}
            }
        });

    Article article=articles.get(position);
    if(article !=null)
    {
        viewHolder.designation.setText(article.DESIGNATION);
        viewHolder.prix.setText(Double.toString(article.PRIX_VENTE));
        viewHolder.quantité.setText(Integer.toString(article.QTE_CHARGE));
    }


    return convertView;
}



private class ViewHolder
{
    TextView designation;
    TextView quantité;
    TextView prix;
    EditText qte;
    CheckBox cb;
}