大家帮我看下listview 异步加载数据 分页的问题!!!

网上有大量介绍listview分页的文章,但都是一些基础,下面我用的是 异步从服务端获取数据,然后绑定到listview里面,服务端程序也是一个大家常见的分页的程序,现在出现一些问题,滑动滚动条加载下一页内容时上一页的内容没有了,大家能不能帮着看看,先谢了!

public class SearchList extends CommonActivity implements OnScrollListener,
OnClickListener ,OnItemClickListener{

private String wherestr="",orderstr=" autoid desc";
private int lastItem = 0;
ImageAndTextListAdapter adapter;
LinearLayout loadingLayout;
/**
 * 设置布局显示属性
 */
private LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
/**
 * 设置布局显示目标最大化属性
 */
private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.FILL_PARENT,
        LinearLayout.LayoutParams.FILL_PARENT);
private ProgressBar progressBar;
private ListView list;
// 获得数据集合
private List<ImageAndText> dataArray = new ArrayList<ImageAndText>();
private int mCurrentPage = Constants.PAGE_FIRST;
private int mTotalPage, mTotalCount;
// 组件声明
private EditText searchTxt = null;
private TextView recent_view_topnotifybar = null;
private ImageView locate_my=null;//搜索确定
private ImageView record_cordition=null; //搜索详细
private ImageView go_filter2=null;
private String Flag="";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.searchlist);

    searchTxt = (EditText) findViewById(R.id.search_edittext);
    recent_view_topnotifybar = (TextView) findViewById(R.id.recent_view_topnotifybar);
    locate_my=(ImageView)findViewById(R.id.locate_my);
    record_cordition=(ImageView)findViewById(R.id.record_cordition);
    go_filter2=(ImageView)findViewById(R.id.go_filter2);

    searchTxt.setOnClickListener(this);
    locate_my.setOnClickListener(this);
    record_cordition.setOnClickListener(this);
    go_filter2.setOnClickListener(this);

    // 线性布局
    LinearLayout layout = new LinearLayout(this);
    // 设置布局 水平方向
    layout.setOrientation(LinearLayout.HORIZONTAL);
    // 进度条
    progressBar = new ProgressBar(this);
    // 进度条显示位置
    progressBar.setPadding(0, 0, 15, 0);
    // 把进度条加入到layout中
    layout.addView(progressBar, mLayoutParams);
    // 文本内容
    TextView textView = new TextView(this);
    textView.setText("加载中...");
    textView.setGravity(Gravity.CENTER_VERTICAL);
    // 把文本加入到layout中
    layout.addView(textView, FFlayoutParams);
    // 设置layout的重力方向,即对齐方式是
    layout.setGravity(Gravity.CENTER);

    // 设置ListView的页脚layout
    loadingLayout = new LinearLayout(this);
    loadingLayout.addView(layout, mLayoutParams);
    loadingLayout.setGravity(Gravity.CENTER);
    // =============================================
    list = (ListView) findViewById(R.id.list);
    list.setOnItemClickListener(this);

}
//
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();


    // load
    new QueryTask().execute();

}

// 单击事件
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent=null;
    switch (v.getId()) {
    case R.id.search_edittext://点击搜索文本框内容清空文本框
        searchTxt.setText("");
        break;
    case R.id.locate_my:// 搜索确定
        String str=searchTxt.getText().toString();
        if(str.equals("")||str.equals("车辆品牌"))
            Toast.makeText(this, "请输入查询关键词!", Toast.LENGTH_LONG).show();
        else
        {   
            this.wherestr=str;//查询条件
            dataArray.clear();
            new QueryTask().execute();
        }
        break;
    case R.id.record_cordition:// 进入搜索的详细页
        intent = new Intent(SearchList.this,
                ZoneMain.class);

        startActivity(intent);
        break;
    case R.id.go_filter2:// 点击筛选进入筛选页,但要返回筛选条件
        intent = new Intent(SearchList.this,
                FilterSearch.class);

        startActivity(intent);
        break;

    }
}

//
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    //Log.e("onscroll adapter.coun", adapter.count + "");
    // TODO Auto-generated method stub
    lastItem = firstVisibleItem + visibleItemCount - 1;
            //如果当前页小于总页数 就需要加载
    if (mCurrentPage <= mTotalPage) {
        if (firstVisibleItem + visibleItemCount == totalItemCount) {
            //adapter.count += 5;
            mCurrentPage++;
            new QueryTask().execute();

            adapter.notifyDataSetChanged();
            list.setSelection(lastItem);

            // mCurrentPage=2;

// mCurrentPage = adapter.count / 5;// 当前页?
// Toast.makeText(getApplicationContext(),
// "第" + mCurrentPage + "页", Toast.LENGTH_LONG).show();
}
} else {
list.removeFooterView(loadingLayout);
}

}

//
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub

}

// 加载数据
class QueryTask extends CommonTask<Void> {

    private CarSearchListReq getListReq = null;
    // 实例化此方法后就已经开始加载服务端的数据了
    private CarSearchListResp getListResp = new CarSearchListResp();

    public QueryTask() {
        super(SearchList.this, getString(R.string.get_data));
        // TODO Auto-generated constructor stub
    }

    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        this.getListReq = new CarSearchListReq();
        this.getListReq.wherestr = SearchList.this.wherestr;
        this.getListReq.orderstr = SearchList.this.orderstr;
        // 这个必须存在 mCurrentPage赋值改变记录查询
        this.getListReq.pageIndex = mCurrentPage;

        InputStream is = null;
        HttpConnectWrapper httpCon = new HttpConnectWrapper();
        try {
            is = httpCon.httpPost(getListReq.getData(), getListReq
                    .getNeedRespHeaderProps());

            getListResp.parse(is);
        } catch (Exception e) {

        }
        return super.doInBackground(params);
    }

    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        int hRet = this.getListResp.getData();

        if (hRet == 1 || hRet == 1004) {
            dataArray = this.getListResp.InfoList;
            // 总记录数
            mTotalCount = this.getListResp.count;
            // 总页数
            mTotalPage = mTotalCount % PAGE_MAX_LINE == 0 ? mTotalCount
                    / PAGE_MAX_LINE : mTotalCount / PAGE_MAX_LINE + 1;

            setPage();

            if (hRet == 1004) {

            }

        } else {

        }

    }
}

/**
 * 设置ListView
 */
private void setPage() {
    adapter = new ImageAndTextListAdapter(this, dataArray, list);
    // 添加到脚页显示
    list.addFooterView(loadingLayout);

    list.setAdapter(adapter);
    // 给ListView注册滚动监听
    list.setOnScrollListener(this);
}

//单击列表促发事件
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

    if (arg2 != -1) {
        Intent intent=new Intent(SearchList.this,CarDetail.class);
        intent.putExtra("AutoID", dataArray.get(arg2).AutoId.toString());
        startActivity(intent);
    }
}

4个回答

把[code="java"]ImageAndTextListAdapter [/code]这个类中的
imageAndTexts 设置get/set方法

[code="java"]
将onPostExecute方法改成这样
dataArray.addAll(this.getListResp.InfoList);
adapter.setImageAndTexts(dataArray);
adapter.notifyDataSetChanged();
[/code]

ImageAndTextListAdapter 贴这个Adapter代码看看

为什么你确定问题不在ImageAndTextListAdapter ?
adapter = new ImageAndTextListAdapter(this, dataArray, list);
你listview的数据源来自dataArray
,而你dataArray每次翻页时加载的是当前页的数据
而并不是已显示的数据+当前页 的数据
不知道我理解是否正确?

:lol: 如果不修改ImageAndTextListAdapter 结构
按照你这个情况把这赋值数据源的代码
[code="java"]dataArray = this.getListResp.InfoList; [/code]
改成[code="java"]dataArray.add(this.getListResp.InfoList);[/code]
你直接赋值那么每次翻页赋值一遍
而改成添加,后是在原来的数据上添加新加载的数据
试试吧

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐