【问题标题】:LoadMore in listview clears previous item when scrolled at bottom android?在底部android滚动时,listview中的LoadMore会清除上一个项目吗?
【发布时间】:2016-01-13 06:09:15
【问题描述】:

我正在使用 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;
}

【问题讨论】:

    标签: android android-listview android-adapter android-scrollview


    【解决方案1】:
    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);
    }
    

    【讨论】:

    • 如果回答对你有帮助,也请标记为正确
    • 非常感谢您纠正错误:) 是的,我会接受您的回答
    猜你喜欢
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多