我在ListView中輸入了EditText
的值,但是當我向上和向下滾動時,值不會聚焦並丟失。請幫幫我。我是新手。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;
}
THX前
我想指出一個錯誤,在方法getItem中你必須返回數據[position] .........'public Object getItem(int position){ return data [position]; }' – Psypher
錯誤先生。它表示「表達式的類型必須是數組」@Ranjith –
'return data.get(position);'data是一個ArrayList。 – iForests