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

时间:2016-12-01 13:22:50

标签: java android methods android-context instanceof

我从数据库中提取了3个String值,然后我将其转换为Long,然后我计算差异,然后将此计算结果{ {1}}方法中的值作为参数。我使用FastAdapter

LongfilterRequests(List <Long> l)中的一种方法,它根据长MainActivity执行过滤请求/内容的逻辑。

整个适配器:

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(); } } } 时,我获得log1中显示的所有3个值,但是当我&#39 ; m记录Logcat仅显示最后计算的值,这是使用log2调用的值。

更新 - 更新适配器代码后,filterRequests(Long l)log1现在显示:

log2

D/log1: [2197] D/log1: [2197, -1007] D/log1: [2197, -1007, 4003] D/log2: [2197, -1007, 4003] 方法是完成基于时间过滤内容的逻辑的方法。 filterRequests()中的参数是filterRequests(),现在有3个holder.diffNowsdtel值并且应该根据它执行逻辑..如果long值是long应显示具有长值<=900的内容,当长值为-1007时,应显示具有长值>9002197的内容。

这里是代码:

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(); } 的日志值:

lng

我想要的是D/lng: [2197, -1007, 4003] 方法应该使用filterRequests(Long l)的所有值并使用它们来执行逻辑。

我抱歉含糊不清的问题。请帮我解决这个问题!

3 个答案:

答案 0 :(得分:2)

你在做什么

只要您记录 log1 ViewHolderdiffNowsdtel long被声明为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();

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

修改

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

GRModelClass

String primaryDbKey; public GRModelClass(String primaryDbKey, ...) { this.primaryDbKey = primaryDbKey; .... } 内使用onMenuItemClick从数据库表中获取3 primaryDbKey个值(您正在其他地方执行此操作),然后进行计算。

修改

您使用String制作了一个列表,LongList都不是原始数据类型。 在您的问题中,您要比较原始数据类型和非原始数据类型,以便比较比较器的两侧应具有相同的数据类型。

你在做什么:

Long

此处if (l.get(i) <= 900) 返回l.get(i)值,其中Long为整数值。

你应该做什么:

只需将您的900l.get(i)进行比较Long

900L

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

答案 1 :(得分:1)

尝试在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;
                }
            });

答案 2 :(得分:1)

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