如何覆盖Android

时间:2018-03-20 01:40:43

标签: java android listview button custom-adapter

所以我有一个按钮,这个按钮用上面的单元格切换列表视图的单元格,反之亦然,用于向下的另一个按钮(这个用于向上...这不重要,我刚刚决定谈论这一个)。整个按钮和列表视图的工作正常,但我的问题是当我按下向上按钮,然后决定再次按下它时,它只是作为一个向下按钮。这是因为它仍然停留在列表视图的相同项目/位置,我需要找到一种方法来覆盖代码中onItemClick的位置?

```

upButton.setOnClickListener(new View.OnClickListener(){

                            @Override

                            public void onClick(View v) {

                                //upButton onClick

                                Integer myTeam = position; //7 -159

                                Integer otherTeam = position - 1; //6 - 1678

                                Map<Integer, String> onClickMap = sortByValue(Constants.picklistMap);

                                String extraValue;

                                Log.e("myposition", myTeam.toString());

                                Log.e("otherposition", otherTeam.toString());

                                extraValue = onClickMap.get(myTeam); //159

                                String team = onClickMap.get(otherTeam);

                                Constants.picklistMap.put(myTeam, onClickMap.get(otherTeam));

                                Constants.picklistMap.put(otherTeam, extraValue); //6

                                Log.e("Position: ",Constants.picklistMap.get(position));

                                Log.e("Position - 1: ",Constants.picklistMap.get(position - 1));

                                if(myTeam != 0) {

                                    dref.child("picklist").child(myTeam.toString()).setValue(Integer.parseInt(Constants.picklistMap.get(myTeam)));
                                    dref.child("picklist").child(otherTeam.toString()).setValue(Integer.parseInt(Constants.picklistMap.get(otherTeam)));


                                } else {

                                    Toast.makeText(getActivity(), "Nice try. 
    If you try it again, the app is going to crash as punishment ... (:",
                                            Toast.LENGTH_LONG).show();

                                }

                            }

                        });

```

通过重写这个位置,我的意思是如何 position =+ 1 会在python中使position = position + 1,我想在Java中做同样的事情。问题是我知道无法使用我刚才用来增加位置值的代码片段来完成!

请帮忙!

我无法使用position = position + 1的原因是因为在onItemClick上定义了int位置,然后为按钮创建了onClick,所以我需要最终位置才能使用它在onClick中,如果我摆脱了final,我将无法在onClick中使用它,因为它不能在内部类中进行访问,因为它不是最终的

1 个答案:

答案 0 :(得分:0)

定义要更改的位置,并使用此方法更新位置。您可以在onClickListener中使用此方法。这应该有用。

    private int position = 0;

    private void up(ArrayList<Integer> arr){
            if (position < 1 || position >= arr.size()) return;
            int temp = arr.get(position - 1);
            arr.set(position - 1, arr.get(position));
            arr.set(position, temp);
            position --;
        }
相关问题