簡體   English   中英

滾動到底部android時,listview中的LoadMore清除上一項?

[英]LoadMore in listview clears previous item when scrolled at bottom android?

我正在使用loadmore listview獲取項目。 最初我得到20個項目並顯示。但是問題是,當我滾動底部時,它將獲取下一個20個項目,但之前的20個項目被清除。 如何解決這個問題? 這是代碼。

 public class ActivityChatView extends ActivityBase implements
    OnLoadMoreListener {
private LoadMoreListView chatList;
private Intent intent;
private String expertName, chatId;
private int offset = 0;
ArrayList<History> chatHistoryList;
private AdapterChatView mAdapter;
private ProgressBar progressBar;
private TextView progressText;
private boolean isProgressNeeded = true;
private String rowId;

protected void setContentToLayout() {
    setContentView(R.layout.activity_chat_view);
    initChatListView();
    offset = 0;

}

private void initChatListView() {
    intent = getIntent();
    chatList = (LoadMoreListView) findViewById(R.id.list_chat_data);
    progressBar = (ProgressBar) findViewById(R.id.probar);
    progressText = (TextView) findViewById(R.id.loading_text);
    progressBar.setVisibility(View.VISIBLE);
    progressText.setVisibility(View.VISIBLE);
    chatList.setEmptyView(findViewById(android.R.id.empty));
    chatList.setOnLoadMoreListener(this);
    expertName = intent.getStringExtra(Constants.EXPERT_NAME);
    chatId = intent.getStringExtra(Constants.CHAT_ID);
    if (isProgressNeeded)
        progressBar.setVisibility(View.VISIBLE);
    progressText.setVisibility(View.VISIBLE);
    serviceCall(offset);
}
private void serviceCall(int offset) {
    startService(new Intent(ActivityChatView.this, ChatService.class)
            .setAction(Constants.CHAT_HISTORY)
            .putExtra(Constants.USER_ID, expertName)
            .putExtra(Constants.CHAT_ID, chatId)
            .putExtra(Constants.OFFSET, String.valueOf(offset)));
}

protected void onChatHistory(ArrayList<? extends Parcelable> arrayList) {
    ArrayList<History> listHistory = (ArrayList<History>) arrayList;
    chatHistoryList = new ArrayList<History>();
    for (int i = 0; i < listHistory.size(); i++) {
        History historyData = new History();
        historyData.setFromUser(listHistory.get(i).getFromUser());
        historyData.setMsgBody(listHistory.get(i).getMsgBody());
        historyData.setMsgTime(listHistory.get(i).getMsgTime());
        historyData.setRowId(listHistory.get(i).getRowId());
        historyData.setMsgId(listHistory.get(i).getMsgId());
        historyData.setToUser(listHistory.get(i).getToUser());
        chatHistoryList.add(historyData);
    }
    progressBar.setVisibility(View.GONE);
    progressText.setVisibility(View.GONE);
    setValuesInAdapter();
}
@Override
public void onLoadMore() {
    LogMessage.e("number", "number"+offset);
    serviceCall(offset);
}
private void setValuesInAdapter() {
    if (mAdapter == null) {
        mAdapter = new AdapterChatView(this, chatHistoryList);
        chatList.setAdapter(mAdapter);
    }
    mAdapter.setExpertData(chatHistoryList);
    mAdapter.notifyDataSetChanged();
    chatList.onLoadMoreComplete();
    History e = chatHistoryList.get(chatHistoryList.size() - 1);
    rowId= e.getRowId();
    LogMessage.e("rowId", rowId);
    offset = Integer.valueOf(rowId);
    if (chatHistoryList != null && !chatHistoryList.isEmpty()) {
        chatList.setVisibility(View.VISIBLE);
        chatList.getEmptyView().setVisibility(View.GONE);
    } else {
        chatList.getEmptyView().setVisibility(View.VISIBLE);
    }
}

}

  in adapter..
 //setExpertData.
 public void setExpertData(List<History> chatHistoryList) {
    this.mTempData = chatHistoryList;
}
protected void onChatHistory(ArrayList<? extends Parcelable> arrayList) {
    ArrayList<History> listHistory = (ArrayList<History>) arrayList;
    //chatHistoryList = new ArrayList<History>();// (Remove this line from here)
    for (int i = 0; i < listHistory.size(); i++) {
        History historyData = new History();
        historyData.setFromUser(listHistory.get(i).getFromUser());
        historyData.setMsgBody(listHistory.get(i).getMsgBody());
        historyData.setMsgTime(listHistory.get(i).getMsgTime());
        historyData.setRowId(listHistory.get(i).getRowId());
        historyData.setMsgId(listHistory.get(i).getMsgId());
        historyData.setToUser(listHistory.get(i).getToUser());
        chatHistoryList.add(historyData);
    }
    progressBar.setVisibility(View.GONE);
    progressText.setVisibility(View.GONE);
    setValuesInAdapter();
}

並帶進去

private void initChatListView() {

chatHistoryList = new ArrayList<History>();
    intent = getIntent();
    chatList = (LoadMoreListView) findViewById(R.id.list_chat_data);
    progressBar = (ProgressBar) findViewById(R.id.probar);
    progressText = (TextView) findViewById(R.id.loading_text);
    progressBar.setVisibility(View.VISIBLE);
    progressText.setVisibility(View.VISIBLE);
    chatList.setEmptyView(findViewById(android.R.id.empty));
    chatList.setOnLoadMoreListener(this);
    expertName = intent.getStringExtra(Constants.EXPERT_NAME);
    chatId = intent.getStringExtra(Constants.CHAT_ID);
    if (isProgressNeeded)
        progressBar.setVisibility(View.VISIBLE);
    progressText.setVisibility(View.VISIBLE);
    serviceCall(offset);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM