我有两个模型是Message和User.The Fragment我收到消息列表和带有asyncTaskAdapters的用户列表,我希望在listview项目上显示这些列表中的数据。
它应该显示一些消息和用户名
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<select name="type" id="type">
<option value="">Choose a Type</option>
<option value="music">Music</option>
<option value="art">Art</option>
<option value="cook">Cook</option>
</select>
<select name="group" id="group">
<option value="">Choose a group</option>
<option value="band" data="music">Band</option>
<option value="guitarplayer" data="music">Guitar Player</option>
<option value="painter" data="art">Painter</option>
<option value="graffer" data="art">Graffeur</option>
<option value="chief" data="cook">Chief</option>
</select>
<select id="user_job_title_str"></select>
我有点想使用ArrayAdapter
public class DialogsFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
public static final String USER = "user";
private static final String LOG_TAG = DialogsFragment.class.getSimpleName();
private static final int LOADER_MESSAGES_ID = 1;
private static final int LOADER_USERS_ID = 2;
long dialogsRequest = -1;
long usersRequest = -1;
private List<Message> messageList = new ArrayList<>();
private List<User> userList = new ArrayList<>();
private List<Long> usersIdList = new ArrayList<>();
private ListMessagesAdapter mAdapter;
private LoaderManager loaderManager;
@InjectView(R.id.swipe_refresh)
SwipeRefreshLayout refreshLayout;
@InjectView(R.id.footer_conversation)
RelativeLayout mFooter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.activity_dialog_view, container, false);
ButterKnife.inject(this, layout);
refreshLayout.setOnRefreshListener(this);
loadDialogs();
return layout;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mAdapter = new ListMessagesAdapter(messageList, userList, getActivity());
SpeedyQuickReturnListViewOnScrollListener scrollListener =
new SpeedyQuickReturnListViewOnScrollListener
.Builder(getActivity()
, QuickReturnViewType.GOOGLE_PLUS).build();
setListAdapter(mAdapter);
scrollListener.registerExtraOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
}
@Override
public void onScroll(AbsListView absListView, int first, int visibleCount, int total) {
boolean loadMore = /* maybe add a padding*/
first + visibleCount >= total - 1;
if (loadMore && total > 0) {
dialogsRequest = getServiceHelper().getDialogs(total - 1, 25);
}
}
});
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Message message = getOnClickMessage(position);
Log.i(LOG_TAG, "user id from msg " + message.getUserId());
User user = User.UTIL.getUser(message.getUserId(), userList);
Log.i(LOG_TAG, "user id " + user.getId());
Intent intentChatActivity = new Intent(getActivity(), HistoryActivity.class);
intentChatActivity.putExtra(USER, user);
startActivity(intentChatActivity);
}
});
getListView().setOnScrollListener(scrollListener);
}
@Override
public void onRefresh() {
Log.i(LOG_TAG, " refresh start");
dialogsRequest = getServiceHelper().getDialogs(0, 25);
refreshLayout.setRefreshing(true);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onServiceCallback(long requestId, Intent requestIntent, int resultCode, Bundle data) {
super.onServiceCallback(requestId, requestIntent, resultCode, data);
if (requestId == dialogsRequest) {
Log.i(LOG_TAG, "message request complete " + requestId);
loaderManager.getLoader(LOADER_MESSAGES_ID).forceLoad();
} else if (requestId == usersRequest) {
Log.i(LOG_TAG, "user request complete " + requestId);
loaderManager.getLoader(LOADER_USERS_ID).forceLoad();
}
}
private LoaderCallbacks<Dialogs> messagesCallbacks = new LoaderCallbacks<Dialogs>() {
@Override
public Loader<Dialogs> onCreateLoader(int id, Bundle args) {
DialogsAsyncLoader messagesLoader = new DialogsAsyncLoader(getActivity());
return messagesLoader;
}
@Override
public void onLoadFinished(Loader<Dialogs> loader, Dialogs data) {
messageList = data.getDialogs();
Log.i(LOG_TAG, "message size from request " + messageList.size());
if (messageList.size() > 0) {
String userIds = User.UTIL.getIds(messageList);
usersRequest = getServiceHelper().getUsers(userIds);
Log.i(LOG_TAG, "start userRequest " + usersRequest);
}
mAdapter.setMessages(messageList);
}
@Override
public void onLoaderReset(Loader<Dialogs> loader) {
}
};
private LoaderCallbacks<List<User>> usersCallback = new LoaderCallbacks<List<User>>() {
@Override
public Loader<List<User>> onCreateLoader(int id, Bundle args) {
UsersAsyncLoader usersLoader = new UsersAsyncLoader(getActivity());
return usersLoader;
}
@Override
public void onLoadFinished(Loader<List<User>> loader, List<User> data) {
userList = data;
mAdapter.setUsers(userList);
refreshLayout.setRefreshing(false);
}
@Override
public void onLoaderReset(Loader<List<User>> loader) {
mAdapter.setUsers(new ArrayList<User>());
}
};
private Message getOnClickMessage(int positon) {
return messageList.get(positon);
}
private void loadDialogs() {
if (loaderManager == null) {
loaderManager = getLoaderManager();
}
loaderManager.initLoader(LOADER_MESSAGES_ID, null, messagesCallbacks);
loaderManager.initLoader(LOADER_USERS_ID, null, usersCallback);
dialogsRequest = getServiceHelper().getDialogs(0, 25);
}
但是,当片段启动时,它会显示没有用户名的消息,需要等待5-10秒才能显示用户。如何同步这些列表的显示?
重要时刻:
每条消息都有user_id,所以当我从db获取消息列表时 dialogsAsyncLoader我构建包含user_ids的行,然后我使用 这行ID用于执行获取用户列表的请求
答案 0 :(得分:0)
所以我假设您从本地数据库加载数据。如果是这种情况,我建议你一次性加载所有内容。定义一个可能如下所示的新对象:
UserWithMessages {
User mUser;
Message[] mMessages;
}
并定义您的加载器以返回此类对象。您可以从装载程序加载数据并使其在后台加载方法中适应这种新类型。
或者,如果您坚持继续使用当前的解决方案,则可以通过简单检查相关的加载程序“onLoadFinish
”来检查另一个加载程序是否已完成,并使用两个列表(用户和消息)同时。当然,如果你说它需要几秒钟,那么某种进度条是可取的。