ListView меняет порядок при возврате из другого действия - PullRequest
1 голос
/ 10 марта 2012

У меня есть собственный ListView с первыми четырьмя элементами, являющимися кнопками. Когда я нажимаю кнопку, она входит в новое действие, я делаю все что угодно в новом действии, а затем возвращаюсь. Когда я вернусь, я хочу, чтобы кнопка, которую я нажал, была помечена, однако иногда (и только иногда) она проверяет не тот флажок, и я не могу понять, почему. Вот мой код для моего адаптера:

private class MyCustomAdapter extends BaseAdapter {
    private static final int TYPE_BUTTON = 0;
    private static final int TYPE_INFO = 1;
    private static final int TYPE_PICTURE = 2;
    private static final int TYPE_MAX_COUNT = 3;
    private int totalCount = 0;
    private ArrayList<String> mData = new ArrayList<String>();
    private LayoutInflater mInflater;

    public MyCustomAdapter() {
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addButton(final Button btn) {
        mButtons.add(btn);
        totalCount = totalCount + 1;
        notifyDataSetChanged();
    }

    public void addInfo(final String info) {
        mInfo.add(info);
        totalCount = totalCount + 1;
        notifyDataSetChanged();
    }

    public void addPicture(final Bitmap pic) {
        mPictures.add(pic);
        totalCount = totalCount + 1;
        notifyDataSetChanged();
    }

    public void addItem(final String item) {
        mData.add(item);
        totalCount = totalCount + 1;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return totalCount;
    }

    @Override
    public String getItem(int position) {
        return "TEST 5000";
    }

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

    @Override
    public int getItemViewType(int position) {
        if (position <= 3) {
            return TYPE_BUTTON;
        }
        if (mInfo.size() + 3 >= position) {
            return TYPE_INFO;
        } else {
            return TYPE_PICTURE;
        }
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("getView " + position + " " + convertView);
        ViewHolder holder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case TYPE_BUTTON:
                    convertView = mInflater.inflate(R.layout.button, null);
                    holder.btn = (Button)convertView.findViewById(R.id.btn);
                    holder.btn.setText(mButtons.get(position).getText().toString());
                    holder.btn.setLayoutParams(new LinearLayout.LayoutParams(mButtons.get(position).getLayoutParams().width, mButtons.get(position).getLayoutParams().height));
                    break;
                case TYPE_INFO:
                    convertView = mInflater.inflate(R.layout.incident_summary_items, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.text);
                    holder.textView.setText(mInfo.get(position-4));
                    break;
                case TYPE_PICTURE:
                    convertView = mInflater.inflate(R.layout.image, null);
                    holder.imageView = (ImageView)convertView.findViewById(R.id.pic);
                    holder.imageView.setImageBitmap(mPictures.get(position-4-mInfo.size()));
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
            switch(type) {
                case TYPE_BUTTON:
                    holder.btn.setText(mButtons.get(position).getText().toString());
                    holder.btn.setLayoutParams(new LinearLayout.LayoutParams(mButtons.get(position).getLayoutParams().width, mButtons.get(position).getLayoutParams().height));
                    break;
                case TYPE_INFO:
                    holder.textView.setText(mInfo.get(position-4));
                    break;
                case TYPE_PICTURE:
                    holder.imageView.setImageBitmap(mPictures.get(position-4-mInfo.size()));
                    break;
            }
        }
        return convertView;
    }

}

public static class ViewHolder {
    public TextView textView;
    public ImageView imageView;
    public Button btn;
}

и вот мой код, где я установил флажок и вызвал новое действие:

public void myClickHandler(View v)
{
    //get the row the clicked button is in
    vwParentRow = (LinearLayout)v.getParent();

    btnChild = (Button)vwParentRow.getChildAt(0);


    if (btnChild.getText().toString().equals("Take Information")) {
        btnChild.setCompoundDrawablesWithIntrinsicBounds(null,null,mCheckMark,null);
        Intent intent = new Intent(this, Info.class);
        startActivityForResult(intent, 1);
    }

    if (btnChild.getText().toString().equals("Take Pictures")) {
        btnChild.setCompoundDrawablesWithIntrinsicBounds(null,null,mCheckMark,null);
        Intent intent = new Intent(this, Pictures.class);
        startActivityForResult(intent, 2);
    }
}

Я нашел, если закомментировал строки

holder.btn.setText(mButtons.get(position).getText().toString());
holder.btn.setLayoutParams(new LinearLayout.LayoutParams(mButtons.get(position).getLayoutParams().width, mButtons.get(position).getLayoutParams().height));

в операторе else моего настраиваемого адаптера списка контрольный список проверял правильную кнопку, но кнопки меняли порядок в списке.

1 Ответ

0 голосов
/ 15 марта 2012

Поскольку вы используете startActivityForResult для нового действия, вы можете вернуть значение из этого и из вашего родителя, где ваши кнопки используют onResume, чтобы установить статус кнопки как отключенный.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...