我在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;
}
之前
答案 0 :(得分:0)
当滚动ListView时,它将调用方法getView()
,因此您需要将文本设置为EditText,因此您需要做的是:
TxtStock.setText(valueList[pos]);
答案 1 :(得分:0)
首先在ListView中使用EditText并不是一个好主意!但如果你必须这样做,你可以尝试一下..
像这样改变你的代码..
@Override
public Object getItem(int position) {
return data.get(position); }
并且在getView(..)方法中,您可以回收视图以及导致数据丢失的原因。做这个改变..
移动此行` TxtStock.setText(Result.get(InputActivity.INITIAL_STOCK)); 到getView(..)
里面的if..else条件之外希望这会有所帮助..当 convertView为空时只能查找ViewById,并且当你回收观点。谢谢!! ; d