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

Ham*_*sir 9 java methods android instanceof android-context

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

filterRequests(List <Long> l)是一种MainActivity基于long来过滤请求/内容的逻辑的方法l.

整个适配器:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题在于,当我正在记录时log1,我得到了所有显示的3个值Logcat,但是当我正在记录时,log2只显示最后计算的值,这是用于filterRequests(Long l)调用的值.

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

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

 D/log2: [2197, -1007, 4003]
Run Code Online (Sandbox Code Playgroud)

filterRequests()方法是完成基于时间过滤内容的逻辑的方法.它出现在参数filterRequests()holder.diffNowsdtel具有3个long值和现在做它应该做的基于它的逻辑..如果长值是<=900其具有长值的内容-1007应被示出和时长值>900,其具有所述内容很长的价值2197,4003应该显示.

这是代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

日志值lng:

D/lng: [2197, -1007, 4003]
Run Code Online (Sandbox Code Playgroud)

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

对于含糊不清的问题,我很抱歉.请帮我解决这个问题!

Har*_*shi 2

你在做什么

每当您记录log1ViewHolder diffNowsdtel时,您都会将其声明为(保存最新值) (最新值,因此您的日志显示不同的值,因为每当您更新行或完整数据集时都会调用多次)long LoggerdiffNowsdtelbindView

D/log1: -22136
D/log1: -22403
D/log1: -25094
Run Code Online (Sandbox Code Playgroud)

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

D/log2: -25094
Run Code Online (Sandbox Code Playgroud)

你应该做什么

使用设置标签holder.diffNowsdtelTV.setTag(your database key or row_id)并在您的内部onMenuItemClick使用获取标签

Object someTagName = (Object) holder.diffNowsdtelTV.getTag();
Run Code Online (Sandbox Code Playgroud)

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

编辑

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

String primaryDbKey;

public GRModelClass(String primaryDbKey, ...) {
    this.primaryDbKey = primaryDbKey;
    ....
}
Run Code Online (Sandbox Code Playgroud)

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


编辑

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

你在做什么:

if (l.get(i) <= 900)
Run Code Online (Sandbox Code Playgroud)

这里l.get(i)返回一个Long值,其中 as900是整数值。

你应该做什么:

l.get(i)只需将您的与Long做比较900L

if (l.get(i) <= 900L)
Run Code Online (Sandbox Code Playgroud)

或者

if (l.get(i) > 900L)
Run Code Online (Sandbox Code Playgroud)