我在editText中已经没有聚焦的情况下获取文本的最后一个值时遇到问题。当我第一次输入一个值并且它没有聚焦时我得到最后一个值我输入,但第二次等等,每当我更改editText内的iputed值时,它不在焦点,它应该得到我输入的值,但它会更改为我输入的最后一个值,但保存到数据库时它保存的值我真的感觉到了那种感觉。我知道我很难理解我在这里说的话,但我希望你能帮助我...这里是我的代码
package com.functions;
import java.util.ArrayList;
import java.util.List;
import com.example.senditem.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
String adapter;
private final Activity context;
private final int id;
int listPosition;
public MyAdapter(Activity context, List<Model> list, int id){
super(context, R.layout.userproduct, list);
this.context = context;
this.list = list;
this.id = id;
}
class ViewHolder{
protected TextView text;
EditText quantity;
protected TextView amount;
TextView ext;
Spinner uom;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
listPosition = position;
ViewHolder viewHolder = null;
if(convertView == null){
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.userproduct, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.text.setFocusable(true);
viewHolder.quantity = (EditText) convertView.findViewById(R.id.inputQty);
viewHolder.amount = (TextView) convertView.findViewById(R.id.inputAmt);
viewHolder.uom = (Spinner) convertView.findViewById(R.id.inputUom);
viewHolder.ext = (TextView) convertView.findViewById(R.id.inputTamt);
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.inputQty, viewHolder.quantity);
convertView.setTag(R.id.inputAmt, viewHolder.amount);
convertView.setTag(R.id.inputTamt, viewHolder.ext);
convertView.setTag(R.id.inputUom, viewHolder.uom);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
SqliteHelper db = new SqliteHelper(context);
viewHolder.text.setText(list.get(position).getName());
viewHolder.quantity.setId(position);
viewHolder.amount.setText(String.valueOf(list.get(position).getAmt()));
viewHolder.amount.setId(position);
viewHolder.ext.setId(position);
viewHolder.uom.setId(position);
viewHolder.text.setId(position);
String data = db.getAllDetails(id, list.get(position).getitemId());
String data2 = db.getExt(id,list.get(position).getitemId());
if(data==null)
{
viewHolder.quantity.setText(list.get(position).quantity);
}
else{
viewHolder.quantity.setText(data);
}
if(data2==null)
{
viewHolder.ext.setText(String.valueOf(list.get(position).ext));
}
else{
viewHolder.ext.setText(data2);
}
viewHolder.quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
final int position = v.getId();
final EditText Caption = (EditText) v;
list.get(position).quantity = Caption.getText().toString();
}
}
});
viewHolder.ext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
final int position = v.getId();
final TextView Caption = (TextView) v;
list.get(position).ext = (float) Integer.parseInt((Caption.getText().toString()));
}
}
});
List<Model> uomlist = (List<Model>)
db.selectUom(list.get(position).getitemId());
List<String> myuom = new ArrayList<String>();
for(int j=0;j<uomlist.size();j++){
myuom.add(uomlist.get(j).getUom());
}
ArrayAdapter<String> uomadapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_item,myuom);
uomadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
viewHolder.uom.setAdapter(uomadapter);
return convertView;
}
}
答案 0 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
String adapter;
private final Activity context;
private final int id;
int listPosition;
public MyAdapter(Activity context, List<Model> list, int id){
super(context, R.layout.userproduct, list);
this.context = context;
this.list = list;
this.id = id;
}
class ViewHolder{
protected TextView text;
EditText quantity;
protected TextView amount;
TextView ext;
Spinner uom;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
listPosition = position;
ViewHolder viewHolder = null;
if(convertView == null){
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.userproduct, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.text.setFocusable(true);
viewHolder.quantity = (EditText) convertView.findViewById(R.id.inputQty);
viewHolder.amount = (TextView) convertView.findViewById(R.id.inputAmt);
viewHolder.uom = (Spinner) convertView.findViewById(R.id.inputUom);
viewHolder.ext = (TextView) convertView.findViewById(R.id.inputTamt);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
SqliteHelper db = new SqliteHelper(context);
viewHolder.text.setText(list.get(position).getName());
viewHolder.quantity.setId(position);
viewHolder.amount.setText(String.valueOf(list.get(position).getAmt()));
viewHolder.amount.setId(position);
viewHolder.ext.setId(position);
viewHolder.uom.setId(position);
viewHolder.text.setId(position);
String data = db.getAllDetails(id, list.get(position).getitemId());
String data2 = db.getExt(id,list.get(position).getitemId());
if(data==null)
{
viewHolder.quantity.setText(list.get(position).quantity);
}
else{
viewHolder.quantity.setText(data);
}
if(data2==null)
{
viewHolder.ext.setText(String.valueOf(list.get(position).ext));
}
else{
viewHolder.ext.setText(data2);
}
viewHolder.quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
list.get(position).quantity = ((EditText) v).getText().toString();
}
}
});
viewHolder.ext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
list.get(position).ext = (float) Integer.parseInt((((TextView) v).getText().toString()));
}
}
});
List<Model> uomlist = (List<Model>) db.selectUom(list.get(position).getitemId());
List<String> myuom = new ArrayList<String>();
for(int j=0;j<uomlist.size();j++){
myuom.add(uomlist.get(j).getUom());
}
ArrayAdapter<String> uomadapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_item,myuom);
uomadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
viewHolder.uom.setAdapter(uomadapter);
return convertView;
}
}