3

我正在尝试从 ListView 中的 EditTexts 获取值,但它不起作用。在 afterTextChanged 方法中设置为我的“列表”数组的值显然丢失了。我是android编程新手,有人可以帮助我吗?(抱歉英语不好)这是我的适配器中 getView 的代码:

@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.checkbox = (CheckBox) 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.checkbox.setOnCheckedChangeListener(null);
    holder.checkbox.setChecked(getItem(position).Selecionado());
    holder.checkbox.setTag(new LinhaItemTag(getItem(position), position));

    holder.checkbox.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;
}

}

我设法从 EditTexts 中获取值查看这篇文章:如何从 ListView Bu 中的所有 EditText 中检索值,我面临其他问题:当我向下滚动列表时,来自 EditTexts 的值丢失了。这就像我的代码现在的样子:

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 CheckBox checkbox;
    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.checkbox = (CheckBox) 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.checkbox.setOnCheckedChangeListener(null);
    holder.checkbox.setChecked(getItem(position).Selecionado());
    holder.checkbox.setTag(new LinhaItemTag(getItem(position), position));

    holder.checkbox.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;
}

}

我换行:

holder.edit.setText("");

至:

//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])**);

但这并不像我预期的那样工作,例如,如果我为 ListView 第一行的 EditText 设置一个值,则在我向下滚动列表后,该值被设置为列表第三行中的 EditText。 .

4

4 回答 4

16

要从EditText您的 中获取值ListView,您可以在您的活动中执行此操作。您必须需要某些视图单击或其他内容的值。因此,每当发生这种情况时,只需编写以下代码

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

在这里,上面的代码将为您EditText提供位于position您的ListView. 当所有的检查都被检查时,您似乎需要 EditTexts 的值的总和?你可以使用这样的东西

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);
}
于 2012-12-25T17:27:06.860 回答
1

适配器类 getView() 方法的变化看起来像。

 @Override
public View getView(final int position, View view, ViewGroup parent) {

    TextView txt_id, txt_courny_symbol, txt_member_view;
    EditText edit_member_amount;

    final int posicao = position;
    Log.v("ConvertView", String.valueOf(posicao));


    inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.list_member_layout, null);
    txt_id = (TextView) view.findViewById(R.id.txt_id);
    txt_courny_symbol = (TextView) view.findViewById(R.id.txt_courny_symbol);
    edit_member_amount = (EditText) view.findViewById(R.id.edit_member_amount);

    edit_member_amount.setTag(new LinhaItemTag(getItem(posicao), posicao));
    edit_member_amount.addTextChangedListener(new MeuTextWatcher(edit_member_amount));

    if (al_contact.size() != 0) {
        MemberClass memberClass = al_contact.get(position);
        txt_id.setText(memberClass.getName());
        txt_courny_symbol.setText(sym);
    }

    return view;

}

擦除我们的 ViewHolder 类

于 2016-07-22T04:53:16.297 回答
0

您好,您可以使用下面的代码来执行此操作,listview 无法将控制器状态保存在行上,因此您需要使用控件的 settag 和 gettag。

来源:https ://dl.dropbox.com/u/68130108/ListViewEditItem.rar

http://i.stack.imgur.com/DqKv3.png

于 2012-12-25T13:04:49.217 回答
0

到目前为止,从 EditTexts 中丢失值可能会很烦人。我在工作时找到了这个解决方案。

在您的 android 清单中,选择您的列表视图所在的活动。然后将这个代码块放在那里。

android:windowSoftInputMode="adjustPan"

然后转到您的ListView, 并导航到 xml 布局并将此块放在那里。

android:descendantFocusability="beforeDescendants"
于 2017-08-02T10:25:49.530 回答