Android的可扩展列表视图 - 孩子有持续状态中onClickListener的按钮 [英] Android Expandable List View - onClickListener for buttons within the child with persistent states

查看:133
本文介绍了Android的可扩展列表视图 - 孩子有持续状态中onClickListener的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个问题,我的创作ExpandableListView的。我不能确定发生了什么事的时​​刻,但我会解释我想要做的事情。

I am currently having an issue with the creation of my ExpandableListView. I'm unsure what is happening at the moment but I'll explain what I'm trying to do.

我编程方式分配餐点,落在某一天。在每餐(标题为组)是一个孩子,其中包含投票和命令按钮。这两者都更改为有序时,命令是一顿饭。如果另一个命令是在previous命令按钮返回到订单状态,而选择一个人去订购。这是相同的表决。我去这个通过所有其他按钮上点击监听器设置的,以我的按钮,循环,并设置其文字'订单'/'投票',并设置当前按钮文本排序。

I programmatically allocate meals that fall on a certain day. Within each meal (the title is the group) is a child that contains a vote and order button. Both of which are changed to "Ordered" when a order is made on a meal. If another order is made the previous order button goes back to the 'order' state and the selected one goes to ordered. This is the same for vote. I go about this by setting an on click listener to my buttons that loop through all the other buttons and set their text to 'order'/'vote' and set the current buttons text to ordered.

这工作正常,在某些极为罕见的情况下 - 但大多数的时候,我订购/票为它改变了选择一个有序,并责令以及在我的扩展列表视图中的最后一个元素的项目。然后,如果我命令,可以说第三个元素,关闭并重新打开它也变为有序,反之亦然第二个元素。有时候,他们都变回订购或全部更改为订购。我发现很难明白为什么这会发生。

This works fine in some extremely rare cases - but most of the time when I order/vote for an item it changes the selected one to 'ordered' and the last element in my expandable list view to ordered as well. Then if I order, lets say the 3rd element, close and reopen the second element it also changes to ordered and visa versa. Sometimes they all change back to order or all change to ordered. I'm finding it difficult to work out why this may be happening.

我认为它可能有一些做的getChildView功能。当我通过列表打开视图从不最后一个元素设置每一个元素,尽管它已经被分配了相应的数据移动。

I believe it may have something to do with the getChildView function. As when I traverse through the list opening each element the view is never set for the last element even though it has been allocated the appropriate data.

我误解了ExpandableListView的一个主要概念,还是有别的东西。

Am I misunderstanding a major concept of the ExpandableListView or is there something else.

下面是getChildView功能,我认为有可能是一个错误。我还将提供链接到低于其它相关类。如果您需要了解更多信息,请不要犹豫,问。

Here is the getChildView function that I believe there may be a fault. I'll also supply links to the other relevant classes below. If you need further information, please don't hesitate to ask.

//Static class view holder
static class ViewHolder {
    protected Button oBut;
    protected Button vBut;
    protected TextView description;
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    //Getting the appropriate child for the group and position
    ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition);

    View view = null;

    //Set up if doesn't exist
    if (convertView == null) {
        Log.d("OnClickListener", "groupPosition: " + groupPosition + " childPosition: " + childPosition);
        Log.d("OnClickListener", "View being set up for: " + child.getName() + " desc: " + child.getDescription());

        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        view = infalInflater.inflate(R.layout.expandlist_child_item_todays, null);

        //Attempt at viewholder
        final ViewHolder holder = new ViewHolder();
        holder.description = (TextView) view.findViewById(R.id.mealDescription);
        holder.vBut = (Button)view.findViewById(R.id.vote);
        holder.oBut = (Button)view.findViewById(R.id.order);

        view.setTag(holder);
    }
    else {
        view = convertView;
    }

    ViewHolder holder = (ViewHolder) view.getTag();

    holder.description.setText(child.getDescription());
    holder.description.setTag(child.getTag());

    holder.oBut.setOnClickListener(new OrderOnClick(groups, (Activity)context, holder.oBut, view, child));
    holder.vBut.setOnClickListener(new VoteOnClick(groups, (Activity)context, holder.vBut, view, child));

    return view;
}

ExpandListAdapterTodays (延伸和扩展了我的BaseExpandableListAdapter)

ExpandListAdapterTodays (extends and extends off my BaseExpandableListAdapter)

ExpandListAdapter (扩展BaseExpandableListAdapter)

ExpandListAdapter (extends the BaseExpandableListAdapter)

VoteOnClick (实现按钮的文字时,全成的投票已放置在改变类)

VoteOnClick (Class that implements the changing of button text when a successfull vote has been placed)

推荐答案

这是列表视图在Android上的工作是有效的方式是他们实现一个地方,如果有些人认为熄灭屏幕,同样的看法是把观再造的方法回到地方与所有必要位在屏幕上改变。这令使用非常少的资源,实际上是重复使用相同的资源的事情,但肆虐,如果你需要国家坚持按照特定的顺序像你一样。你应该做的是实施某种形式的地图或ArrayList的哪个对象上的位置对应列表视图中的位置,然后让通过人造物体的变化。有点跛,但它有点像一个适配器的方法为您的适配器。

the way that listviews on android work to be efficient is that they implement a "view recycling" method where if some view goes off screen, that same view is put back somewhere on the screen with all the necessary bits changed. That keeps things using much less resources, actually reusing the same resources, but wreaks havoc if you need states persisting in a specific order like you do. What you should do is to implement some sort of map or arraylist of which the position on the object corresponds to its position of the listview and then make changes through the made object. A little lame but its kinda like an adapter method for your adapter.

现在我道歉,因为我不能完全可视化的点击数的方法是如何工作的,但翻翻吧..是这样的:

Now i apologize because i can't exactly visualize how the onlick method is supposed to work, but looking through it.. something like:

for (int i = 0; i < groups.size(); i++) {
....
but.setText("Vote");

将成为

ArrayList<String> group_string_states = new ArrayList<String> ();

private void fillGroupStringStates () {
        for(int i = 0; i < groups.length; i++) {
            group_string_states.add("Order");
        }
    }

....

for (int i = 0; i < group_string_states.size(); i++) {
....
group_string_states.set(i, "Vote");

然后,你做一个有条件的,像 如果group_string_states.get [位置]说:投票,那么做到这一点。

Then you do a conditional, like if group_string_states.get[position] says "vote", then do this.

嗯,这是我怎么会试图做到这一点。我希望它帮助,我很抱歉,如果它没有。

Well, that's how i'd attempt to do it. i hope it helped and i'm really sorry if it didn't.

这篇关于Android的可扩展列表视图 - 孩子有持续状态中onClickListener的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆