如何获取使用不同参数调用的方法?

How to get a method called with different parameters?

我从数据库中获取 3 个 String 值,然后将其转换为 Long,然后计算差值,然后将计算出的 Long方法中的值作为参数。我正在使用 FastAdapter.

filterRequests(List <Long> l)MainActivity中的一个方法,它根据长l做过滤requests/content的逻辑。

整个适配器:

public class GRModelClass extends AbstractItem<GRModelClass, GRClass.ViewHolder>{

    private static final ViewHolderFactory<? extends ViewHolder> FACTORY = new ItemFactory();

    String postedBy, postedTime, currentLat, currentLng, utcFormatDateTime, userEmail, userID;
    String startDateTimeInEpoch, endDateTimeInEpoch;
    DatabaseReference primaryDBKey;
    long ms;
    String itemID;

    public GRModelClass(){}

    public GRModelClass(String postedBy, String postedTime, String currentLat, String currentLng, String utcFormatDateTime, String userEmail, String userID, String startDateTimeInEpoch, String endDateTimeInEpoch, DatabaseReference primaryDBKey) {
        this.postedBy = " " + postedBy;
        this.postedTime = postedTime;
        this.currentLat = currentLat;
        this.currentLng = currentLng;
        this.utcFormatDateTime = utcFormatDateTime;
        this.userEmail = userEmail;
        this.userID = userID;
        this.startDateTimeInEpoch = startDateTimeInEpoch;
        this.endDateTimeInEpoch = endDateTimeInEpoch;
        this.primaryDBKey = primaryDBKey;
    }

    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("pBy", postedBy);
        result.put("cLat", currentLat);
        result.put("cLng", currentLng);
        result.put("utcFormatDateTime", utcFormatDateTime);
        result.put("userEmail", userEmail);
        result.put("userID", userID);
        result.put("startDateTime", startDateTimeInEpoch);
        result.put("endDateTime", endDateTimeInEpoch);

        return result;
    }


    @Override
    public int getType() {
        return R.id.recycler_view;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.sr_layout;
    }

    @Override
    public void bindView(final ViewHolder holder, List list) {
        super.bindView(holder, list);

        holder.postedBy.setText(postedBy);
        holder.postedBy.setTypeface(null, Typeface.BOLD);
        holder.startDateTimeInEpoch.setText(startDateTimeInEpoch);
        holder.startDateTimeInEpoch.setVisibility(View.INVISIBLE);
        holder.endDateTimeInEpoch.setText(endDateTimeInEpoch);
        holder.endDateTimeInEpoch.setVisibility(View.INVISIBLE);

        MainActivity.filterButton.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                holder.geoQuery = holder.geoFireReference.queryAtLocation(new GeoLocation(holder.currentLatDouble, holder.currentLngDouble), 5);

            holder.geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
                @Override
                public void onKeyEntered(String key, GeoLocation location) {
                    primaryDBKey.child(key).child("startDateTimeInEpoch").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (dataSnapshot.getValue() != null) {
                                holder.startTimeDateInEpochLong2 = Long.parseLong(dataSnapshot.getValue().toString());
                                holder.now = System.currentTimeMillis() / 1000;
                                holder.diffNowsdtel.add(holder.startTimeDateInEpochLong2 - holder.now);
                                Log.d("log1", String.valueOf(holder.diffNowsdtel));

                                Handler handler = new Handler();
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        if(holder.mContext instanceof MainActivity){
                                            ((MainActivity)holder.mContext).filterRequests(holder.diffNowsdtel);
                                            Log.d("log2", String.valueOf(holder.diffNowsdtel));
                                        }
                                    }
                                }, 1500);
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                }

                @Override
                public void onKeyExited(String key) {

                }

                @Override
                public void onKeyMoved(String key, GeoLocation location) {

                }

                @Override
                public void onGeoQueryReady() {

                }

                @Override
                public void onGeoQueryError(DatabaseError error) {

                }
            });
                return true;
            }
        });

    }

    /**
     * our ItemFactory implementation which creates the ViewHolder for our adapter.
     * It is highly recommended to implement a ViewHolderFactory as it is 0-1ms faster for ViewHolder creation,
     * and it is also many many timesa more efficient if you define custom listeners on views within your item.
     */
    protected static class ItemFactory implements ViewHolderFactory<ViewHolder> {
        public ViewHolder create(View v) {
            return new ViewHolder(v);
        }
    }

    /**
     * return our ViewHolderFactory implementation here
     *
     * @return
     */
    @Override
    public ViewHolderFactory<? extends ViewHolder> getFactory() {
        return FACTORY;
    }


    // Manually create the ViewHolder class
    protected static class ViewHolder extends RecyclerView.ViewHolder {

        TextView postedBy, userID, currentLt, currentLn, requestID, postedFrom;
        TextView startDateTimeInEpoch, endDateTimeInEpoch, diffNowsdtelTV;
        LinearLayout linearLayout;
        long difference, differenceCurrentStartTime, handlerGap;
        long startTimeDateInEpochLong2;
        public static long now;
        List<Long> diffNowsdtel;
        Context mContext;
        DatabaseReference firebaseDatabase;
        GeoFire geoFireReference;
        GeoQuery geoQuery;


        public ViewHolder(final View itemView) {
            super(itemView);

            postedBy = (TextView) itemView.findViewById(R.id.postedBy);
            startDateTimeInEpoch = (TextView) itemView.findViewById(R.id.startTimeDateInEpoch);
            endDateTimeInEpoch = (TextView) itemView.findViewById(R.id.endTimeDateInEpoch);
            diffNowsdtelTV = (TextView) itemView.findViewById(R.id.diffNowsdtelTV);

            this.mContext = itemView.getContext();

        private boolean isNetworkAvailable() {
            ConnectivityManager connectivityManager
                    = (ConnectivityManager) itemView.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null && activeNetworkInfo.isConnected();
        }
    }
}

问题 是当我记录 log1 时,我得到了 Logcat 中显示的所有 3 个值,但是当我' m logging log2 仅显示最后计算的值,这是调用 filterRequests(Long l) 所使用的值。

更新 - 更新适配器代码后,log1log2 现在显示为:

 D/log1: [2197]
 D/log1: [2197, -1007]
 D/log1: [2197, -1007, 4003]

 D/log2: [2197, -1007, 4003]

filterRequests() 方法是完成基于时间过滤内容的逻辑的方法。 filterRequests() 中的参数是 holder.diffNowsdtel,它现在有 3 个 long 值,应该根据它执行逻辑。如果长值是 <=900长值 -1007 的内容应该显示,当长值为 >900 时,长值 21974003 的内容应该显示。

代码如下:

public void filterRequests(final List<Long> l) {

    final int size = l.size();

            Log.d("lng", String.valueOf(l));

                    if (isNetworkAvailable()) {
                        if (chkBoxLiveRqsts.isChecked()) {



                                    firebaseDatabase.child(key).addValueEventListener(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot) {
                                            if (dataSnapshot.getValue() != null) {
                                                for (int i = 0; i < size; i++){
                                                    if (l.get(i) <= 900) {
                                                    ...       
                                                    } else {
                                                    }
                                                }
                                                progressDialogAdding.dismiss();
                                            } else {
                                            }
                                        }

                                        @Override
                                        public void onCancelled(DatabaseError databaseError) {
                                        }
                                    });
                                }

                                ...
                            });
                        } else if (chkBoxSFLRqsts.isChecked()) {
                            fastItemAdapter.clear();
                            firebaseDatabase.child(key).addValueEventListener(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot) {
                                            if (dataSnapshot.getValue() != null) {
                                                for (int i = 0; i < size; i++) {
                                                    if (l.get(i) > 900) {
                                                        ...

                                                    } else {
                                                    }
                                                }
                                                progressDialogAdding.dismiss();
                                            } else {
                                            }
                                        }

                                        @Override
                                        public void onCancelled(DatabaseError databaseError) {
                                        }
                                    });
                                }
                                ...
                        } 
                    } else {
                        Snackbar snackbar = Snackbar
                                .make(coordinatorLayout, "No internet connection", Snackbar.LENGTH_SHORT);
                        snackbar.show();
                        progressDialogAdding.dismiss();
                    }
                }
            });
            dialog = builder.create();
        dialog.show();
}

lng 的日志值:

D/lng: [2197, -1007, 4003]

我想要的filterRequests(Long l) 方法应该使用 holder.diffNowsdtelTV.getText().toString() 的所有值并使用它们执行逻辑。

对于模棱两可的问题,我深表歉意。请帮我解决这个问题!

尝试在onBindView中设置初始化你想要的值

   Log.d("diffNowsdtel", holder.diffNowsdtelTV.getText().toString());
    final String val = holder.diffNowsdtelTV.getText().toString();

            MainActivity.filterButton.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    if(holder.mContext instanceof MainActivity){
                        ((MainActivity)holder.mContext).filterRequests(Long.parseLong(val));
                        Log.d("diffNowsdtel2",val);
                    }
                    return true;
                }
            });

您在 MainActivity 中有一个名为 filterButton 的静态定义对象。每次为新视图调用 bindView 时,它都会替换该按钮的菜单项侦听器。因此,当您单击该菜单项时,它只会调用您为调用 bindView 的最后一项设置的处理程序。这就是为什么在按下筛选按钮时您只会看到一个日志条目的原因。

你在做什么

在你的 ViewHolderdiffNowsdtel 被声明为 long (保存最新值)每当你记录时 log1 Logger 显示(来自 diffNowsdtel 的最新值,因此您的日志显示不同的值,因为 bindView 在您更新行或完整数据集时调用多次)

D/log1: -22136
D/log1: -22403
D/log1: -25094

onMenuItemClick 中,您直接从 TextView 获取值,现在是 -25094,这就是为什么您的 TextView 中只有一个值并且日志显示

D/log2: -25094

你应该做什么

使用 holder.diffNowsdtelTV.setTag(your database key or row_id) 设置标签并在 onMenuItemClick 中使用

获取标签
Object someTagName = (Object) holder.diffNowsdtelTV.getTag();

现在使用存储在 someTagName 中的值从数据库中获取 3 个 String 值,然后进行计算。

编辑

实际上您需要 3 个值来进行计算,而在您当前的逻辑中,您只有最新的值存储在 diffNowsdtel 中。所以现在你需要一个逻辑来将你的值存储在某处和内部 onMenuItemClick 使用它们但是如果你要保存你的值你必须将你的 diffNowsdtel 更改为 long[]List<Long> 并在每个需要一些逻辑的 bindView 调用中保存你的每个值所以最简单的方法是传递你唯一的数据库列说主键并将它保存在你的 GRModelClass

String primaryDbKey;

public GRModelClass(String primaryDbKey, ...) {
    this.primaryDbKey = primaryDbKey;
    ....
}

在你的 onMenuItemClick 中使用 primaryDbKey 从你的数据库 table 中获取你的 3 String 值(你正在其他地方做)然后进行计算。


编辑

您用 Long 创建了一个列表,ListLong 都不是原始数据类型。 在您的问题中,您正在比较原始数据类型和非原始数据类型,为了进行比较,比较器的两侧应该是相同的数据类型。

你在做什么:

if (l.get(i) <= 900)

此处 l.get(i) returns 一个 Long 值,其中 900 是整数值。

你应该做什么:

只需通过 900L

将您的 l.get(i)Long 进行比较
if (l.get(i) <= 900L)

if (l.get(i) > 900L)