gpt4 book ai didi

java - 按字母顺序更新 RecyclerView 项目

转载 作者:搜寻专家 更新时间:2023-11-01 09:31:20 25 4
gpt4 key购买 nike

我有一个 recyclerView,我想按字母顺序对项目进行排序。我使用 Collections.sort 对列表进行排序,但是当我尝试使用 notifyDatasetChanged() 更新列表时,它显示无法访问的语句。

这个问题是否与适配器的范围有关?

这是我的 MainActivity:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setToolbar();
mSort = (TextView) findViewById(R.id.sort);
// mFilter = (TextView)findViewById(R.id.filter);
mRecyclerView = (RecyclerView) findViewById(R.id.kictstarterRecycler);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

fetchKickStarter(mRecyclerView);
setClickListner();
}

private void setClickListner() {
mSort.setOnClickListener(this);
//mFilter.setOnClickListener(this);
}

private void setToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);

final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
search(searchView);
return true;
}

private void fetchKickStarter(final RecyclerView mRecyclerView) {
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<JsonElement> call = apiService.getKickStarter();
call.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
Log.d("resonse", response.toString());
JsonElement object = response.body();
Type listType = new TypeToken<List<Kickstarter>>() {
}.getType();
mKickStarter = new Gson().fromJson(object.getAsJsonArray(), listType);
mAdapter = new KickstarterAdapter(mKickStarter, R.layout.kickstarter_row_layout, MainActivity.this);
mRecyclerView.setAdapter(mAdapter);
}

@Override
public void onFailure(Call<JsonElement> call, Throwable t) { }
});
}

@Override
public void onClick(View view) {
mSort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Collections.sort(mKickStarter, new Comparator<Kickstarter>() {
@Override
public int compare(Kickstarter item1, Kickstarter item2) {
return item1.getTitle().compareToIgnoreCase(item2.getTitle());
**mAdapter.notifyDataSetChanged()**;
}
});
}
});
}

我很困惑为什么我无法访问 mAdapter

最佳答案

      @Override
public void onClick(View view) {
mSort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Collections.sort(mKickStarter, new Comparator<Kickstarter>() {
@Override
public int compare(Kickstarter item1, Kickstarter item2) {
return item1.getTitle().compareToIgnoreCase(item2.getTitle());

}
});
}
});
mAdapter.notifyDataSetChanged();
}

关于java - 按字母顺序更新 RecyclerView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47049325/

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