Android – get EditText value from listview

I tried to get a value from edittexts in listview, but it didn't work. The value set to 'lista' array in the aftertextchanged method has obviously been lost. I'm a novice in Android programming. Can someone help me? (sorry for poor English) the following is the code of getview in the adapter:

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

    final int posicao = position;

    if (convertView == null) {
        LayoutInflater inflater = contexto.getLayoutInflater();
        convertView = inflater.inflate(R.layout.produtos, null);
        holder = new ViewHolder();
        holder.texto = (TextView) convertView.findViewById(R.id.txtDescricao);
        holder.check@R_785_2419@ = (Check@R_785_2419@) convertView.findViewById(R.id.chkProduto);
        holder.edit = (EditText) convertView.findViewById(R.id.txtValor);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.texto.setText(getItem(position).getTexto());     

    holder.edit.setText("0");
    holder.edit.setTag(new LinhaItemTag(getItem(position), position));  

//here a get an exception just setting the focus on edit
holder.edit.setOnFocuschangelistener(new OnFocuschangelistener() {
        public void onFocusChange(View v, boolean hasFocus) {
            int posicao = v.getId();
            EditText e = ((EditText) v);
            if (!e.getText().toString().equals("")) {
                if (hasFocus) {
                    if(e.getText().toString().length() != 0)
                        lista.get(posicao).setValor(Double.parseDouble(e.getText().toString()));
                }
            }

        }
    });

    holder.edit.addTextChangedListener( new TextWatcher() {             

        public void afterTextChanged(Editable s)
        {           

            if(s.length() != 0) 
            {
                lista.get(posicao).setValor(Double.parseDouble(s.toString()));              
            }


        }

        @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) {
            // TODO Auto-generated method stub

        }
    }); 


    holder.check@R_785_2419@.setOnCheckedchangelistener(null);
    holder.check@R_785_2419@.setChecked(getItem(position).Selecionado());
    holder.check@R_785_2419@.setTag(new LinhaItemTag(getItem(position), position));

    holder.check@R_785_2419@.setOnCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            LinhaItemTag elemento = (LinhaItemTag) buttonView.getTag();
            elemento.item.setSelecionado(isChecked);

            if (isChecked) {
                pegos[elemento.position] = true;                
            } else {
                pegos[elemento.position] = false;
            }
            boolean checados = true;
            for (int i = 0; i < lista.size(); i++) {
                if (!pegos[i]) {
                    checados = false;
                    break;
                }
            }
            if (checados) {
                for(int i = 0; i < lista.size(); i++)
                {                   
                    total += lista.get(i).getValor();
                }
                Toast.makeText(contexto, "Compra finalizada - Valor Total: " + total, Toast.LENGTH_LONG).show();
            }
        }
    });

    return convertView;
}

}

I managed to see the value of this article from edittexts: how to retrieve value from all EditText in listview now, I have other problems: when I scroll the list, the value in edittexts is lost. It's like my code now looks like:

public class MeuAdapter extends ArrayAdapter<LinhaItem> {

private final List<LinhaItem> lista;
private final Activity contexto;
private final boolean[] pegos;
private final double[] valores;
double total;

public MeuAdapter(Activity contexto, List<LinhaItem> lista) {
    super(contexto, 0, lista);
    this.contexto = contexto;
    this.lista = lista;
    pegos = new boolean[lista.size()];
    valores = new double[lista.size()];
    for (int i = 0; i < lista.size(); i++) {
        pegos[i] = false;
        valores[i] = 0;
    }
    total = 0;
}

public class ViewHolder
{
    protected TextView texto;
    protected Check@R_785_2419@ check@R_785_2419@;
    protected EditText edit;
}

public class LinhaItemTag {

    LinhaItem item;
    int position;

    LinhaItemTag(LinhaItem item, int position) {
        this.item = item;
        this.position = position;
    }
}

private class MeuTextWatcher implements TextWatcher {

    private View v; 

    public MeuTextWatcher(View v)
    {
        this.v = v;     
    }

    @Override
    public void afterTextChanged(Editable e) {
        String s = e.toString();
        LinhaItemTag lTag = (LinhaItemTag) v.getTag();      
        if(s.length() != 0)
        {           
            valores[lTag.position] = Double.parseDouble(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) {
        // TODO Auto-generated method stub

    }

}

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

    if (convertView == null) {
        LayoutInflater inflater = contexto.getLayoutInflater();
        convertView = inflater.inflate(R.layout.produtos, null);
        holder = new ViewHolder();
        holder.texto = (TextView) convertView.findViewById(R.id.txtDescricao);
        holder.check@R_785_2419@ = (Check@R_785_2419@) convertView.findViewById(R.id.chkProduto);
        holder.edit = (EditText) convertView.findViewById(R.id.txtValor);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.texto.setText(getItem(position).getTexto()); 

    holder.edit.setText("");
    holder.edit.setTag(new LinhaItemTag(getItem(position), position));      

    holder.edit.addTextChangedListener(new MeuTextWatcher(holder.edit));    


    holder.check@R_785_2419@.setOnCheckedchangelistener(null);
    holder.check@R_785_2419@.setChecked(getItem(position).Selecionado());
    holder.check@R_785_2419@.setTag(new LinhaItemTag(getItem(position), position));

    holder.check@R_785_2419@.setOnCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            LinhaItemTag elemento = (LinhaItemTag) buttonView.getTag();
            elemento.item.setSelecionado(isChecked);

            if (isChecked) {
                pegos[elemento.position] = true;                
            } else {
                pegos[elemento.position] = false;
            }
            boolean checados = true;
            for (int i = 0; i < lista.size(); i++) {
                if (!pegos[i]) {
                    checados = false;
                    break;
                }
            }
            String vlrs = "";
            if (checados) {
                for(int i = 0; i < lista.size(); i++)
                {                   
                    total += valores[i];                    
                }
                Toast.makeText(contexto, "Total da compra: " + total, Toast.LENGTH_LONG).show();
            }
        }
    });

    return convertView;
}

}

I changed this line:

holder.edit.setText("");

To:

//the valores array is where i store the values in the afterTextChanged method in the TextWatcher class implementation.
holder.edit.setText(**Double.toString(valores[position])**);

But this doesn't work as I expected. For example, if I set a value for EditText in the first row of listview, after I scroll the list, this value is set to EditText in the third row of the list

resolvent:

To get the value from EditText in listview, you can do this in the activity. You must click on some views or something needs a value. So as long as this happens, just write the following code

View view=listView.getChildAt(position);
EditText editText=view.findViewById(R.id.editText);
String string=editText.getText().toString();

Here, the above code will provide you with the EditText text existing in the listview location. After checking all the contents, you seem to need the sum of the edittexts values? You can use something like this

for(int i=0;i<items.size();i++){
    View view=listView.getChildAt(i);
    EditText editText=view.findViewById(R.id.editText);
    String string=editText.getText().toString();
    if(!string.equals(""))
         total+=Double.parseDouble(string);
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>