多条目加载

本文介绍了如何在Android应用中实现在MainActivity中加载更多条目,通过GetJieXi方法获取网络数据并停止刷新列表。数据解析采用自定义的Mydata工具类,将InputStream转换为字符串。同时,提到了ImageLoader工具类用于处理图片加载,并展示了自定义适配器MyBaseAdapter的基础结构。

//主页面
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {

private XListView xListView;
private int num=0;
private boolean into=true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    xListView = (XListView) findViewById(R.id.xl);
    xListView.setPullLoadEnable(true);
    xListView.setXListViewListener(this);
    GetJieXi("http://apis.juhe.cn/cook/query?menu="
            + URLEncoder.encode("水煮鱼")+"&key="
            +URLEncoder.encode("34689251284051bcf818c8fdd7f37303")+"&pn="+num);


}

public void GetJieXi(String path){
    new AsyncTask<String,Void,String>(){

        private MyBase myBase;

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s !=null){

              Gson gson=  new Gson();
                Bean bean = gson.fromJson(s, Bean.class);
                if (myBase==null) {
                    myBase = new MyBase(MainActivity.this, bean.getResult().getData());
                    xListView.setAdapter(myBase);
                }else {
                    myBase.imageL(bean.getResult().getData(),into);
                    myBase.notifyDataSetChanged();
                }
            }
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                String path= params[0];
                URL url=new URL(path);
                HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                connection.setReadTimeout(5000);
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
              int code=  connection.getResponseCode();
                if (code==200){
                   InputStream si= connection.getInputStream();
                   String json= Mydata.GetData(si);

                    return  json;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }.execute(path);
}

@Override
public void onRefresh() {

++num;
GetJieXi(“http://apis.juhe.cn/cook/query?menu=
+ URLEncoder.encode(“水煮鱼”)+”&key=”
+URLEncoder.encode(“34689251284051bcf818c8fdd7f37303”)+”&pn=”+num);
xListView.stopRefresh(true);

}

@Override
public void onLoadMore() {
    ++num;

    GetJieXi("http://apis.juhe.cn/cook/query?menu="
            + URLEncoder.encode("水煮鱼")+"&key="
            +URLEncoder.encode("34689251284051bcf818c8fdd7f37303")+"&pn="+num);
    xListView.stopLoadMore();

}

}
//解析工具类
public class Mydata {
public static String GetData(InputStream si){
try {
ByteArrayOutputStream stream= new ByteArrayOutputStream();
byte[] by=new byte[1024];
int len=0;
while ((len=si.read(by)) !=-1){
stream.write(by,0,len);

        }
        return  stream
                .toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return  null;
}

}
//ImageLoder工具类
public class ImageLoder extends Application{
private static final String TAG = “MyApplication”;

@Override
public void onCreate() {
    super.onCreate();



    try {
        //缓存路径
        File cacheDir = StorageUtils.getOwnCacheDirectory(this, Environment.getExternalStorageDirectory().getPath());

        Log.d(TAG, "onCreate: "+Environment.getExternalStorageDirectory().getPath());
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
                .threadPoolSize(3)//配置线程数量
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))//内存缓存图片 2M
                .diskCache(new UnlimitedDiskCache(cacheDir))//配饰sdcard缓存路径
                .diskCacheSize(50 * 1024 * 1024)//sdcard上缓存50M的图片
                .diskCacheFileCount(100)//缓存文件的数量   100个
               // .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .build();

        //配置缓存选项
        ImageLoader.getInstance().init(configuration);
    } catch (Exception e) {
        e.printStackTrace();
    }



}

}
/适配器
public class MyBase extends BaseAdapter{
private Context context;
private List

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值