如何在Android中为hashmap适配器重新配置列表适配器?

时间:2013-12-05 01:50:19

标签: android listadapter

我很难让点击监听器与ListFragment一起正常工作。目标是显示喜欢列表,每个喜欢列表与类别相关联。首先显示类别,然后显示该类别中的喜欢。我已经创建了一个自定义适配器,它扩展了名为LikeHashMapAdapter的BaseAdapter,它可以按预期显示所有内容。这是getView()方法:

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

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

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.like_row, null);
    }

    LinearLayout likeWrapper = (LinearLayout) convertView.findViewById(R.id.likeWrapper);

    String category = mCategories[pos];
    ArrayList<Like> arrLikes = (ArrayList<Like>) getItem(pos);

    for(Like like: arrLikes) {
        TextView txtLike = new TextView(mContext);
        txtLike.setText(like.getKeyword());
        likeWrapper.addView(txtLike);
    }

    TextView txtLike = (TextView) convertView.findViewById(R.id.txtCategory);
    txtLike.setText(category);

    return convertView;
}

在我的ListFragment中,我按类别排序:

private LinkedHashMap<String, ArrayList<Like>> sortLikesByCategory(ArrayList<Like> arrayList) {

    // The hashmap value will be the category name, and the value will be the array of likes
    LinkedHashMap<String, ArrayList<Like>> map = new LinkedHashMap<String, ArrayList<Like>>();

    for(Like like: arrayList) {

        // If the key does not exist in the hashmap
        if(!map.containsKey(like.getCategory().getName())) {
            ArrayList<Like> listInHash = new ArrayList<Like>();
            listInHash.add(like);
            map.put(like.getCategory().getName(), listInHash);
        } else {
            // add the like to the arraylist that corresponds to the key
            ArrayList<Like> listInHash = map.get(like.getCategory().getName());
            listInHash.add(like);

        }

    }

    return map;

}

最后设置适配器:

final LinkedHashMap<String, ArrayList<Like>> finalLikeMap = sortLikesByCategory(finalLikes);

            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    final LikeHashMapAdapter adapter = new LikeHashMapAdapter(getActivity(),finalLikeMap);
                    setListAdapter(adapter);
                }
            });

问题是整个类别部分是可点击的,而不仅仅是单个喜欢。有没有一种简单的方法来解决这个问题?或者我需要从头开始吗?

3 个答案:

答案 0 :(得分:1)

我发现最好的办法是放弃我自己的代码并使用Android的Expandable List View适配器。很方便。我按照此链接给出的示例:http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

此处发布的代码太多,但它与标准的Android适配器实现类似。文档在这里: http://developer.android.com/reference/android/widget/ExpandableListView.html

答案 1 :(得分:0)

您可以通过在循环中向它们添加单击侦听器来使每个可点击的单击,如下所示:

for(Like like: arrLikes) {
    TextView txtLike = new TextView(mContext);
    txtLike.setText(like.getKeyword());
    txtLike.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something when this like is clicked
        }
    });
    likeWrapper.addView(txtLike);
}

答案 2 :(得分:0)

主要问题是您将列表项和类别合并到同一视图中。列表视图将内容视为单个项目,这就是它不起作用的原因。你应该做的是创建两种不同的项目类型:一个类别和一个项目。然后单击元素就没问题了。

您可以在适配器中使用以下行:

@Override
public int getViewTypeCount() {
    return 2; //One is for the elements, the other one is the category title
}

@Override
public int getItemViewType(int position) {
    //Here you tell the view which type of item it is  
    return positionToTypeMap.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //Return different views here depending on the current item type
}

这样它就可以按原样运作。