我有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
。
答案 0 :(得分:1)
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_NUMBER_INT token
at [Source: 1 2 3; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1122)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1075)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:338)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:269)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:259)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:26)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2842)
at com.sample.Test.main(Test.java:17)
答案 1 :(得分:1)
也就是说,你在return语句后调用mAdapter.notifyDataSetChanged()
。
返回语句将在该点返回一个值。返回值时,代码执行模式也会路由到值接收方法。
return
之后的语句是无法访问的语句。
试试这个
@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();
});
}