gpt4 book ai didi

android - RecyclerView 不交换适配器,onBindViewHolder IndexOutOfBounds

转载 作者:太空狗 更新时间:2023-10-29 14:51:36 28 4
gpt4 key购买 nike

我正在尝试在我的应用程序中利用我的 API 来更新基于 Google MapView 位置的 RecyclerView,因此这意味着该 API 将返回不同大小的列表,而且频率很高。初始应用程序加载后,当我更改位置时,我会触发一个新的 API 调用,该调用会创建一个新的适配器并调用 RecyclerView.swapAdapter() 方法。这似乎是一个 super 简单的解决方案,但我在 RecyclerView 或 RecyclerView.ViewHolder 文档中找不到任何内容来解决这个问题。

问题是:

  • 如果 API 返回的列表大于初始列表大小,recyclerview 将保持与初始加载列表大小相同的大小
  • 如果 API 返回的列表小于初始列表大小,则会出现运行时错误并返回 IndexOutOfBoundsException

这是我的基本代码结构:

在我的 API 类中

public void getFeed(final RecyclerView recView, final Location loc){

service().getFeed(MyFeeds.API_FEED).enqueue(new Callback<NearbyPeopleProto.NearbyPeopleFeed>() {
@Override
public void onResponse(Response<NearbyPeopleProto.NearbyPeopleFeed> response, Retrofit retrofit) {

Log.d(LOG_TAG, "recview Child count: " + recView.getChildCount());
ArrayList<NearbyPeople> near = new RtGtfsParser(c).getStopsByLocation(response.body(), loc);

/*Log.d(LOG_TAG, "onResponse: " + recView.getChildCount() + " -- " + near.get(0).getStops().size());
if(recView.getChildCount() > near.get(0).getStops().size()) {
//recView.removeViews(near.get(0).getStops().size() - 1, recView.getChildCount() - 1);
recView.removeViewAt(1);
Log.d(LOG_TAG, "onResponse: " + recView.getChildCount());
}*/

GroupListAdapter ta = new GroupListAdapter(c, near);
Log.d(LOG_TAG, "onResponse: Swapping");
//recView.setAdapter(ta);

recView.swapAdapter(ta, true);
}

@Override
public void onFailure(Throwable t) {
Log.e(LOG_TAG, t.getMessage());
}
});
}

我的适配器类

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

ArrayList<NearbyPeople> NearbyPeople;

public GroupListAdapter(ArrayList<NearbyPeople> NearbyPeople) {
this.NearbyPeople = NearbyPeople;
}

@Override
public GroupListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View stationView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);

return new ViewHolder(stationView);
}

@Override
public void onBindViewHolder(GroupListAdapter.ViewHolder vh, int position) {

if(NearbyPeople.get(0).getStops() != null) {
Log.d(LOG_TAG, "onBindViewHolder: " + NearbyPeople.get(0).getStops().size());
vh.locationTag.setText(NearbyPeople.get(0).getStops().get(position).stopName);
} else {
vh.locationTag.setText("Loading...");
}
}

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

public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView locationTag;

public ViewHolder(View itemView) {
super(itemView);

locationTag = (TextView) itemView.findViewById(R.id.location_tag);
}
}
}

LogCat 输出:

01-12 22:36:27.718 6821-6821/net.mastrgamr.myapp D/TripActivity: onCameraChange: 
//API gets called on the initial app load
01-12 22:36:28.597 6821-6821/net.mastrgamr.myapp I/System.out: LOADING MAP
01-12 22:36:28.622 6821-6831/net.mastrgamr.myapp W/art: Suspending all threads took: 9.511ms
01-12 22:36:28.633 6821-6831/net.mastrgamr.myapp I/art: Background sticky concurrent mark sweep GC freed 16785(866KB) AllocSpace objects, 9(3MB) LOS objects, 0% free, 68MB/68MB, paused 16.852ms total 67.527ms
01-12 22:36:28.635 6821-6821/net.mastrgamr.myapp D/TripActivity: onMyLocationChange: Location[fused 40.882174,-73.833406 acc=50 et=+2d19h35m3s487ms]
01-12 22:36:28.685 6821-6831/net.mastrgamr.myapp I/art: Background partial concurrent mark sweep GC freed 2960(217KB) AllocSpace objects, 28(26MB) LOS objects, 24% free, 48MB/64MB, paused 5.893ms total 47.174ms
01-12 22:36:28.903 6821-6821/net.mastrgamr.myapp D/ApiService: recview Child count: 1
01-12 22:36:28.906 6821-6821/net.mastrgamr.myapp D/NearbyGroups: mapping to model
01-12 22:36:28.913 6821-6821/net.mastrgamr.myapp I/System.out: MAPPED!!: [net.mastrgamr.tpulse.models.realtime.NearbyGroups$Stops@6ebd215, net.mastrgamr.tpulse.models.realtime.NearbyGroups$Stops@da9092a]
01-12 22:36:28.913 6821-6821/net.mastrgamr.myapp D/NearbyGroups: mapping to model
01-12 22:36:28.926 6821-6821/net.mastrgamr.myapp I/System.out: MAPPED!!: [net.mastrgamr.tpulse.models.realtime.NearbyGroups$Stops@23fe31b, net.mastrgamr.tpulse.models.realtime.NearbyGroups$Stops@e481cb8]
01-12 22:36:28.927 6821-6821/net.mastrgamr.myapp D/ApiService: onResponse: Swapping
01-12 22:36:28.943 6821-6821/net.mastrgamr.myapp D/GroupListAdpter: onBindViewHolder: 2
01-12 22:36:28.952 6821-6821/net.mastrgamr.myapp D/GroupListAdpter: onBindViewHolder: 2
01-12 22:36:29.996 6821-6821/net.mastrgamr.myapp D/TripActivity: onCameraChange:
01-12 22:36:33.397 6821-6821/net.mastrgamr.myapp D/TripActivity: onCameraChange:
//API also gets called when the map is moved.
01-12 22:36:33.399 6821-6821/net.mastrgamr.myapp D/TripActivity: CAMERA MOVED -- Regenerating: Location[Moved Location 40.880845,-73.837932 acc=??? t=?!? et=?!?]
01-12 22:36:33.749 6821-6821/net.mastrgamr.myapp D/ApiService: recview Child count: 2
01-12 22:36:33.749 6821-6821/net.mastrgamr.myapp D/NearbyGroups: mapping to model
01-12 22:36:33.755 6821-6821/net.mastrgamr.myapp I/System.out: MAPPED!!: [net.mastrgamr.tpulse.models.realtime.NearbyGroups$Stops@90eab4a]
01-12 22:36:33.755 6821-6821/net.mastrgamr.myapp D/NearbyGroups: mapping to model
01-12 22:36:33.762 6821-6821/net.mastrgamr.myapp I/System.out: MAPPED!!: [net.mastrgamr.tpulse.models.realtime.NearbyGroups$Stops@8abaabb]
01-12 22:36:33.763 6821-6821/net.mastrgamr.myapp D/ApiService: onResponse: Swapping
01-12 22:36:33.770 6821-6821/net.mastrgamr.myapp D/GroupListAdpter: onBindViewHolder: 1
01-12 22:36:33.772 6821-6821/net.mastrgamr.myapp D/GroupListAdpter: onBindViewHolder: 1
01-12 22:36:33.778 6821-6821/net.mastrgamr.myapp D/AndroidRuntime: Shutting down VM
01-12 22:36:33.995 6821-6821/net.mastrgamr.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.mastrgamr.myapp, PID: 6821
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at net.mastrgamr.tpulse.adapters.GroupListAdpter.onBindViewHolder(GroupListAdpter.java:63)
at net.mastrgamr.tpulse.adapters.GroupListAdpter.onBindViewHolder(GroupListAdpter.java:25)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)

最佳答案

Swap 在两个 list 之间只有当它们的大小相同时才有效,如果 list 中的一个比您的另一个短或大swap 不会按预期工作,您得到的是假设的结果。

示例:String one = {"1","2"}String two = {"4","8","10"} 交换。你知道会发生什么最后一个元素将在 two 数组中被丢弃,因为 one 数组没有第三个元素。

虽然swapAdapter(); , 不显示文档,这是您可以假设的。

处理它的最好和最快的方法是制作一个新的适配器并将其分配给RecyclerView

关于android - RecyclerView 不交换适配器,onBindViewHolder IndexOutOfBounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34758251/

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