使用getItem(int)方法保持ArrayAdapter值的顺序

时间:2015-08-17 19:11:10

标签: android android-arrayadapter

我有一个包含2个EditText的项目列表。如果我用“1”和“1”填充第一行,用“2”和“2”填充第二行,用“3”和“3”填充第20行(例如..),那么当我检索ListAdapter时每个使用getItem(i)方法的值,值按此顺序排列:3x3,1x1,2x2

这是我的ArrayAdapter:

public class GameNoLotAdapter extends ArrayAdapter<ModelGameNoLotList> {

private int NB_DIGITS;
private List<ModelGameNoLotList> list;
private Activity activity;
private int KEY_GAME;

public GameNoLotAdapter(Activity activity, List<ModelGameNoLotList> list, int nbDigits, int keyGame) {
    super(activity, R.layout.item_gamenolot, list);
    this.activity = activity;
    this.list = list;
    NB_DIGITS = nbDigits;
    KEY_GAME = keyGame;
}

static class ViewHolder {
    protected EditText number;
    protected EditText money;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    if (convertView == null) {
        LayoutInflater inflator = activity.getLayoutInflater();

        view = inflator.inflate(R.layout.item_gamenolot, null);

        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.money = (EditText) view.findViewById(R.id.money);
        viewHolder.money.setFilters(new InputFilter[]{ new InputFilterMinMax(1, Utils.getGameLimit(KEY_GAME))});
        viewHolder.money.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                ModelGameNoLotList element = (ModelGameNoLotList) viewHolder.money.getTag();
                element.setMoney(s.toString());
            }

            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

            public void afterTextChanged(Editable s) {
            }
        });
        viewHolder.money.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String str = viewHolder.money.getText().toString();
                if (!hasFocus && str.length() != 0) {
                    if (Long.parseLong(str) == 0)
                        viewHolder.money.setError("No 0");
                }
            }
        });
        viewHolder.number = (EditText) view.findViewById(R.id.number);
        InputFilter[] filters = new InputFilter[1];
        filters[0] = new InputFilter.LengthFilter(NB_DIGITS);
        viewHolder.number.setFilters(filters);
        switch (this.NB_DIGITS) {
            case 2 : viewHolder.number.setHint(R.string.minus2);
                break;
            case 3 : viewHolder.number.setHint(R.string.minus3);
                break;
            case 4 : viewHolder.number.setHint(R.string.minus4);
                break;
            default: viewHolder.number.setHint(R.string.nimewo);
        }
        viewHolder.number.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                ModelGameNoLotList element = (ModelGameNoLotList) viewHolder.money.getTag();
                element.setNumber(s.toString());
            }

            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

            public void afterTextChanged(Editable s) {
            }
        });
        viewHolder.number.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String str = viewHolder.number.getText().toString();
                if (!hasFocus && str.length() != 0 && str.length() < NB_DIGITS)
                    viewHolder.number.setError(new StringBuilder(NB_DIGITS).append(" chif"));
            }
        });
        viewHolder.number.setTag(list.get(position));
        viewHolder.money.setTag(list.get(position));
        view.setTag(viewHolder);
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).number.setTag(list.get(position));
        ((ViewHolder) view.getTag()).money.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.money.setText(list.get(position).getMoney());
    holder.number.setText(list.get(position).getNumber());

    Animation animation = AnimationUtils.loadAnimation(activity, R.anim.list_entrance_effect);
    animation.setDuration(300);
    view.startAnimation(animation);

    return view;

}
}

这是我对listadapter值的检索:

private int updateUser(int index, boolean isEnding) {
    ListFragment fragment = fragments.get(index);
    ListAdapter listAdapter = fragment.getListAdapter();
    int size = listAdapter.getCount();

    HashMap<Integer, Object> map = new HashMap<>();
    for (int i = 0; i < size; i++) {

        if (listAdapter.getItem(i) instanceof ModelGameNoLotList) {
            ModelGameNoLotList model = (ModelGameNoLotList) listAdapter.getItem(i);
            String strNumber = model.getNumber();

我对视图顺序没有任何问题,每个值都保持原来的位置。

有人可以帮我解决吗?

感谢。

0 个答案:

没有答案