gpt4 book ai didi

android - 回收站 View : No adapter attached

转载 作者:行者123 更新时间:2023-11-30 00:59:34 25 4
gpt4 key购买 nike

我知道这个问题已经被问过了,但是看了答案,我还是想不出解决办法。这是我的适配器:

 public static class StarredReposViewHolder extends RecyclerView.ViewHolder {
LinearLayout starredReposLayout;
TextView starredRepoName;

public StarredReposViewHolder(View v) {
super(v);
starredReposLayout = (LinearLayout) v.findViewById(R.id.repo_starred_item_layout);
starredRepoName = (TextView) v.findViewById(R.id.starredepoName);
}
}

public StarredReposAdapter(List<GitHubstarredRepos> repos, int rowLayout, Context context) {
this.starredRepos = repos;
this.rowLayout = rowLayout;
this.context = context;
}

@Override
public StarredReposAdapter.StarredReposViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new StarredReposViewHolder(view);
}


@Override
public void onBindViewHolder(StarredReposViewHolder holder, int position) {
holder.starredRepoName.setText(starredRepos.get(position).getStarredName());
}

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

这是我加载它的方法:

 public void loadStarredRepos (View view){

final RecyclerView recyclerViewSt = (RecyclerView) findViewById(R.id.starred_repos_recycler_view);
recyclerViewSt.setLayoutManager(new LinearLayoutManager(this));

GitHubStarredRepoAPI apiService =
ApiClient.getClient().create(GitHubStarredRepoAPI.class);

Call<List<GitHubstarredRepos>> call = apiService.getStarredRepoName(newString);
call.enqueue(new Callback<List<GitHubstarredRepos>>() {
@Override
public void onResponse(Call<List<GitHubstarredRepos>> call, Response<List<GitHubstarredRepos>>
response) {

recyclerViewSt.setAdapter(new StarredReposAdapter(response.body(), R.layout.list_item_starred_repo,
getApplicationContext()));
}

@Override
public void onFailure(Call<List<GitHubstarredRepos>> call, Throwable t) {
// Log error here since request failed
Log.e("Repos", t.toString());
}

});

}

onResponse 的正文输入正确。问题不可能是它是在 onCreate 中定义的,因为我在同一屏幕上有另一个回收 View ,是从不同的方法加载的。谢谢!

PS:response.body()的响应Here

最佳答案

好吧,从实践来看,你应该在 #onCreate() 的 mainThread 中有你的 RecyclerView#setAdapter()


public void onCreate() {
recyclerViewSt.setAdapter(myAdapter);
}

您需要在 onResponse() 内部更新的是用于为适配器提供数据的数据集。

您可以将 response.body() 提取到 Activity 中的全局变量中,它返回一个对象数组(List repos)。喜欢:


List<GitHubstarredRepos> myDatasource = new ArrayList<>();

然后使用它:


myAdapter = new StarredReposAdapter(myDatasource, R.layout.list_item_starred_repo, getApplicationContext()));

接下来,网络调用的回调里面:


onResponse(response) {
myDatasource.clear();
myDatasource.addAll(response.body());
myRecyclerview.notifyDatasetChanged();
}
瞧,我的伪代码,不确定语法;)

关于android - 回收站 View : No adapter attached,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39630222/

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