gpt4 book ai didi

java - recyclerview 中的列表未排序

转载 作者:行者123 更新时间:2023-12-02 00:56:50 24 4
gpt4 key购买 nike

我在数据库中有一个电话号码和消息对的列表。我想在按选项菜单中的相应按钮后按电话号码或消息文本以相反的顺序对列表进行排序。

我使用 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 没有对我的列表进行排序?

最佳答案

不会是因为onLoadFinished中的代码吗?我不喜欢加载器,但您为回收器 View 设置了一个适配器,传递局部变量list。这意味着调用此代码后,您的适配器不再绑定(bind)到mLogList

关于java - recyclerview 中的列表未排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57812190/

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