當顯示在視圖中時,如果我滾動通過第一個EditText
框,則我在第一個和最後一個EditText
框中都會顯示我輸入最後一個EditText
的值。當將值保存到數據庫中時,它只保存最後的EditText
值,但是如預期的那樣。ListView中的第一個和最後一個EditText顯示相同的值
是什麼導致了這種行爲?這裏是我的完整代碼:
package com.example.expensemanager1;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
final int i;
LayoutInflater mInflater;
public InteractiveArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.spared_list_row, list);
this.context = context;
this.list = list;
i=list.size();
notifyDataSetChanged();
}
static class ViewHolder {
protected TextView membname;
protected EditText membamount;
}
public class MyTextWatcher implements TextWatcher
{
private EditText editText;
Model elemente;
public MyTextWatcher(EditText editText, Model m)
{
this.editText = editText;
this.elemente = m;
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Log.d("TEST", "In afterTextChanged");
editText.setFocusable(true);
String ssss= s.toString();
if(editText.getText().toString().length()>0)
list.get((Integer)editText.getTag()).setamount(Double.parseDouble(s.toString()));
else
list.get((Integer)editText.getTag()).setamount(0);
double sum=0.0;
for (int j=0; j<i ; j++)
{
Model m1= list.get(j);
sum += m1.getamount();
}
Log.d("TEST", "sum" +sum);
double balance = New_spared_list.getTextView();
Log.d("TEST", "balance = "+ balance);
sum = balance - sum;
New_spared_list.setX(sum);
Log.d("TEST", "sum" +sum);
}
@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) {
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
final Model elemente = list.get(position);
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.spared_list_row, null);
viewHolder.membname = (TextView) convertView.findViewById(R.id.membersTextView2);
viewHolder.membamount = (EditText) convertView.findViewById(R.id.amount);
final ViewHolder holder = viewHolder;
viewHolder.membamount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something
LinearLayout parent = (LinearLayout) v.getParent();
EditText membamount = (EditText) parent.findViewById(R.id.amount);
membamount.setFocusable(true);
membamount.setFocusableInTouchMode(true);
membamount.requestFocus();
}
});
viewHolder.membamount.addTextChangedListener(new MyTextWatcher(viewHolder.membamount, (Model) viewHolder.membamount.getTag()));
convertView.setTag(viewHolder);
convertView.setTag(R.id.membersTextView2, viewHolder.membname);
convertView.setTag(R.id.amount, viewHolder.membamount);
// viewHolder.membamount.setTag(list.get(position));
// viewHolder.membamount.setHint(String.valueOf((list.get(position).getamount())));
} else {
viewHolder = (ViewHolder) convertView.getTag();
((ViewHolder) convertView.getTag()).membamount.setTag(list.get(position));
}
viewHolder.membamount.setTag(position);
if(position % 2 == 0){
convertView.setBackgroundColor(Color.rgb(231, 247, 253));
}
// viewHolder.membamount.setTag(list.get(position));
viewHolder.membamount.setHint(String.valueOf(list.get(position).getamount()));
viewHolder.membname.setText(list.get(position).getName());
return convertView;
}
}
這是listview的常見行爲,它重用您的意見。所以你的edittext不是一個新的,它是一樣的。你應該設置你的edittext的文本以避免這種情況。 – yahya