从活动传递hashmap数据

时间:2017-11-06 09:19:39

标签: java android android-intent

我只是尝试将av哈希映射的值传递给我的代码ExpListViewAdapterWithCheckbox中的另一个活动(从BaseExpandableListAdapter扩展)。

[enter image description here][1]
    HashMap<Integer, boolean[]> fileList = mChildCheckStates;
    Intent intent = new Intent(ExpListViewAdapterWithCheckbox.this, MainActivity.class);
    intent.putExtra("hashMap", fileList);
    startActivity(intent);

但它正在给出错误,说“无法解析构造函数意图”

以下是完整代码

public class ExpListViewAdapterWithCheckbox extends BaseExpandableListAdapter
{


private Context mContext;

private HashMap<String, List<String>> mListDataChild;

private ArrayList<String> mListDataGroup;

public HashMap<Integer, boolean[]> mChildCheckStates;

public boolean[] getChecked;

private ChildViewHolder childViewHolder;
private GroupViewHolder groupViewHolder;
private String groupText;
private String childText;


public ExpListViewAdapterWithCheckbox(Context context, ArrayList<String> listDataGroup, HashMap<String, List<String>> listDataChild) {

    mContext = context;
    mListDataGroup = listDataGroup;
    mListDataChild = listDataChild;

    mChildCheckStates = new HashMap<Integer, boolean[]>();
}

public int getNumberOfCheckedItemsInGroup(int mGroupPosition) {
    boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
    int count = 0;
    if (getChecked != null) {
        for (int j = 0; j < getChecked.length; ++j) {
            if (getChecked[j] == true)
                count++;
        }
    }
    return count;
}

@Override
public int getGroupCount() {
    return mListDataGroup.size();
}

@Override
public String getGroup(int groupPosition) {
    return mListDataGroup.get(groupPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}


@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {

    groupText = getGroup(groupPosition);

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_group, null);

        groupViewHolder = new GroupViewHolder();

        groupViewHolder.mGroupText = (TextView) convertView.findViewById(R.id.lblListHeader);

        convertView.setTag(groupViewHolder);
    } else {

        groupViewHolder = (GroupViewHolder) convertView.getTag();
    }

    groupViewHolder.mGroupText.setText(groupText);

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return mListDataChild.get(mListDataGroup.get(groupPosition)).size();
}

@Override
public String getChild(int groupPosition, int childPosition) {
    return mListDataChild.get(mListDataGroup.get(groupPosition)).get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    final int mGroupPosition = groupPosition;
    final int mChildPosition = childPosition;

    childText = getChild(mGroupPosition, mChildPosition);

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(R.layout.list_item, null);

        childViewHolder = new ChildViewHolder();

        childViewHolder.mChildText = (TextView) convertView.findViewById(R.id.lblListItem);

        childViewHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.lstcheckBox);

        convertView.setTag(R.layout.list_item, childViewHolder);

    } else {

        childViewHolder = (ChildViewHolder) convertView
                .getTag(R.layout.list_item);
    }

    childViewHolder.mChildText.setText(childText);

    childViewHolder.mCheckBox.setOnCheckedChangeListener(null);

    if (mChildCheckStates.containsKey(mGroupPosition)) {

        boolean getChecked[] = mChildCheckStates.get(mGroupPosition);

        childViewHolder.mCheckBox.setChecked(getChecked[mChildPosition]);

    } else {


         getChecked = new boolean[getChildrenCount(mGroupPosition)];

        mChildCheckStates.put(mGroupPosition, getChecked);

        HashMap<Integer, boolean[]> fileList = mChildCheckStates;
        Intent intent = new Intent(ExpListViewAdapterWithCheckbox.this, MainActivity.class);
        intent.putExtra("hashMap", fileList);
        startActivity(intent);


        childViewHolder.mCheckBox.setChecked(false);
    }

    childViewHolder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked) {

                boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                getChecked[mChildPosition] = isChecked;
                mChildCheckStates.put(mGroupPosition, getChecked);

            } else {

                boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                getChecked[mChildPosition] = isChecked;
                mChildCheckStates.put(mGroupPosition, getChecked);
            }
        }
    });

    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

@Override
public boolean hasStableIds() {
    return false;
}

public final class GroupViewHolder {

    TextView mGroupText;
}

public final class ChildViewHolder {

    TextView mChildText;
    CheckBox mCheckBox;
}
}

我是android的新手。请帮我解决这个问题。提前谢谢。

3 个答案:

答案 0 :(得分:1)

使用intent.putExtra(String,Serializable) - 参见

intent.putExtra("hashMap", fileList);

在接收活动中使用

 HashMap<Integer, boolean[]> fileList = (Integer, boolean[]) intent.getSerializableExtra("hashMap");

还要输入以下代码

Intent intent = new Intent(ExpListViewAdapterWithCheckbox.this, MainActivity.class);

Intent intent = new Intent(context, MainActivity.class);

答案 1 :(得分:0)

Java的HashMap类扩展了Serializable接口,使用Intent.putExtra(String,Serializable)方法可以很容易地将它添加到intent中。

在接收intent的activity / service / broadcast接收器中,然后使用与putExtra一起使用的名称调用Intent.getSerializableExtra(String)。

例如,发送意图时:

HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("key", "value");
Intent intent = new Intent(this, MyOtherActivity.class);
intent.putExtra("map", hashMap);
startActivity(intent);

然后在接收活动中:

protected void onCreate(Bundle bundle) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("map");
    Log.v("HashMapTest", hashMap.get("key"));
}

答案 2 :(得分:0)

new Intent期望第一个参数是类型上下文,而ExpListViewAdapterWithCheckbox类不是Activity类型,它无法获取上下文。 因此,您可以使用app context来实现以下目标。