现在的位置: 首页 > 综合 > 正文

listView配合适配器动态的加载服务器中返回的数据

2013年07月24日 ⁄ 综合 ⁄ 共 4197字 ⁄ 字号 评论关闭

目录:

一.从服务器加载数据并跟listView进行结合使用

1.加载基本的控件,并设置加载时候的滚动条

2.通过线程获取需要显示的数据
3.把数据和listView结合起来
4.对适配器中的设置

二.配置适配器中的步骤
1.加载listView布局
2.设置listView的适配器
3.适配器的用
a.)加载里面的布局文件
b.)加载布局中的控件

c.)设置控件中的值(这是用到了vo,从获得的list值设置到布局当中)

一 .从服务器加载数据并对listView进行结合的前期配置

// 1.启动activity

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_type);
context = TypeActivity.this;

                //注册基本的空间
registView();

dialog = new ProgressDialog(context);
dialog.setMessage("正在加载数据...");
dialog.setCancelable(false);
dialog.show();
Thread thread = new Thread(new loadTypeRunnable());
thread.start();
}

  //2.注册基本组件
private void registView(){
titleText = (TextView)findViewById(R.id.titletext);

                 //加载listView视图
typeListView = (ListView)(findViewById(R.id.pt_type_view));
}

      //3.从服务端中获取分类数据

private class loadTypeRunnable implements Runnable{
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
productTypeService = new ProductTypeImpl(context);
pTypeList = productTypeService.queryTypeList();

     //对服务器段返回的结果进行处理
Bundle bundle = new Bundle();
if(pTypeList != null && pTypeList.size() >0){
bundle.putString("result", "success");
Message message = new Message();
message.setData(bundle);
sendTypeList.sendMessage(message);
}else {
bundle.putString("result", "fail");
}
}
};

       //4.对返回的数据进行处理
private Handler sendTypeList = new Handler(){
@Override
public void handleMessage(Message msg) {
String msStr = msg.getData().getString("result");
if("success".equals(msStr)){
//加载数据赋值
initTypeListView();

               //数据加载完成后结束进度条
dialog.dismiss();
}else {
}
}
};

//5.listView绑定适配器适配器
private void initTypeListView() {
titleText.setText("分类导航");
typeAdapter = new TypeAdapter(context, pTypeList);
typeListView.setAdapter(typeAdapter);
}

      //6.适配器跟数据的设置
private class TypeAdapter extends BaseAdapter{
private Context context;
private List<ProductType> productTypeList = new ArrayList<ProductType>();
private LayoutInflater inflater;//动态的加载布局文件

public TypeAdapter(Context context,  List<ProductType> productTypeList){
this.context = context;
this.productTypeList = productTypeList;
this.inflater = LayoutInflater.from(context);
}

    //获得listView的数据数量
@Override
public int getCount() {
return productTypeList.size();
}

//得到每一个item的位置
@Override
public Object getItem(int position) {
if(productTypeList != null && productTypeList.size() > 0){
return productTypeList.get(position);
} else {
return null;
}
}

//得到每一个item对象
@Override
public long getItemId(int position) {
if(productTypeList != null && productTypeList.size() > 0){
return position;
} else {
return 0;
}
}
//数据和视图进行结合
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
//临时的vo用来传递数据的对象
viewHolder = new ViewHolder();
//加载item的布局
convertView = inflater.inflate(R.layout.type_items, null);
//加载布局中的控件
viewHolder.typeImg = (ImageView) convertView.findViewById(R.id.pt_img_01);
viewHolder.first_type_content = (TextView) convertView.findViewById(R.id.pt_item_01);
viewHolder.second_type_content = (TextView) convertView.findViewById(R.id.pt_item_02);
//用来记录浏览记录当再次加载的时候直接从这里获取
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//获得数据
ProductType productType = new ProductType();
productType = productTypeList.get(position);
viewHolder.first_type_content.setText(productType.getPtName());

//动态的加载图片
String url = ActivityUtil.changeImageUrl(context, TypeActivity.class, productType.getPtsmall());
String tag = url;
if(!tag.equals(String.valueOf(viewHolder.typeImg.getTag()))){
//当图片没有从服务器总加载出来的时候加载一个默认的一张图片
viewHolder.typeImg.setImageResource(R.drawable.img_cache);
//添加图片到队列当中
CommonConstant.downloadImage.addTask(url, viewHolder.typeImg);
//加载图片内容
CommonConstant.downloadImage.doTask(TypeActivity.class.getName());
}

//加载子分类
StringBuffer childTypeName =  new StringBuffer();
if(productType.getChildType() != null && productType.getChildType().size()>0){
boolean flag = false;
for (ProductType sonProductType : productType.getChildType()) {
if(flag){
childTypeName.append("/");
}else
flag = true;
childTypeName.append(sonProductType.getPtName());
}
}
String childStr = String.valueOf(childTypeName);
if(childStr.length() >= 16){

//对显示的内容太长进行处理
childStr = childStr.substring(0, 15)+"...";
}
//设置子类中的
viewHolder.second_type_content.setText(childStr);
return convertView;
}

//学过web的都知道这个相当与一个临时的vo
class ViewHolder{
private ImageView typeImg;//声明分类的图片
private TextView first_type_content,second_type_content;
}
}

抱歉!评论已关闭.