Android:如何从ListView中的Edittext获取文本?

时间:2012-02-20 05:41:47

标签: android listview android-edittext

我在列表视图中的edittext中获取文本时出现问题,因为当获取该edittext的数据时,列表问题中的最后一个edittext,因为当edittext onfocuschangelistner的更改焦点没有得到调用时,该edittext的数据不存储在我的集合变量。

所以任何人都有任何解决方案PLZ建议我

我的代码在这里

    public class YourAdapter extends SimpleAdapter {

    protected static final String TAG = null;
    ArrayList<HashMap<String, String>> c = new ArrayList<HashMap<String, String>>();
    Context context;
    JSONArray getArray_Meter_Reading;
    HashMap<String, String> previous_Meter_Reading;
    ArrayList<HashMap<String, Integer>> getReading = new ArrayList<HashMap<String, Integer>>();
    Map<String,Integer> CurrentReading  = new HashMap<String, Integer>();
    int i, previousMeterReading, CurrentMeterReading, Cumulative;
    View v;
    EditText Caption;
    public static HashMap<Integer, String> myList = new HashMap<Integer, String>();

    public YourAdapter(Context context, int layout,
            ArrayList<HashMap<String, String>> c, String[] from, int[] to,
            JSONArray getArray_Meter_Reading) {
        super(context, c, layout, from, to);
        this.c = c;
        this.context = context;
        this.v = null;
        this.Caption = null;
        this.getArray_Meter_Reading = getArray_Meter_Reading;

        for (i = 0; i < getArray_Meter_Reading.length(); i++) {
            myList.put(i, "");
        }

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        final EditText ed_Current = (EditText) view
                .findViewById(R.id.ed_Current);
        ed_Current.setTag(position);

        ed_Current.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            public void onFocusChange(View arg0, boolean arg1) {

                int tag1 = (Integer) arg0.getTag();
                myList.put(tag1, ed_Current.getText().toString().trim());
                previous_Meter_Reading = new HashMap<String, String>();


                if (v == null) {
                    v = arg0;

                    int tag = (Integer) arg0.getTag();
                    Caption = (EditText) arg0;

                    previous_Meter_Reading = c.get(tag);
                    String getPreviousReading = previous_Meter_Reading
                            .get("previousMeterReading");
                    String CumulativeIndex = previous_Meter_Reading
                            .get("Cumulative");
                    previousMeterReading = Integer.parseInt(getPreviousReading);
                    Cumulative = Integer.parseInt(CumulativeIndex);
                }


                if (v != arg0) {

                    if (!Caption.getText().toString().equals("")
                            && Cumulative == 1) {

                        int tag = ((Integer) arg0.getTag());
                        CurrentMeterReading = Integer.valueOf(Caption.getText()
                                .toString());


                        //CurrentReading.put("Tag"+ tag, CurrentMeterReading);
                        //if(CurrentReading.containsKey("Tag"+ tag)){
                         //  
                        //  Log.d("Contains","Tag"+ tag );
                        //  CurrentReading.remove("Tag"+ tag);
                        //}
                    //  }else{
                        //  
                             CurrentReading.put("Tag"+ tag,CurrentMeterReading);
                        //}
                        //getReading.add(CurrentReading);
                             Log.d("CurrentReading",CurrentReading.toString() );
                        if (CurrentMeterReading < previousMeterReading) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(
                                    context);
                            builder.setTitle("WARNING");
                            builder.setIcon(android.R.drawable.ic_dialog_alert);
                            builder.setMessage("Current value cannot be less than Previous value for metre");
                            builder.setPositiveButton("ok",
                                    new DialogInterface.OnClickListener() {

                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {

                                            //Caption.requestFocus();

                                        }
                                    });

                            AlertDialog diag = builder.create();
                            diag.show();

                            // Caption.requestFocus();
                            // v = null;
                            // Caption = null;
                        } 
                    }else {
                            if (Cumulative == 0

                                && !Caption.getText().toString().equals("")) {
                                int tag = ((Integer) arg0.getTag());
                            // int tag1 = ((Integer) arg0.getTag());
                            CurrentMeterReading = Integer.valueOf(Caption
                                    .getText().toString());


                            CurrentReading.put("Tag" +tag, CurrentMeterReading);
                            if(CurrentReading.containsKey("Tag"+ tag)){
                                CurrentReading.put("Tag"+ tag,CurrentMeterReading);
                            }
                            //getReading.add(CurrentReading);
                        }
                        }

                    //Caption.requestFocus();
                    v = null;
                    Caption = null;
                }

            }
        });

        ed_Current.setText(myList.get(position));
        return view;
    }
}

and my xml which im binding with listview using simpleadapter

    <?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="6dip"
    android:paddingTop="4dip" >

    <TextView
        android:id="@+id/txt_Meter_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:width="140dp" />

    <TextView
        android:id="@+id/txt_Previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:width="100dp" />

    <EditText
        android:id="@+id/ed_Current"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:imeOptions="actionNext|actionDone"
        android:inputType="number"
        android:numeric="integer"
        android:singleLine="true"
        android:width="100dp" >
    </EditText>

</LinearLayout>

提前致谢

1 个答案:

答案 0 :(得分:2)