hdf336 2011-08-04 15:36
浏览 230
已采纳

大家帮我看下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条回答

  • laopeng301 2011-08-04 19:06
    关注

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码