ListView从另一个活动返回时更改顺序

时间:2012-03-09 20:16:22

标签: android android-listview

我有一个自定义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 :(得分:0)

当您使用startActivityForResult进行新活动时,您可以返回该值以及父级上的值,其中按钮使用onResume将按钮状态设置为禁用。