嗨,我知道这个问题已经出现在这里,但答案对我没有帮助。好吧,我有一个活动,列表视图绑定到ArrayListAdapter为我的班级ZamowieniePoz。
public class ZamowieniePoz {
private int _idZamowienie;
private String _plu;
private String _nazwa;
private String _ilosc;
private String _jm;
private String _uwagi;
private String _dataSync;
private int _isSync; }
这是我的listView定义:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
<ListView
android:id="@+id/listView"
android:scrollbars="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_vertical"
android:layout_alignParentTop="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/listView" >
<Button
android:id="@+id/btnWyslij"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="WYŚLIJ ZAMÓWIENIE" />
<Button
android:id="@+id/btnPowrotZamowienieAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="POWRÓT" />
</LinearLayout>
这是listView行:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtPlu"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/txtIlosc"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/txtPlu"
android:layout_marginLeft="25sp"
android:lines="1"
android:inputType="numberDecimal" />
</RelativeLayout>
这是ArrayListAdapter:
public class ZamowieniePozArrayAdapter extends ArrayAdapter<ZamowieniePoz> {
Context context;
int layoutResourceId;
ArrayList<ZamowieniePoz> data=null;
public ZamowieniePozArrayAdapter(Context context, int layoutResourceId, ArrayList<ZamowieniePoz> data) {
super(context, layoutResourceId, data);
this.layoutResourceId=layoutResourceId;
this.context=context;
this.data = data;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ZamowienieHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row=inflater.inflate(layoutResourceId, parent, false);
holder = new ZamowienieHolder();
holder.txtPlu = (TextView)row.findViewById(R.id.txtPlu);
holder.txtIlosc=(EditText)row.findViewById(R.id.txtIlosc);
holder.txtIlosc.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
data.get(position).setIlosc(s.toString());
}
});
row.setTag(holder);
}
else{
holder = (ZamowienieHolder)row.getTag();
}
ZamowieniePoz zamowieniePoz = data.get(position);
holder.txtPlu.setText(zamowieniePoz.getPlu());
holder.txtIlosc.setText(zamowieniePoz.getIlosc());
return row;
}
static class ZamowienieHolder {
TextView txtPlu;
EditText txtIlosc;
}
}
这就是我将适配器绑定到listView的方式:
final DbHandler db = new DbHandler(ZamowienieAddActivity.this);
listView = (ListView) findViewById(R.id.listView);
arrayZamowieniePoz = db.getAllZamowienie();
arrayAdapterZamowieniePoz = new ZamowieniePozArrayAdapter(this, R.layout.zamowienie_add_list_items, arrayZamowieniePoz);
listView.setAdapter(arrayAdapterZamowieniePoz);
就是这样。问题是listView的第一行重复了最后一行。我认为这行代码data.get(position).setIlosc(s.toString());在afterTextChange中的getView中。
请帮帮我,我不知道有什么不对。谢谢。
答案 0 :(得分:0)
使用您的arrayAdaper的Bellow代码。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding_small"
android:layout_marginTop="@dimen/padding_small" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="@dimen/padding_large"
android:paddingTop="@dimen/padding_small"
android:text="Rename To: "
android:textColor="@color/black"
android:textStyle="bold" >
</TextView>
<EditText
android:id="@+id/ZoneRenameEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/relay_txtbox"
android:ems="2"
android:maxLength="50"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:singleLine="true"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:gravity="center_vertical|right" >
<ImageView
android:id="@+id/ZoneDoneImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/padding_large"
android:background="@drawable/bk_done_button" />
</LinearLayout>
答案 1 :(得分:0)
您的TextWatcher
与行View
如果您获得了回收的视图(row != null
),请参阅以下代码
@Override
public void afterTextChanged(Editable s) {
data.get(position).setIlosc(s.toString());
}
position
仍然会引用原始视图的位置。
您需要为每次迭代重置TextWatcher
,确保在再次致电addTextChangeListener()
之前致电removeTextChangeListener()
。
可以像这样完成一个简单的实现,尽管每次实例化TextWatch可能不是最佳的:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ZamowienieHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ZamowienieHolder();
holder.txtPlu = (TextView)row.findViewById(R.id.txtPlu);
holder.txtIlosc = (EditText)row.findViewById(R.id.txtIlosc);
row.setTag(holder);
}
else {
holder = (ZamowienieHolder)row.getTag();
}
// Remove previous TextWatcher if any
if (holder.textWatcher != null) {
holder.txtIlosc.removeTextChangedListener(holder.textWatcher);
}
// Create the TextWatcher corresponding to this row
holder.textWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
data.get(position).setIlosc(s.toString());
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
});
holder.txtIlosc.addTextChangedListener(holder.textWatcher);
ZamowieniePoz zamowieniePoz = data.get(position);
holder.txtPlu.setText(zamowieniePoz.getPlu());
holder.txtIlosc.setText(zamowieniePoz.getIlosc());
return row;
}