listview内的Edittext失去焦点

时间:2014-06-09 11:13:13

标签: android listview

我在listview项目中有一些TextView和edittexts。

我想要的是显示一些项目,数量,费率和总价...

每当数量或费率发生变化时,总价格也应该改变...... 当关注edittext时,Listitem应被视为已选中。

public class MainActivity extends Activity {

private MySimpleArrayAdapterNew sampleListAdapter;
private ArrayList<HashMap<String, String>> sampleArrayList;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView=(ListView)findViewById(R.id.listView1);
    sampleArrayList = new ArrayList<HashMap<String, String>>();
    for(int i=0;i<100;i++)
    {
    HashMap<String, String> sampleObjectMap = new HashMap<String, String>();
    sampleObjectMap.put("value1", "aaa"+i);

    sampleObjectMap.put("value2", "666");
    sampleObjectMap.put("value3", "1");
    sampleObjectMap.put("value4", "777");
    sampleObjectMap.put("value5", "444"+i);
    sampleObjectMap.put("value6", "444"+i);
    sampleArrayList.add(sampleObjectMap);
    }

    sampleListAdapter = new MySimpleArrayAdapterNew(MainActivity.this,
            R.layout.cartitem, sampleArrayList);
    listView.setAdapter(sampleListAdapter);
}

public class MySimpleArrayAdapterNew extends
        ArrayAdapter<HashMap<String, String>> {
    private Context context;
    private int layoutResourceId;
    private ArrayList<HashMap<String, String>> sampleArrayLists;

    public MySimpleArrayAdapterNew(Context context, int layoutResourceId,
            ArrayList<HashMap<String, String>> sampleArrayList) {
        super(context, R.layout.cartitem, sampleArrayList);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.sampleArrayLists = sampleArrayList;

    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context)
                    .getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.slno = (TextView) row.findViewById(R.id.slno);
            holder.itemname = (TextView) row.findViewById(R.id.itemname);
            holder.qty = (EditText) row.findViewById(R.id.qty);
            holder.rate = (EditText) row.findViewById(R.id.rate);
            holder.seatno = (EditText) row.findViewById(R.id.seat);
            holder.value = (TextView) row.findViewById(R.id.value);

            row.setTag(holder);
        } else {
            holder = (WeatherHolder) row.getTag();
        }
        final HashMap<String, String> sampleObjectMap = sampleArrayLists
                .get(position);
        holder.itemname.setText(sampleObjectMap.get("value1"));
        holder.slno.setText(sampleObjectMap.get("value2"));
        holder.qty.setText(sampleObjectMap.get("value3"));
        holder.rate.setText(sampleObjectMap.get("value4"));
        holder.seatno.setText(sampleObjectMap.get("value5"));
        holder.value.setText(sampleObjectMap.get("value6"));
        holder.rate.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                final EditText Caption = (EditText) v;
                String currentrate = sampleObjectMap.get("value4");
                try {
                    if (!((sampleObjectMap.get("value4")) == null)) {
                        sampleObjectMap.put("value4", Caption.getText()
                                .toString());
                        int currentquantity = Integer
                                .parseInt(sampleObjectMap.get("value3"));
                        sampleObjectMap.put(
                                "value6",
                                (Double.parseDouble(sampleObjectMap
                                        .get("value4")) * (currentquantity))
                                        + "");
                        sampleArrayList.set(position, sampleObjectMap);
                    }
                } catch (NumberFormatException ne) {
                    ne.printStackTrace();

                    sampleObjectMap.put("value4", currentrate);
                    sampleArrayList.add(sampleObjectMap);

                }

                if (!hasFocus) {
                    notifyDataSetChanged();
                } else if (hasFocus) {

                }
            }
        });

        holder.qty.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                final EditText Caption = (EditText) v;
                try {
                    if (!((sampleObjectMap.get("value3")) == null)) {
                        sampleObjectMap.put("value3", Caption.getText()
                                .toString());
                        int currentquantity = Integer
                                .parseInt(sampleObjectMap.get("value3"));
                        sampleObjectMap.put(
                                "value6",
                                (Double.parseDouble(sampleObjectMap
                                        .get("value4")) * (currentquantity))
                                        + "");
                        sampleArrayList.set(position, sampleObjectMap);
                    }
                } catch (NumberFormatException ne) {
                    ne.printStackTrace();
                    sampleObjectMap.put("value3", "1");
                    sampleArrayList.add(sampleObjectMap);
                }
                if (!hasFocus) {
                    notifyDataSetChanged();
                } else if (hasFocus) {

                }
            }
        });
        holder.seatno.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                final EditText Caption = (EditText) v;
                sampleObjectMap.put("value5", Caption.getText().toString());
                sampleArrayList.set(position, sampleObjectMap);
                if (!hasFocus) {
                    notifyDataSetChanged();
                } else if (hasFocus) {

                }
            }
        });
        row.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                listView.setItemChecked(position, true);
            }
        });
        return row;
    }

    public class WeatherHolder {
        TextView slno;
        TextView itemname;
        EditText qty;
        EditText rate;
        EditText seatno;
        TextView value;

    }
}

}

1 个答案:

答案 0 :(得分:0)

此更改可能有所帮助。

首先,更改为listview:

<ListView
    android:id="@android:id/list" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:descendantFocusability="beforeDescendants"
    />

然后,在Manifest.xml中添加:

<activity android:name= ".yourActivity" android:windowSoftInputMode="adjustPan"/>