ListView中的EditText具有重复的光标位置android

时间:2016-04-20 13:08:18

标签: android

我有一个listview,其中有一个edittext。当我点击edittext的第一个位置并在其上写一些内容时,相同的文本将写在listview的不同位置的edittext的相同字段id的另一个edittext中。

过去12个小时我一直被困在这里。我试图搜索每个地方,但我没有找到任何答案。

enter image description here

这是我的适配器类代码

公共类AttendanceSheetAdapter扩展了ArrayAdapter {

Activity activity;
LayoutInflater inflater;
String trip, vehicleNo, msg;
ArrayList<Datalist> datalists;
int a;
boolean b;

public AttendanceSheetAdapter(Activity activity, int resource, ArrayList<Datalist> datalists) {
    super(activity, resource, datalists);
    this.activity = activity;
    this.datalists = datalists;

}

@Override
public int getCount() {
    return datalists.size();
}

@Override
public Datalist getItem(int position) {
    return datalists.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {


    if (inflater == null)
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.attendance_adapter, null);

    ViewHolder holder = new ViewHolder();
    holder.vehicleId = (TextView) convertView.findViewById(R.id.vehicleId);
    holder.tripId = (EditText) convertView.findViewById(R.id.tripId);
    holder.comments = (EditText) convertView.findViewById(R.id.comments);
    holder.driverPhoto = (ImageView) convertView.findViewById(R.id.driverPhoto);

    Datalist d = datalists.get(position);
    d.getVehicleNo();
    d.getDriverPhoto();

    holder.vehicleId.setText(d.vehicleNo);
    holder.driverName.setText(d.getDriverName());
    if (holder.driverPhoto != null) {
        Picasso.with(activity).load(d.getDriverPhoto()).into(holder.driverPhoto);
    }
    holder.comments.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            msg = arg0.toString();
        }
    });
    holder.comments.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                if (a == position) {
                    if (msg != null) {
                        msg = msg.replace(" ", "%20");
                        vehicleNo = datalists.get(position).getVehicleNo();
                        COMMENTS(); // api call
                    }
                }
            }
            if (a != position && b != false) {
                if (trip != null) {
                    Intent i = new Intent(activity.getApplicationContext(), AttendanceSheet.class);
                    activity.startActivity(i);
                    activity.finish();
                }
            }
            a = position;
            b = true;
        }
    });

    holder.tripId.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {

            trip = arg0.toString();
        }
    });
    holder.tripId.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {


            if (!hasFocus) {
                if (a == position) {
                    if (trip != null) {
                        trip = trip.replace(" ", "%20");
                        vehicleNo = datalists.get(position).getVehicleNo();
                        TRIPID(); // api call
                    }
                }
            }
            if (a != position && b != false) {
                if (trip != null) {
                    Intent i = new Intent(activity.getApplicationContext(), AttendanceSheet.class);
                    activity.startActivity(i);
                    activity.finish();
                }
            }
            a = position;
            b = true;
        }
    });

    return convertView;
}

static class ViewHolder {
    TextView vehicleId;
    EditText comments, tripId;
    ImageView driverPhoto;
}

}

1 个答案:

答案 0 :(得分:0)

我已编辑您的代码尝试使用以下代码并检查它是否有效:

    Activity activity;
        LayoutInflater inflater;
        String trip, vehicleNo, msg;
        ArrayList<Datalist> datalists;
        int a;
        boolean b;

        public AttendanceSheetAdapter(Activity activity, int resource, ArrayList<Datalist> datalists) {
            super(activity, resource, datalists);
            this.activity = activity;
            this.datalists = datalists;

        }

        @Override
        public int getCount() {
            return datalists.size();
        }

        @Override
        public Datalist getItem(int position) {
            return datalists.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

            if (inflater == null)
                inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null){
                convertView = inflater.inflate(R.layout.attendance_adapter, parent,false);

            holder = new ViewHolder();
            holder.vehicleId = (TextView) convertView.findViewById(R.id.vehicleId);
            holder.tripId = (EditText) convertView.findViewById(R.id.tripId);
            holder.comments = (EditText) convertView.findViewById(R.id.comments);
            holder.driverPhoto = (ImageView) convertView.findViewById(R.id.driverPhoto);
convertView.setTag(holder);
        }else{

    holder = (ViewHolder)convertView.getTag();
    }
            Datalist d = datalists.get(position);
            d.getVehicleNo();
            d.getDriverPhoto();

            holder.vehicleId.setText(d.vehicleNo);
            holder.driverName.setText(d.getDriverName());
            if (holder.driverPhoto != null) {
                Picasso.with(activity).load(d.getDriverPhoto()).into(holder.driverPhoto);
            }
            holder.comments.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

                }

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

                }

                @Override
                public void afterTextChanged(Editable arg0) {
                    msg = arg0.toString();
                }
            });
            holder.comments.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus) {
                        if (a == position) {
                            if (msg != null) {
                                msg = msg.replace(" ", "%20");
                                vehicleNo = datalists.get(position).getVehicleNo();
                                COMMENTS(); // api call
                            }
                        }
                    }
                    if (a != position && b != false) {
                        if (trip != null) {
                            Intent i = new Intent(activity.getApplicationContext(), AttendanceSheet.class);
                            activity.startActivity(i);
                            activity.finish();
                        }
                    }
                    a = position;
                    b = true;
                }
            });

            holder.tripId.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

                }

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

                }

                @Override
                public void afterTextChanged(Editable arg0) {

                    trip = arg0.toString();
                }
            });
            holder.tripId.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {


                    if (!hasFocus) {
                        if (a == position) {
                            if (trip != null) {
                                trip = trip.replace(" ", "%20");
                                vehicleNo = datalists.get(position).getVehicleNo();
                                TRIPID(); // api call
                            }
                        }
                    }
                    if (a != position && b != false) {
                        if (trip != null) {
                            Intent i = new Intent(activity.getApplicationContext(), AttendanceSheet.class);
                            activity.startActivity(i);
                            activity.finish();
                        }
                    }
                    a = position;
                    b = true;
                }
            });

            return convertView;
        }

        static class ViewHolder {
            TextView vehicleId;
            EditText comments, tripId;
            ImageView driverPhoto;
}