【问题标题】:EditText in ListView Loses value when scroll up and downListView 中的 EditText 在上下滚动时丢失值
【发布时间】:2015-02-05 07:29:08
【问题描述】:

我在 ListView 的EditText 中输入了值,但是当我上下滚动时,这些值不是焦点并丢失。请帮我。我是新手。

我试过TextWatcher,并将数据保存到array,但问题是ListView中返回的视图位置并不总是正确的,所以我从数组中丢失了一些数据。

这是我的代码

public class ListViewAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater;
private View itemView;
HashMap<String, String> Result = new HashMap<String, String>();
private Bitmap ImageCover;

private TextView bookname;
private TextView bookcode;
private TextView index;
private ImageView bookcover;
private String BookIDSend;
private String QtySend;
private EditText TxtStock;
private String[] valueList;
private boolean InputState;

public ListViewAdapter(Context context, ArrayList<HashMap<String, String>> arraylist, boolean state) {
    this.context    = context;
    data            = arraylist;
    valueList       = new String[data.size()];
    InputState      = state;
}
@Override
public int getCount() { 
    return data.size();
}
@Override
public Object getItem(int position) {
    return position; }

@Override
public long getItemId(int position) { 
    return position; }

public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        inflater    = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView    = inflater.inflate(R.layout.input_list_item, parent, false);
        if ( ( position % 2 ) == 0 ) {
            itemView.setBackgroundColor(Color.rgb(250,235,215));
        } else {
            itemView.setBackgroundColor(Color.rgb(255,235,205));
        }
        Result                  = data.get(position);
        ClassFile FileName      = new ClassFile();
        String AppDirectory     = FileName.GetAppDirectory(context);
        String Directory        = AppDirectory+"/cover/";

        index                   = (TextView) itemView.findViewById(R.id.numb);
        bookname                = (TextView) itemView.findViewById(R.id.book_name);
        bookcode                = (TextView) itemView.findViewById(R.id.code_book_input);
        bookcover               = (ImageView) itemView.findViewById(R.id.imgCover);
        TxtStock                = (EditText) itemView.findViewById(R.id.EdtIsi);
        TxtStock.setText(Result.get(InputActivity.INITIAL_STOCK));

        final int pos   = position;
        valueList[pos]  = Result.get(InputActivity.INITIAL_STOCK);
        if (InputState){
            TxtStock.setEnabled(true);
        }else{
            TxtStock.setEnabled(false);
        }
        TxtStock.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {
            }
            public void afterTextChanged(Editable s) {
                valueList[pos] = s.toString().trim();
            }
        });
        bookname.setText(Result.get(InputActivity.BOOKNAME));
        bookcode.setText(Result.get(InputActivity.BOOK_CODE));
        ClassImage Procimg      = new ClassImage();
        ImageCover              = Procimg.decodeSampledBitmapFromUri( Directory + Result.get(InputActivity.BOOKCOVER), 90, 120);
        bookcover.setImageBitmap(ImageCover);
    }else {
        itemView    = convertView;
    }

    int j = position+ 1;
    index.setText("" + j);
    return itemView;
}

感谢之前

【问题讨论】:

  • 我想指出一个错误,在getItem方法中你必须返回数据[位置]......public Object getItem(int position) { return data[position]; }
  • 错误先生。它说“表达式的类型必须是数组”@Ranjith
  • return data.get(position); 数据是一个 ArrayList。
  • 什么都没发生,先生@iForests
  • 哦..我的错..我的意思是 data.get[position]...@iForests 感谢您的纠正..

标签: android listview scroll android-edittext


【解决方案1】:

ListView滚动时会调用getView()方法,所以需要给你的EditText设置文字,所以你需要做的是:

TxtStock.setText(valueList[pos]);

【讨论】:

    【解决方案2】:

    首先在 ListView 中使用 EditText 不是一个好主意!但如果你必须这样做,你可能会试一试..

    像这样改变你的代码..

    @Override
    public Object getItem(int position) {
    return data.get(position); }
    

    在 getView(..) 方法中,您正在回收视图,这就是您丢失数据的原因。进行此更改..

    移动这一行`TxtStock.setText(Result.get(InputActivity.INITIAL_STOCK)); 到 getView(..) 内的 if..else 条件之外

    希望这会有所帮助..仅当 convertView 为 null 时 findViewById 并且所有其他 set 方法都应在 if..else 条件之外调用看法。谢谢!! ;D

    【讨论】:

      猜你喜欢
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 2014-12-18
      相关资源
      最近更新 更多