[英]I have a Recycler View, onRun will display, but upon a button click it will be empty, I want to set a default recycler view
I currently have a recycler view that works off Latitude and Longitude within Android studio.我目前有一个回收站视图,可以在 Android 工作室中使用纬度和经度。 On load due to using an emulator i pass a default location, this loads areas around the location within a map.
在由于使用模拟器而加载时,我传递了一个默认位置,这会加载地图中该位置周围的区域。 I also have a spinner which allows you to change area and display markers on a map on screen.
我还有一个微调器,它允许您更改区域并在屏幕上的地图上显示标记。 However the problem is when the Location which in my case is the current location (set to default emulator location Google Pixel San Francisco?) is used, the recycler view shows as empty, i want to edit my recycler view within my Adapter class so that IF the view is empty set the text to "No places around you" if code needed please ask.
但是问题是当 Location 在我的例子中是当前位置(设置为默认模拟器位置 Google Pixel San Francisco?)被使用时,回收器视图显示为空,我想在我的 Adapter 类中编辑我的回收器视图,以便如果视图为空,请将文本设置为“No places around you” 如果需要代码请询问。
@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(" ");
}
}
Edit:编辑:
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);
}
}
}
Don't pass the list in constructor if it is dynamic.如果它是动态的,不要在构造函数中传递列表。
Add this function in your adapter在您的适配器中添加此功能
public void setPanelList(List<QuietPlace> quietPlaces){
mQuietPlace = quietPlaces;
notifyDataSetChanged();
}
In activity, setPanelList(quietPlaces) when your quietPlaces
changed.在活动中,当您的
quietPlaces
发生变化时,setPanelList(quietPlaces)。
quietPlaceAdapter.setPanelList(quietPlaces)
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.