我的ListView中有两个EditTexts。我想要的只是向EditTexts添加TextWatcher,因此我可以避免在滚动时复制和更改其中的值。我可以成功地将TextWatcher添加到其中一个EditTexts,但无法实现两个EditTexts。我尝试了stackoverflow的其他答案。但我没有得到理想的输出。 这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.orders);
list = (ListView)findViewById(R.id.list1);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
SelectItems();
ViewItems();
}
public List<Map<String, String>> SelectItems() {
// TODO Auto-generated method stub
try {
datas = new ArrayList<Map<String, String>>();
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp"
+ " from itemmaster", null);
c.moveToFirst();
while (c.moveToNext()){
datanums.put("name",c.getString(c.getColumnIndex("im_desc")));
datanums.put("code", c.getString(c.getColumnIndex("im_code")));
datanums.put("imsrp",c.getString(c.getColumnIndex("im_srp")));
datas.add(datanums);
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
return datas;
}
public void ViewItems() {
// TODO Auto-generated method stub
arrTemp = new String[datas.size()];
MyListAdapter myListAdapter = new MyListAdapter();
list.setAdapter(myListAdapter);
}
public class MyListAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
if(datas != null && datas.size() != 0){
return datas.size();
}
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//ViewHolder holder = null;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = Orders.this.getLayoutInflater();
convertView = inflater.inflate(R.layout.order_simple_row, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.name);
holder.textView2 = (TextView) convertView.findViewById(R.id.srp);
holder.textview3 = (TextView) convertView.findViewById(R.id.code);
holder.editText1 = (EditText) convertView.findViewById(R.id.cases);
holder.editText2 = (EditText) convertView.findViewById(R.id.pcs);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
String [] names = new String[550];
String [] codes = new String[550];
String [] prize = new String[550];
DatabaseHelper dbHelper = new DatabaseHelper(Orders.this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp "
+ " from itemmaster", null);
Log.v("item detailss", c.toString());
c.moveToFirst();
while (c.moveToNext()){
String cod = c.getString(c.getColumnIndex("im_code"));
codes[c.getPosition()] =cod;
String desc1 = c.getString(c.getColumnIndex("im_desc"));
names[c.getPosition()] = desc1;
String price = c.getString(c.getColumnIndex("im_srp"));
prize[c.getPosition()] = price;
}
newDB.close();
holder.textView1.setText(names[position+1]);
holder.textview3.setText(codes[position+1]);
holder.textView2.setText(prize[position+1]);
holder.editText1.setText(arrTemp[holder.ref]);
holder.editText2.setText(arrTemp[holder.ref]);
holder.editText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
arrTemp[holder.ref] = arg0.toString();
datas.get(position).put("pref", holder.editText1.toString().trim());
}
});
return convertView;
}
private class ViewHolder {
TextView textView1,textView2,textview3;
EditText editText1,editText2;
public int ref;
}
}
请帮我正确地做到这一点。
答案 0 :(得分:0)
如果要执行,请为每个编辑文本使用不同的textwatcher 每个编辑文本的不同操作
EditText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
也适用于其他EditText
答案 1 :(得分:0)
最近我遇到了类似的问题,这是我如何使用HashMap解决它:
在适配器构造函数中,初始化HashMap并确保清除它:
public MyAdapter(Context context, String[] texts){
this.context = context;
this.texts = texts;
hashMapTexts = new HashMap<String,String>();
for(String text: texts){
hashMapTexts.put(text, "");
}
然后,在适配器中更正您的私有类ViewHolder:
private class ViewHolder {
private TextView listItemTextView;
private EditText listItemEditText;
private int position;
public ViewHolder(View view) {
listItemTextView = (TextView)view.findViewById(R.id.list_item_text_view);
listItemEditText = (EditText)view.findViewById(R.id.list_item_edit_text);
}
}
然后,在覆盖的getView()方法中初始化ViewHolder:
if(convertView == null){
convertView = View.inflate(context, R.layout.list_item, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
}
以相同的方法获取ViewHolder的位置:
viewHolder.position = position;
设置视图的文本:
viewHolder.listItemTextView.setText(texts[position]);
viewHolder.listItemEditText.setText(hashMapTexts.get(texts[position]));
现在使用TextWatcher:
viewHolder.listItemFormEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
hashMapTexts.put(texts[viewHolder.position], s.toString());
}
});
return convertView;
我建议你从像我这样的简单案例开始,然后将其应用到你的特定场景中。
答案 2 :(得分:-1)
et1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});