EditText in listview repeats the value(列表视图中的 EditText 重复值)
问题描述
列表中有三个文本视图和一个编辑文本.当我在编辑文本中输入任何值时,相同的值也会复制到另一个编辑文本中.我厌倦了同时使用 onFocusChangedListener 和 onTextChanged 实现,但两种情况下的问题都是一样的.
I have three textviews and one edit text in the list. when I entered any value in the edittext, the same value is also copied in the other edittext. I tired implementing with both onFocusChangedListener and onTextChanged but the problem is same in both the cases.
public class ProductListAdapter extends BaseAdapter {
Context context;
public ArrayList<Products> prodList;
private static LayoutInflater inflater = null;
public ProductsList productListActivity;
public ProductListAdapter(Context context,ArrayList<Products> prodList) {
this.context = context;
this.prodList = prodList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.e("In adapter", "yes");
}
@Override
public int getCount() {
return prodList.size();
}
@Override
public Products getItem(int position) {
return prodList.get(position); }
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView( final int position, View convertView, ViewGroup parent) {
Log.i(" in prodlist adapter","View holder");
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.productslistviewadapter, parent, false);
holder = new ViewHolder();
holder.tvdrCode = (TextView) convertView.findViewById(R.id.tvname);
holder.tvDrName = (TextView) convertView.findViewById(R.id.tvprodpack);
holder.tvterrcode= (TextView) convertView.findViewById(R.id.textView3);
holder.caption = (EditText)convertView.findViewById(R.id.editText1);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Products p = prodList.get(position);
holder.tvdrCode.setText(p.getDocCode());
holder.tvDrName.setText(p.getDocName());
holder.tvterrcode.setText(p.getAdr());
//holder.caption.setText(prodList.get(position).caption);
/* holder.caption.setId(position);
holder.caption.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
final int position = v.getId();
final EditText Caption = (EditText) v;
// prodList.get(position)= Caption.getText().toString();
String d = prodList.get(position).getDocCode();
Log.e("dr code",d);
}
}
}); */
holder.caption.setTag(position);
// holder.caption.setText(ProdList.get(position).toString());
int tag_position=(Integer) holder.caption.getTag();
holder.caption.setId(tag_position);
holder.caption.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,int before, int count) {
//2nd method String d = planArraylist.get(position).getDocCode();
final int position2 = holder.caption.getId();
final EditText Caption = (EditText) holder.caption;
if(Caption.getText().toString().length()>0){
String d = prodList.get(position2).getDocCode();
Log.e("dr code",d);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
return convertView;
}
static class ViewHolder {
TextView tvdrCode;
TextView tvDrName;
TextView tvterrcode;
EditText caption;
}
}
xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animationCache="false"
android:scrollingCache="false"
android:smoothScrollbar="true" >
<!-- android:descendantFocusability="beforeDescendants" -->
</ListView>
推荐答案
我们知道 ListView 在滚动整个列表时会重用 ListItem 的视图.
As we know ListView reuses the view of ListItem as we scroll the whole list.
所以当我们有一个带有 EditText 的自定义 ListView 时就会出现问题,如果我们在第一个 EditText 中输入任何值并开始滚动,那么当我们滚动 listview 时,一个 EditText 的值会被一个一个地复制到另一个 EditTexts 中.
So problem arises when we have a custom ListView with EditText where if we enter any value in the first EditText and start scrolling then the value of EditText one is copied to another the EditTexts one by one as we scroll the listview .
当列表视图重用视图时发生这种情况,并且当另一个视图中的另一个列表项(即未看到的视图向上滚动)时,它会重用旧列表视图,因此该视图的旧值会在新的编辑文本中看到.
This happens as the listview reuses the view and as the other listitem from another view i.e. the view which is not seen scrolls upwards it reuses the old lists view and hence the old value of that view is seen in the new edittext.
http://www.webplusandroid.com/creating-listview-with-edittext-and-textwatcher-in-android/
这篇关于列表视图中的 EditText 重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:列表视图中的 EditText 重复值
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01