Search code examples
androidandroid-listviewandroid-adapterandroid-scrollview

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


I am using loadmore listview to get items. Initially i am getting 20 items and displayed.But problem is when i scroll bottom it fetches next 20 items but previous 20 item gets cleared. How to solve this issue? Here is code.

 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;
}

Solution

  • 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();
    }
    

    and bring it in

    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);
    }