recyclerview 中的列表未排序
List in recyclerview not sorting
我在数据库中有一个 phone 号码和消息对的列表。我想按选项菜单中的相应按钮后按 phone 数字或消息文本的相反顺序对列表进行排序。
我用 compare
实现了 Collections.sort
,并用 notifyDataSetChanged()
更新了适配器。但是,当我按下按钮时,没有任何反应。作为测试,我尝试在 onViewCreated()
的末尾调用排序方法和 notifyDataSetChanged()
,但也没有排序。
public class LogScreen extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int LOG_LOADER_ID = 0;
private LogAdapter mLogAdapter;
private RecyclerView mRecyclerView;
List<LogEntryItem> mLogList = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_log_screen, parent, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_log);
int recyclerViewOrientation = LinearLayoutManager.VERTICAL;
boolean shouldReverseLayout = false;
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), recyclerViewOrientation, shouldReverseLayout);
mRecyclerView.setLayoutManager(layoutManager);
mLogAdapter = new LogAdapter(getContext(), mLogList);
mRecyclerView.setAdapter(mLogAdapter);
getLoaderManager().initLoader(LOG_LOADER_ID, null, this);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_log_screen, menu);
MenuItem buttonSortByPhone = menu.findItem(R.id.log_screen_sort_by_phone);
MenuItem buttonSortByMessage = menu.findItem(R.id.log_screen_sort_by_message);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.log_screen_sort_by_phone:
reverseSortComparePhoneNumber();
return true;
case R.id.log_screen_sort_by_message:
reverseSortCompareMessageSent();
return true;
default:
break;
}
return false;
}
public void reverseSortComparePhoneNumber() {
Collections.sort(mLogList, new Comparator<LogEntryItem>() {
@Override
public int compare(LogEntryItem itemA, LogEntryItem itemB) {
return itemB.getPhoneNumber().compareTo(itemA.getPhoneNumber());
}
});
mLogAdapter.notifyDataSetChanged();
}
public void reverseSortCompareMessageSent() {
Collections.sort(mLogList, new Comparator<LogEntryItem>() {
@Override
public int compare(LogEntryItem itemA, LogEntryItem itemB) {
return itemB.getMessageSent().compareTo(itemA.getMessageSent());
}
});
mLogAdapter.notifyDataSetChanged();
}
@NonNull
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new CursorLoader(getContext(), LogContentProvider.CONTENT_URI, null, null, null, null);
}
@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
List<LogEntryItem> list = new ArrayList<>();
while (cursor.moveToNext()) {
int index1 = cursor.getColumnIndex(LogDBHelper.COLUMN_PHONE_NUMBER);
int index2 = cursor.getColumnIndex(LogDBHelper.COLUMN_MESSAGE);
String phoneNumber = cursor.getString(index1);
String messageSent = cursor.getString(index2);
LogEntryItem logEntry = new LogEntryItem(phoneNumber, messageSent);
list.add(logEntry);
}
mLogAdapter = new LogAdapter(getContext(), list);
mRecyclerView.setAdapter(mLogAdapter);
}
@Override
public void onLoaderReset(@NonNull Loader<Cursor> loader) {
}
}
为什么 Collections.sort
没有对我的列表进行排序?
对于按字典顺序对您的 LogEntryItem 消息进行反向排序:
class SortbyMessage implements Comparator<LogEntryItem> {
@Override
public int compare(LogEntryItem a, LogEntryItem b) {
return a.getMessageSent().compareTo(b.getMessageSent());
}
}
现在你可以这样使用了
Comparator comparator = Collections.reverseOrder(new SortbyMessage());
Collections.sort(ar, comparator);
除了
,数字排序与上面类似
return a.getPhoneNumber() - b.getPhoneNumber();
或在您的 reverseSortComparePhoneNumber 方法中将您的 return 更改为
itemB.getPhoneNumber() - itemA.getPhoneNumber();
难道是因为onLoadFinished
中的代码?我不喜欢加载程序,但你在那里设置了一个适配器,用于传递局部变量 list
的回收器视图。这意味着调用此代码后,您的适配器将不再绑定到 mLogList
。
我在数据库中有一个 phone 号码和消息对的列表。我想按选项菜单中的相应按钮后按 phone 数字或消息文本的相反顺序对列表进行排序。
我用 compare
实现了 Collections.sort
,并用 notifyDataSetChanged()
更新了适配器。但是,当我按下按钮时,没有任何反应。作为测试,我尝试在 onViewCreated()
的末尾调用排序方法和 notifyDataSetChanged()
,但也没有排序。
public class LogScreen extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int LOG_LOADER_ID = 0;
private LogAdapter mLogAdapter;
private RecyclerView mRecyclerView;
List<LogEntryItem> mLogList = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_log_screen, parent, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_log);
int recyclerViewOrientation = LinearLayoutManager.VERTICAL;
boolean shouldReverseLayout = false;
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), recyclerViewOrientation, shouldReverseLayout);
mRecyclerView.setLayoutManager(layoutManager);
mLogAdapter = new LogAdapter(getContext(), mLogList);
mRecyclerView.setAdapter(mLogAdapter);
getLoaderManager().initLoader(LOG_LOADER_ID, null, this);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_log_screen, menu);
MenuItem buttonSortByPhone = menu.findItem(R.id.log_screen_sort_by_phone);
MenuItem buttonSortByMessage = menu.findItem(R.id.log_screen_sort_by_message);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.log_screen_sort_by_phone:
reverseSortComparePhoneNumber();
return true;
case R.id.log_screen_sort_by_message:
reverseSortCompareMessageSent();
return true;
default:
break;
}
return false;
}
public void reverseSortComparePhoneNumber() {
Collections.sort(mLogList, new Comparator<LogEntryItem>() {
@Override
public int compare(LogEntryItem itemA, LogEntryItem itemB) {
return itemB.getPhoneNumber().compareTo(itemA.getPhoneNumber());
}
});
mLogAdapter.notifyDataSetChanged();
}
public void reverseSortCompareMessageSent() {
Collections.sort(mLogList, new Comparator<LogEntryItem>() {
@Override
public int compare(LogEntryItem itemA, LogEntryItem itemB) {
return itemB.getMessageSent().compareTo(itemA.getMessageSent());
}
});
mLogAdapter.notifyDataSetChanged();
}
@NonNull
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new CursorLoader(getContext(), LogContentProvider.CONTENT_URI, null, null, null, null);
}
@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
List<LogEntryItem> list = new ArrayList<>();
while (cursor.moveToNext()) {
int index1 = cursor.getColumnIndex(LogDBHelper.COLUMN_PHONE_NUMBER);
int index2 = cursor.getColumnIndex(LogDBHelper.COLUMN_MESSAGE);
String phoneNumber = cursor.getString(index1);
String messageSent = cursor.getString(index2);
LogEntryItem logEntry = new LogEntryItem(phoneNumber, messageSent);
list.add(logEntry);
}
mLogAdapter = new LogAdapter(getContext(), list);
mRecyclerView.setAdapter(mLogAdapter);
}
@Override
public void onLoaderReset(@NonNull Loader<Cursor> loader) {
}
}
为什么 Collections.sort
没有对我的列表进行排序?
对于按字典顺序对您的 LogEntryItem 消息进行反向排序:
class SortbyMessage implements Comparator<LogEntryItem> {
@Override
public int compare(LogEntryItem a, LogEntryItem b) {
return a.getMessageSent().compareTo(b.getMessageSent());
}
}
现在你可以这样使用了
Comparator comparator = Collections.reverseOrder(new SortbyMessage());
Collections.sort(ar, comparator);
除了
,数字排序与上面类似return a.getPhoneNumber() - b.getPhoneNumber();
或在您的 reverseSortComparePhoneNumber 方法中将您的 return 更改为
itemB.getPhoneNumber() - itemA.getPhoneNumber();
难道是因为onLoadFinished
中的代码?我不喜欢加载程序,但你在那里设置了一个适配器,用于传递局部变量 list
的回收器视图。这意味着调用此代码后,您的适配器将不再绑定到 mLogList
。