How to implement listener text changes in custom ListView with edittexts?

若如初见. 提交于 2019-12-13 04:55:25

问题


im trying to save the values of Edittext in the ListView and it works, but when there is a ListView too large, when Im editing 1 edittext, if I scroll the listview, another EditText is editing at the same time... I don't know what to do, I wasted 6hours, and got nothing. If some can help me, please.

This is the code of the getView of the Custom Listview

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int position2 = position;
        View v = convertView;
        final ViewHolder holder;

        if(v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.lista_item_prepedido, null);

            holder = new ViewHolder();

            holder.codi = (TextView) v.findViewById(R.id.NomProducte);
            holder.nom = (EditText) v.findViewById(R.id.Preu);
            holder.eliminar = (ImageButton) v.findViewById(R.id.delete);
            View eliminarPrepedido = (ImageButton) v.findViewById(R.id.delete);

            if(blocClients.equals("0"))
            {
                eliminarPrepedido.setVisibility(View.GONE);
                holder.nom.setFocusable(false);
            }


            v.setTag(holder);

            holder.nom.setText(items.get(position).getNom());
            holder.nom.addTextChangedListener(new TextWatcher(){

                @Override
                public void afterTextChanged(Editable s) {


                }

                @Override
                public void beforeTextChanged(CharSequence s, int start,
                        int count, int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onTextChanged(CharSequence s, int start,
                        int before, int count) {



                    items.get(position2).Nom = s.toString();



                    String preu = items.get(position2).Nom;
                    String nomproducte = items.get(position2).Codic;
                    int position = position2;

                    //mostrarMensaje("Valor: "+preu+"  Nom Producte: "+nomproducte+"  CodiClient: " +CodicClient+ " posicio: "+position);
                    baseDatos = openOrCreateDatabase(nombreBD, MODE_WORLD_WRITEABLE, null);

                    String sqlStr = "UPDATE estadistiques SET ultimpreu = '" +preu+"' WHERE codic_client='"+CodicClient+"' AND desc_article = '"+nomproducte+"' ";
                    baseDatos.execSQL(sqlStr);

                    try {
                        fnGlobal.CreaSincroFitxer(sqlStr);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    baseDatos.close();
                }


            });


        } else {
            holder = (ViewHolder) v.getTag();

        }


        holder.codi.setText(items.get(position).getCodic());


        holder.eliminar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                final int position = position2;
                String CodicClient = null;
                Bundle extras = getIntent().getExtras();
                CodicClient = extras.getString("codiClientTab");
                String nomproducte = items.get(position).Codic;
                int Opcio = 1;


                baseDatos = openOrCreateDatabase(nombreBD, MODE_WORLD_WRITEABLE, null);
                String sqlStr = "DELETE FROM estadistiques WHERE codic_client ='"+CodicClient+"' AND desc_article ='"+nomproducte+"'" ;
                baseDatos.execSQL(sqlStr);

                try {
                    fnGlobal.CreaSincroFitxer(sqlStr);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                baseDatos.close();

                notifyDataSetChanged();

                onResume();

            }

        });





        return v;
    }
}

Custom XML item for listview

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="@drawable/rounded_rectangle"
    android:orientation="horizontal"
    android:padding="6dip" >

    <ImageButton
        android:id="@+id/delete"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="15dp"
        android:background="@drawable/delete" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <EditText
            android:id="@+id/Preu"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/NomProducte"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:background="@color/white"
            android:ellipsize="end"
            android:gravity="right"
            android:inputType="numberDecimal"
            android:singleLine="true"
            android:text="nom"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/NomProducte"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/Preu"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"
            android:layout_weight="0.16"
            android:clickable="false"
            android:ellipsize="end"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:singleLine="true"
            android:text="codi"
            android:textColor="#000000"
            android:textSize="18sp" />

    </RelativeLayout>

</LinearLayout>

Thanks a lot! :)


回答1:


If I've understood your problem, it would be very hard to make this always behave in an easy way to understand. I would suggest that you change the way you implement this to have two TextViews in the ListView showing the two piece of information. When the editable one is tapped, bring up a Dialog to edit the information, which the user must then either accept or cancel before the move the listview, making it clear what is going on. In summary, I suggest:

  • NomProducte become a TextView which is set up as now.
  • Catch onTouch of NomProducte to bring up a dialog showing the image, Preu text as TextView and NomProducte text as an EditText.
  • The user enters the data into that one.
  • OK -> Accept the new text actions
  • Cancel -> Ignore the new text actions.


来源:https://stackoverflow.com/questions/15408464/how-to-implement-listener-text-changes-in-custom-listview-with-edittexts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!