在适配器视图中使用非最终变量

时间:2017-10-26 22:40:57

标签: android listview onclicklistener android-adapter final

我有一个页面显示有关电影的信息,以及一个名为Trigger对象的对象的投票系统。投票系统不起作用,我认为因为为了在OnClickListener的视图中使用Trigger对象,我必须声明触发器Final,所以它将onclicklistener设置为第一个触发器对象,所以我与之交互的按钮是什么只有第一个要加载的触发器对象发生了变化。我如何实现这一点,以便我不必在onclick监听器中使用最终变量?

TriggerAdapter.java

...

    @Override
    public View getView(int i, View view, final ViewGroup viewGroup) {
        final ViewHolder holder;

        FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();

        FirebaseUser user = firebaseAuth.getCurrentUser();

        movieDB = FirebaseDatabase.getInstance().getReference("movies");
        userDB = FirebaseDatabase.getInstance().getReference("users");
        adminDB = FirebaseDatabase.getInstance().getReference("admin");

        final String mUserId = user.getUid();
        trigger = mTriggers.get(i);

        triggerDB = movieDB.child(mImdbID).child("Triggers").child(trigger.getTriggerName());

        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.trigger_list_item, null);

            holder = new ViewHolder();
            holder.triggerName = view.findViewById(R.id.triggerName);
            holder.upButton = view.findViewById(R.id.mTriggerButtonUp);
            holder.downButton = view.findViewById(R.id.mTriggerButtonDown);
            holder.total = view.findViewById(R.id.triggerCounter);

            view.setTag(holder);
        }
        else {
            holder = (ViewHolder) view.getTag();
        }

        holder.triggerName.setText(trigger.getTriggerName());

        if (mContext instanceof AdminPanelActivity) {
            holder.upButton.setVisibility(View.INVISIBLE);
            holder.downButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    adminDB.child("triggerList").child(trigger.getTriggerName()).removeValue();
                    int i = mTriggers.indexOf(trigger);
                    mTriggers.remove(i);
                    TriggerAdapter.this.notifyDataSetChanged();
                }
            });
        }

        if (mContext instanceof MovieActivity) {

            triggerDB.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Trigger trigger;
                    trigger = dataSnapshot.getValue(Trigger.class);
                    holder.total.setText(String.valueOf(trigger.getTriggerVotesTotal()));
                    holder.upButton.setCount(trigger.getTriggerVotesYes());
                    int downBoats = (trigger.getTriggerVotesTotal() - trigger.getTriggerVotesYes());
                    holder.downButton.setCount(downBoats);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

            userDB.child(mUserId).child("changes").child(mImdbID).child("triggers").child(trigger.getTriggerName()).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.getValue() != null) {
                        int userVote = dataSnapshot.getValue(Integer.class);

                        if (userVote == 0) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                holder.upButton.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.colorAccent, null));
                                holder.downButton.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.colorAccent, null));

                            }
                            else {
                                holder.upButton.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.colorAccent));
                                holder.downButton.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.colorAccent));
                            }

                            neitherButtonIsSelected(holder, trigger, mUserId);
                        }
                        if (userVote == 1) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                holder.upButton.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.votebuttons, null));
                                holder.downButton.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.colorAccent, null));

                            }
                            else {
                                holder.upButton.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.votebuttons));
                                holder.downButton.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.colorAccent));
                            }
                            upButtonIsSelected(holder, trigger, mUserId);
                        }
                        if (userVote == -1) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                holder.upButton.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.colorAccent, null));
                                holder.downButton.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.votebuttons, null));

                            }
                            else {
                                holder.upButton.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.colorAccent));
                                holder.downButton.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.votebuttons));
                            }
                            downButtonIsSelected(holder, trigger, mUserId);
                        }
                    }
                    else {
                        userDB.child(mUserId).child("changes").child(mImdbID).child("triggers").child(trigger.getTriggerName()).setValue(0);
                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }


        return view;
    }

    private static class ViewHolder {
        TextView triggerName;
        CounterFab upButton;
        CounterFab downButton;
        TextView total;
    }
    private void neitherButtonIsSelected(final ViewHolder holder, final Trigger trigger, final String mUserId) {


        holder.upButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                triggerDB.child("triggerVotesYes").runTransaction(new Transaction.Handler() {
                    @Override
                    public Transaction.Result doTransaction(MutableData mutableData) {
                        int count = mutableData.getValue(Integer.class);
                        mutableData.setValue(count + 1);
                        return Transaction.success(mutableData);
                    }

                    @Override
                    public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {

                    }
                });
                triggerDB.child("triggerVotesTotal").runTransaction(new Transaction.Handler() {
                    @Override
                    public Transaction.Result doTransaction(MutableData mutableData) {
                        int count = mutableData.getValue(Integer.class);
                        mutableData.setValue(count + 1);

                        return Transaction.success(mutableData);
                    }

                    @Override
                    public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {

                    }
                });
                userDB.child(mUserId).child("changes").child(mImdbID).child("triggers").child(trigger.getTriggerName()).setValue(1);
            }
        });

        holder.downButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {

                triggerDB.child("triggerVotesTotal").runTransaction(new Transaction.Handler() {
                    @Override
                    public Transaction.Result doTransaction(MutableData mutableData) {
                        int count = mutableData.getValue(Integer.class);
                        mutableData.setValue(count + 1);

                        return Transaction.success(mutableData);
                    }

                    @Override
                    public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
                    }
                });
                userDB.child(mUserId).child("changes").child(mImdbID).child("triggers").child(trigger.getTriggerName()).setValue(1);
            }
        });
    }

    private void upButtonIsSelected(final ViewHolder holder, final Trigger trigger, final String mUserId) {
        holder.upButton.setOnClickListener(null);
        holder.downButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                triggerDB.child("triggerVotesYes").runTransaction(new Transaction.Handler() {
                    @Override
                    public Transaction.Result doTransaction(MutableData mutableData) {
                        int count = mutableData.getValue(Integer.class);
                        mutableData.setValue(count - 1);
                        userDB.child(mUserId).child("changes").child(mImdbID).child("triggers").child(trigger.getTriggerName()).setValue(-1);
                        return Transaction.success(mutableData);
                    }
                    @Override
                    public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
//                        holder.downButton.getBackground().setAlpha(127);
                    }
                });
            }
        });
    }
    private void downButtonIsSelected(final ViewHolder holder, final Trigger trigger, final String mUserId) {
        holder.downButton.setOnClickListener(null);
        holder.upButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                triggerDB.child("triggerVotesYes").runTransaction(new Transaction.Handler() {
                    @Override
                    public Transaction.Result doTransaction(MutableData mutableData) {
                        int count = mutableData.getValue(Integer.class);
                        mutableData.setValue(count + 1);
                        userDB.child(mUserId).child("changes").child(mImdbID).child("triggers").child(trigger.getTriggerName()).setValue(1);
                        return Transaction.success(mutableData);
                    }
                    @Override
                    public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
//                        holder.downButton.getBackground().setAlpha(127);
                    }
                });
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

  

投票系统不起作用,我认为是因为为了使用   我必须声明OnClickListener视图中的触发器对象   触发器最终

trigger是实例字段吗?然后是的,每当有人执行点击时,它的值就会重置。只需在onclick中执行mTriggers.get(i)即可。您需要将参数i设为final。

或者,将trigger作为本地决赛,

final Trigger trigger = mTriggers.get(i);

现在你可以放心,通过这种方法的后续执行,它的价值无法改变。