android 列表开发 ListView

原创 2015年07月09日 23:29:20


1、android 端



二个entity


consultInfo:


private String name;


private String id;



consultInfoRef


private iList<ConsultInfo>  tems 


private String ret;


private String createTime;


private String msg;





一个Adapter



adapter = new Adapter();..



Holder 条目属性


class Holder


{

TextVeiw xx

 }  



3、请求



4、服务端



实例代码:


package com.gc.app.jsk.activity.mine;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gc.app.jsk.base.BaseActivity;
import com.gc.app.jsk.base.MyApplication;
import com.gc.app.jsk.entity.ConsultInfo;
import com.gc.app.jsk.entity.ConsultInfoRef;
import com.gc.app.jsk.entity.EvalEntity;
import com.gc.app.jsk.entity.RequestMessage;
import com.gc.app.jsk.view.CircleImageView;
import com.gc.app.jsk_2.R;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

/**
 * 
 * @Description: 历史咨询列表

 */
public class MyConsultListActivity extends BaseActivity implements
        OnItemClickListener {

    private TextView mTitle;

    private ArrayList<ConsultInfo> list = new ArrayList<ConsultInfo>();

    private PullToRefreshListView consultlist_view;
    
    private final static int CONSULT_LIST = 1;//历史咨询列表
    
    private BaseAdapter adapter = new BaseAdapter() {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ConsultInfo consultInfo = list.get(position);

            ViewHolder holder;

            if (convertView == null) {
                convertView = LayoutInflater.from(MyConsultListActivity.this)
                        .inflate(R.layout.item_consult_list, null);
                holder = new ViewHolder();
                holder.item_circleImageView = (CircleImageView) convertView
                        .findViewById(R.id.doctorHead);
                holder.item_doctorName = (TextView) convertView
                        .findViewById(R.id.doctorName);
                holder.item_deptCat = (TextView) convertView
                        .findViewById(R.id.deptCat);
                holder.item_consultDate = (TextView) convertView
                        .findViewById(R.id.consultDate);
                holder.item_lastMsg = (TextView) convertView.findViewById(R.id.lastMsg);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            /** 此处需要做image Resource 判空处理 */
             if (consultInfo.getPhoto() != null
             && consultInfo.getPhoto().length() > 0) {
                 MyApplication.iLoader.displayImage(consultInfo.getPhoto(),
                 holder.item_circleImageView);
             }
             holder.item_doctorName.setText(consultInfo.getDoctorName());
             holder.item_deptCat.setText(consultInfo.getDeptCatName());
             holder.item_consultDate.setText("2015-07-09");
             holder.item_lastMsg.setText("上海国臣");
            return convertView;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    };

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.btn_back) {
            finish();
        }
    }

    @Override
    protected void initComponent() {
        setContentView(R.layout.activity_consult_list);
        mTitle = (TextView) findViewById(R.id.text_title);
        consultlist_view = (PullToRefreshListView) findViewById(R.id.consultlist_view);
        consultlist_view.setAdapter(adapter);
        mTitle.setText("我的咨询");
    }

    /**
     * 数据初始化
     */
    @Override
    protected void initData() {
        consultList();
    }

    @Override
    protected void registerListener() {
        consultlist_view.setOnItemClickListener(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finishActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    class ViewHolder {
        CircleImageView item_circleImageView;
        TextView item_doctorName;
        TextView item_deptCat;
        TextView item_consultDate;
        TextView item_lastMsg;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        EvalEntity evalEntity = (EvalEntity) parent.getAdapter().getItem(
                position);

        if (evalEntity != null) {
            Intent intent = new Intent(this, EvaluationActivity.class);

            intent.putExtra("evalEntity", evalEntity);

            startActivity(intent);
        }
    }

    @Override
    public boolean handleMessage(Message msg) {

        switch (msg.what) {
        case 0:
            if (msg.arg1 == RET_OK) {
                // String retStr = msg.obj.toString();
                // EvalJson json = gson.fromJson(retStr, EvalJson.class);
                // if (json != null && json.items != null) {
                // list.clear();
                // list.addAll(json.items);
                // }
                // adapter.notifyDataSetChanged();
                // } else {
                // EvalEntityRef evalEntityRef = (EvalEntityRef) gson.fromJson(
                // msg.obj.toString(), EvalEntityRef.class);
                // Log.i("JSK", msg.obj.toString());
                // if (evalEntityRef.data != null) {
                // list.clear();
                // list.addAll(evalEntityRef.data);
                // adapter.notifyDataSetChanged();
                // }
            }
            break;
        case CONSULT_LIST: {
            if (msg.arg1 != RET_OK) {
                ConsultInfoRef consultInfoRef = gson.fromJson(msg.obj.toString(), ConsultInfoRef.class);
                list.clear();
                list.addAll(consultInfoRef.items);
                adapter.notifyDataSetChanged();
            }
            break;
        }

        default:
            break;
        }
        return super.handleMessage(msg);
    }

    
    /**
     * 
     * @Title: consultList
     * @Description: 咨询历史列表
     */
    private void consultList() {
        RequestMessage reqMsg = new RequestMessage("myConsultList");
        reqMsg.put("userID", getUserID());
        request(handler,reqMsg,CONSULT_LIST);
    }

}


服务端:

package com.gc.apps.jsk.online.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;

import com.gc.apps.jsk.common.message.InMessage;
import com.gc.apps.jsk.controller.JSKCommand;
import com.gc.apps.jsk.online.dbobj.Consult;
import com.gc.biz.basedata.dbobj.HospDept;
import com.gc.biz.doctor.dbobj.RESDoctor;
import com.gc.biz.order.dbobj.ConsultBooking;
import com.gc.common.util.StrUtil;
import com.gc.frame.spring.controller.AbstractCommand;

/**
 * 
 * MyConsultListCommand.java
 *
 * @Description: 我的咨询历史列表
 *
 * @version

 */
public class MyConsultListCommand extends AbstractCommand implements JSKCommand {

    // 日志
    private final static Logger log = Logger
            .getLogger(MyConsultListCommand.class);

    @Override
    public ModelAndView run(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        if (log.isDebugEnabled()) {
            log.debug("用户咨询历史列表:MyConsultListCommand");
        }

        InMessage inMessage = getInMessage(request);
        String createTime = System.currentTimeMillis() / 1000 + "";
        String msg = "操作失败!";
        String ret = "-1";

        try {
            // 分页参数 当前页和每页记录数
            String currentPage = StrUtil.notNull(inMessage.get("currentPage"));
            if (currentPage == null || currentPage.length() == 0) {
                currentPage = "1";
            }
            String pageCount = StrUtil.notNull(inMessage.get("pageCount"));
            if (pageCount == null || pageCount.length() == 0) {
                pageCount = "10";
            }

            // 每页记录数
            int npageCount = Integer.parseInt(pageCount);
            // 偏移
            int offset = npageCount * (Integer.parseInt(currentPage) - 1);
            // 获取用户ID
            String userID = StrUtil.notNull(inMessage.get("userID"));
            ConsultBooking cb = new ConsultBooking();
            cb.setField("UserID", userID);
            List<ConsultBooking> listcb = new ArrayList<ConsultBooking>();
            List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
            listcb = cb.searchAndRetrieveList();
            for (Iterator<ConsultBooking> it = listcb.iterator(); it.hasNext();) {
                cb = it.next();
                Map<String, Object> map = new HashMap<String, Object>();

                // 与医生相关的信息
                String doctorID = cb.getField("DoctorID");
                RESDoctor resDoctor = new RESDoctor();
                resDoctor.setField("DoctorID", doctorID);
                if (resDoctor.find()) {
                    map.put("doctorName", resDoctor.getField("DoctorName"));
                    map.put("photo", resDoctor.getField("Photo"));
                    String deptCatID = resDoctor.getField("DeptCatID");
                    HospDept hd = new HospDept();
                    hd.setField("DeptCatID", deptCatID);
                    if (hd.find()) {
                        map.put("deptCatName", hd.getField("DeptName"));
                    }
                }
                map.put("orderDate", cb.getField("OrderDate"));
                listmap.add(map);
            }

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("ret", "1");
            map.put("msg", "操作成功!");
            map.put("createTime", createTime);
            map.put("items", listmap);
            sendJsonResult(response, JSONObject.fromObject(map).toString());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("用户咨询记录列表出错:" + e);
        }
        return null;
    }

    @Override
    public InMessage getInMessage(HttpServletRequest request) {
        return (InMessage) request.getAttribute(JSKCommand.InMessage_KEY);
    }

    @Override
    public String getMessageKey() {
        return getName();
    }
}



相关文章推荐

Android开发:ListView 长按删除列表项两种方式

在今天的学习当中,怎么响应长按事件去删除一个列表项困扰了我

android开发 列表显示(ListView)

今天编写了一个 列表显示(ListView),给大家分享一下 文章来源:好岸园it技术网 http://www.hopean.com string.xml   0_Li...

Android开发入门之采用ListView实现数据列表显示

再次用到上一篇写过的db工程, activity_main.xml:

Android 应用开发笔记 - 列表(ListView)& SimpleAdapter

1. 新建一Layout XML文件,如下图: 在面板拖拽,更改相关属性, res/list_view.xml清单如下: ...

Android开发教程13:UI之列表:ListView

转自: http://liuzhichao.com/p/365.html ListView是Android开发中比较常用的一个组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。...
  • meryvn
  • meryvn
  • 2015年09月22日 15:03
  • 623

Android开发——listview中嵌套checkBox复选框实现单选、全选删除列表内容

Android开发——listview中嵌套checkBox复选框实现单选、全选删除列表内容 listView中嵌套复选框进行条目的选中删除操作,我们经常会用到,比如:购物车清单中的商品添加删除,...
  • jxnk25
  • jxnk25
  • 2015年12月19日 14:20
  • 2645

Android开发总结笔记 ListView(列表视图[中]) 1-3-2

打造万能适配器(来自Android-打造万能适配器) 在使用ListView的时候,每次都要写一个适配器,而且适配的写法都是差不多的。如果一个项目有20个项目,我们就要重复写20个适配器,因此。对适配...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:android 列表开发 ListView
举报原因:
原因补充:

(最多只允许输入30个字)