做了一个listview的下拉刷新和底部自动加载,但我做完之后项目能运行,但就是始终加载不出来,求教

底下是代码

 package caifu.life.ZHSQ.main;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import caifu.life.ZHSQ.R;
import caifu.life.ZHSQ.sqShouYeListView.AutoListView;
import caifu.life.ZHSQ.sqShouYeListView.AutoListView.OnLoadListener;
import caifu.life.ZHSQ.sqShouYeListView.AutoListView.OnRefreshListener;

public class ShouYeActivity extends Activity implements OnRefreshListener,
        OnLoadListener {
    // 获取listview
    private AutoListView syListView;
    private listViewAdapter adapter;
    private ArrayList<HashMap<String, Object>> item;

    // 传递消息处理刷新数据
    private Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            @SuppressWarnings("unchecked")
            ArrayList<HashMap<String, Object>> result = (ArrayList<HashMap<String, Object>>) msg.obj;
            switch (msg.what) {
            case AutoListView.REFRESH:// 刷新操作
                syListView.onRefreshComplete();
                item.clear();
                item.addAll(result);
                break;
            case AutoListView.LOAD:// 加载
                syListView.onLoadComplete();
                item.addAll(result);
                break;
            }
            syListView.setResultSize(result.size());
            adapter.notifyDataSetChanged();
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sq_message);
        // 获取listview
        syListView = (AutoListView) findViewById(R.id.shouyeListView);
        // 配置适配器
        adapter = new listViewAdapter(this, item);
        // 设置适配
        syListView.setAdapter(adapter);
        // 追加刷新与加载监听
        syListView.setOnRefreshListener(this);
        syListView.setOnLoadListener(this);
        initData();
        // 一个动态数组
        getDate();
    }

    // 定义一个得到数据的方法
    private static ArrayList<HashMap<String, Object>> getDate() {
        ArrayList<HashMap<String, Object>> item = new ArrayList<HashMap<String, Object>>();
        for (int i = 1; i < 4; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("touXiang", R.drawable.sylvtouxiang);
            map.put("yongHuMing", "第" + i + "用户");
            map.put("neiRong", "第" + i + "内容");
            map.put("sylv_image1", R.drawable.sylvimage1);
            map.put("sylv_image2", R.drawable.sylvimage2);
            map.put("sylv_image3", R.drawable.sylvimage3);
            item.add(map);
        }
        return item;
    }

    private void initData() {
        loadData(AutoListView.REFRESH);
    }

    private void loadData(final int what) {
        // 起线程获取数据
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Message msg = handler.obtainMessage();
                    msg.what = what;
                    msg.obj = getDate();
                    handler.sendMessage(msg);
                }
            }
        }).start();
    }

    public void onLoad() {
        loadData(AutoListView.LOAD);
    }

    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }

    // 适配器
    private class listViewAdapter extends BaseAdapter {
        private ViewHolder holder;
        private Context context;
        private ArrayList<HashMap<String, Object>> item;

        public listViewAdapter(Context context,
                ArrayList<HashMap<String, Object>> item) {
            this.item = item;
            this.context = context;
        }

        public int getCount() {
            return getDate().size();
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.activity_sq_message_item, null);
                // 得到各控件对象
                holder.touXiang = (ImageView) convertView
                        .findViewById(R.id.touXiang);
                holder.yongHuMing = (TextView) convertView
                        .findViewById(R.id.yongHuMing);
                holder.neiRong = (TextView) convertView
                        .findViewById(R.id.neiRong);
                holder.sylv_image1 = (ImageView) convertView
                        .findViewById(R.id.sylv_image1);
                holder.sylv_image2 = (ImageView) convertView
                        .findViewById(R.id.sylv_image2);
                holder.sylv_image3 = (ImageView) convertView
                        .findViewById(R.id.sylv_image3);
                // 绑定viewHolder对象
                convertView.setTag(holder);
            } else {
                // 取出ViewHolder对象
                holder = (ViewHolder) convertView.getTag();
            }
            holder.yongHuMing.setText(getDate().get(position).get("yongHuMing")
                    .toString());
            holder.neiRong.setText(getDate().get(position).get("neiRong")
                    .toString());
            return convertView;
        }

        public final class ViewHolder {
            public ImageView touXiang;
            public TextView yongHuMing;
            public TextView neiRong;
            public ImageView sylv_image1;
            public ImageView sylv_image2;
            public ImageView sylv_image3;
        }

    }
    /** 测试数据
        public List<String> getData() {
            List<String> result = new ArrayList<String>();
            Random random = new Random();
            for (int i = 0; i < 10; i++) {
                long l = random.nextInt(10000);
                result.add("当前条目的ID:" + l);
            }
            return result;
        }*/
}

2个回答

看看是否有数据,再看看布局是否有问题。

推荐你一个人的博客,或许对你这个问题有帮助,http://blog.csdn.net/zxt0601

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