ListView の EditTexts から値を取得しようとしていますが、うまくいきません。afterTextChanged メソッドで「lista」の配列に設定された値が明らかに失われています。私は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 のすべての EditText から値を取得する方法 Bu 今、他の問題に直面しました: リストをロールダウンすると、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 に値を設定すると、この値は、リストをロールダウンした後、リストの 3 行目の EditText に設定されます。 .