gpt4 book ai didi

java - 为什么我的 recyclerview 适配器不起作用?/为什么该类没有运行?

转载 作者:行者123 更新时间:2023-12-02 13:39:17 25 4
gpt4 key购买 nike

我一直在尝试让我的 RecyclerView 适配器正常工作,但我无法在卡片上显示任何内容,而且 CycleTrails 类不会运行,因为 Logcat 不会显示通过类...在设置适配器之前,一切正常,我可以从 CycleTrails 类中获取所有值。但现在,事情变得一团糟,我不明白为什么。

MainActivity.java(onCreate 部分/我使用适配器添加的唯一内容)

@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvItem = (RecyclerView) findViewById(R.id.rvItem);
rvItem.setHasFixedSize(true);
LinearLayoutManager manager = new LinearLayoutManager(this);
rvItem.setLayoutManager(manager);
ArrayList<Trails> finalData = getTrails();
Adapter adapter = new Adapter(this, finalData);
rvItem.setAdapter(adapter);

CycleTrails.java

public class CycleTrails extends MainActivity {

public static double LAT_MAX = myLatitude + (0.01 * 200);
public static double LAT_MIN = myLatitude - (0.01 * 200);
public static double LON_MAX = myLongitude + (0.01 * 200);
public static double LON_MIN = myLongitude - (0.01 * 200);

public static ArrayList<Trails> getTrails() {
Log.d("trails", "Cycle trails running");
ArrayList<Trails> trailList = new ArrayList<>();
Trails t1 = new Trails(51.71, -3.36, "Bike Park Wales", "", "", "");
Trails t2 = new Trails(51.66, -3.35, "Mountain Ash", "", "", "");
Trails t3 = new Trails(50.50, -4.17, "FlyUp Downhill", "", "", "");
trailList.add(t1);
trailList.add(t2);
trailList.add(t3);


final ArrayList<Trails> localTrails = new ArrayList<>();
for (final Trails trail : trailList) {
Log.d("List running", "Local trail list");
if (trail.lat > LAT_MIN && trail.lat < LAT_MAX && trail.lon > LON_MIN && trail.lon < LON_MAX) {
localTrails.add(trail);
Log.d("Calc", "Run code");


final RequestBuilder weather = new RequestBuilder();

final Request request = new Request();
request.setLat(String.valueOf(trail.lat));
Log.d("Lat used", String.valueOf(trail.lat));
Log.d("Actual Lat", request.getLat());
request.setLng(String.valueOf(trail.lon));
Log.d("Lon used", String.valueOf(trail.lon));
request.setUnits(Request.Units.UK);
request.setLanguage(Request.Language.ENGLISH);
request.addExcludeBlock(Request.Block.CURRENTLY);
request.removeExcludeBlock(Request.Block.CURRENTLY);


weather.getWeather(request, new Callback<WeatherResponse>() {
@Override
public void success(WeatherResponse weatherResponse, Response response) {
Log.d("Weather", "We have weather!");
Log.d("Location", weatherResponse.getTimezone());
trail.temp = String.valueOf(weatherResponse.getCurrently().getTemperature());
Log.d("Temperature", trail.temp);
trail.weather = weatherResponse.getCurrently().getIcon();
}

@Override
public void failure(RetrofitError retrofitError) {
Log.d("Error while calling: ", "Weather");
}


});
} else {
Log.d("Too far", "Trails too far" + trail.name);
}
}
return localTrails;
}

}

适配器

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private Context context;
public ArrayList <Trails> finalData;

public Adapter (Context context, ArrayList<Trails> finalData) {
Log.d("Hello", "Adapter");
this.context = context;
this.finalData = finalData;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
Log.d("Hello", "2");
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.trail_card, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Trails trails = finalData.get(position);
Log.d("Hello", "3");
Picasso.with(context)
.load("http://www.wigglestatic.com/product-media/5360108808/Wiggle-Road-Bike-Road-Bikes-Black-1WGMY16R7048UK0001-6.jpg?w=2000&h=2000&a=7")
.placeholder(R.drawable.cycle)
.error(android.R.drawable.stat_notify_error)
.into(holder.locationImage);
holder.locationText.setText(trails.name);
Log.d("Location", trails.name);
holder.temperatureText.setText(trails.temp);
Log.d("Temperature", trails.temp);
holder.weatherText.setText(trails.weather);
holder.conditionsText.setText(trails.conditions);
}

@Override
public int getItemCount() {
Log.d("Hello", "4");
if(finalData != null) {
Log.d("Hello", "5");
return finalData.size();
}
return 0;
}

public static class ViewHolder extends RecyclerView.ViewHolder {

public CardView cardView;
public ImageView locationImage;
public TextView locationText;
public TextView temperatureText;
public TextView weatherText;
public TextView conditionsText;

public ViewHolder(View itemView) {
super(itemView);
cardView = (CardView)itemView.findViewById(R.id.cardView);
locationImage = (ImageView)itemView.findViewById(R.id.locationImage);
locationText = (TextView)itemView.findViewById(R.id.locationText);
temperatureText = (TextView)itemView.findViewById(R.id.temperatureText);
weatherText = (TextView)itemView.findViewById(R.id.weatherText);
conditionsText = (TextView)itemView.findViewById(R.id.conditionsText);
}
}
}

Trails.java

public class Trails {
public String name;
public double lat; // Object fields
public double lon;
public String temp;
public String weather;
public static String conditions;


Trails(double lat, double lon, String name, String temp, String weather, String conditions){ // construcor
this.lat = lat;
this.lon = lon;
this.name = name;
this.temp = temp;
this.weather = weather;
this.conditions = conditions;
}
}

这是整个应用程序运行的 logcat

03-16 10:32:48.877 20535-20535/com.example.android.cycleuk D/Lat: 52.1981981981982
03-16 10:32:48.877 20535-20535/com.example.android.cycleuk D/Lon: -2.2341297062250933
03-16 10:33:19.859 20535-20542/com.example.android.cycleuk W/art: Suspending all threads took: 10.141ms
03-16 10:33:19.956 20535-20535/com.example.android.cycleuk D/Hello: Adapter
03-16 10:33:20.023 20535-20535/com.example.android.cycleuk D/Hello: 4
03-16 10:33:20.023 20535-20535/com.example.android.cycleuk D/Hello: 4
03-16 10:33:20.069 20535-20535/com.example.android.cycleuk D/Lat: 52.1981981981982
03-16 10:33:20.069 20535-20535/com.example.android.cycleuk D/Lon: -2.2341297062250933

最佳答案

将您的适配器与recycleview绑定(bind)..

ArrayList<Trails> finalData = getTrails();
Adapter adapter = new Adapter(this, finalData);
rvItem.setAdapter(adapter)

关于java - 为什么我的 recyclerview 适配器不起作用?/为什么该类没有运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42825396/

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