gpt4 book ai didi

java - 我有一个回收器 View ,onRun 将显示,但单击按钮后它将为空,我想设置默认回收器 View

转载 作者:行者123 更新时间:2023-12-01 19:51:59 24 4
gpt4 key购买 nike

我目前有一个回收器 View ,可以在 Android studio 中使用纬度和经度。由于使用模拟器而加载时,我传递了一个默认位置,这会加载 map 中该位置周围的区域。我还有一个微调器,可以让您更改区域并在屏幕上的 map 上显示标记。但是问题是,当使用位置(在我的情况下是当前位置(设置为默认模拟器位置 Google Pixel San Francisco?)时,回收器 View 显示为空,我想在 Adapter 类中编辑我的回收器 View ,以便如果 View 为空,请将文本设置为“您周围没有地方”,如果需要代码,请询问。

    @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

QuietPlace quietPlace = mQuietPlace.get(position);
if (mQuietPlace.size() > 0){
TextView textView = holder.titleTextView;
textView.setText(quietPlace.getName());
TextView textView2 = holder.noiseTextView;
textView2.setText(quietPlace.getNoise());
TextView textView3 = holder.distanceTextView;
textView3.setText(String.valueOf(quietPlace.getDistance()));
}
else {
TextView textViewEmpty = holder.titleTextView;
textViewEmpty.setText("No Quiet Places near your location");
TextView textViewEmpty2 = holder.noiseTextView;
textViewEmpty2.setText(" ");
TextView textViewEmpty3 = holder.distanceTextView;
textViewEmpty3.setText(" ");
}
}

编辑:

package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class QuietPlaceAdapter extends RecyclerView.Adapter<QuietPlaceAdapter.ViewHolder> {

private List<QuietPlace> mQuietPlace;

public QuietPlaceAdapter(List<QuietPlace> quietPlaces)
{
mQuietPlace = quietPlaces;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View quietPlaceView = inflater.inflate(R.layout.recycler_view, parent, false);
ViewHolder viewHolder = new ViewHolder(quietPlaceView);
return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
QuietPlace quietPlace = mQuietPlace.get(position);

TextView textView = holder.titleTextView;
textView.setText(quietPlace.getName());
TextView textView2 = holder.noiseTextView;
textView2.setText(quietPlace.getNoise());
TextView textView3 = holder.distanceTextView;
textView3.setText(String.valueOf(quietPlace.getDistance()));
}

@Override
public int getItemCount() {
return mQuietPlace.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
public TextView titleTextView;
public TextView noiseTextView;
public TextView distanceTextView;
public ViewHolder(View itemView) {
super(itemView);

titleTextView = (TextView) itemView.findViewById(R.id.quietPlaceTitle);
noiseTextView = (TextView) itemView.findViewById(R.id.quietPlaceNoise);
distanceTextView = (TextView) itemView.findViewById(R.id.quietPlaceDistance);
}
}

}

最佳答案

如果列表是动态的,请勿在构造函数中传递列表。

在您的适配器中添加此功能

public void setPanelList(List<QuietPlace> quietPlaces){

mQuietPlace = quietPlaces;
notifyDataSetChanged();
}

在 Activity 中,当 quietPlaces 更改时 setPanelList(quietPlaces)。

quietPlaceAdapter.setPanelList(quietPlaces)

希望这有帮助。

关于java - 我有一个回收器 View ,onRun 将显示,但单击按钮后它将为空,我想设置默认回收器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59079812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com