重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android基于BaseExpandableListAdapter实现的二级列表仿通话记录功能详解

本文实例讲述了Android基于BaseExpandableListAdapter实现的二级列表仿通话记录功能。分享给大家供大家参考,具体如下:

创新互联服务项目包括赞皇网站建设、赞皇网站制作、赞皇网页制作以及赞皇网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,赞皇网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到赞皇省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

android SDK中带有这样类似的例子,但是那个还是静态数据,没有实际应用价值,参考意义不大。

网上找了很多,还是那样的情况,几乎是同一篇文章,大家转来转去。况且,那篇例子也是静态的数据。

还是自己试试,自己写一个吧。程序读取手机系统的通话记录,按联系人分组,显示到列表。

开发工具:eclipse
运行环境:htc G9 android2.3.3

不多说,先看效果:

Android基于BaseExpandableListAdapter实现的二级列表仿通话记录功能详解

展开后的效果:

Android基于BaseExpandableListAdapter实现的二级列表仿通话记录功能详解

继续展开的效果:

Android基于BaseExpandableListAdapter实现的二级列表仿通话记录功能详解

main.xml:

<?xml version="1.0" encoding="utf-8"?>

 


lis_item.xml:

<?xml version="1.0" encoding="utf-8"?>










这里的也是采用继承BaseExpandableListAdapter来实现,具体请看代码。

主角Activity:

public class CollLogActivity extends Activity {
  private ExpandableListView listView;
  private MyAsyncQueryHandler queryHandler;
  private MyExpandableListAdapter listAdapter;
  private ArrayList group;
  private ArrayList> child;
  private static final Uri uri = Uri.parse("content://call_log/calls");
  private static final String[] projection = { "_id", "number", "date",
      "type", "new", "name" };
  private static final int INCOMING_TYPE = 1;
  private static final int OUTGOING_TYPE = 2;
  private static final int MISSED_TYPE = 3;
  private String currentNumber = "-1";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    listView = (ExpandableListView) findViewById(R.id.list);
    queryHandler = new MyAsyncQueryHandler(getContentResolver());
    group = new ArrayList();
    child = new ArrayList>();
  }
  @Override
  protected void onResume() {
    super.onResume();
    startQuery();
  }
  private class MyExpandableListAdapter extends BaseExpandableListAdapter {
    private LayoutInflater inflater;
    public MyExpandableListAdapter(Context context) {
      this.inflater = LayoutInflater.from(context);
    }
    @Override
    public Object getChild(int groupPosition, int childPosition) {
      return child.get(groupPosition).get(childPosition);
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
      return childPosition;
    }
    @Override
    public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
      ViewHolder holder;
      if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.number = (TextView) convertView
            .findViewById(R.id.number);
        holder.date = (TextView) convertView.findViewById(R.id.date);
        holder.count = (TextView) convertView.findViewById(R.id.count);
        holder.type = (ImageView) convertView.findViewById(R.id.type);
        holder.open = (ImageView) convertView.findViewById(R.id.open);
        convertView.setTag(holder);
      } else {
        holder = (ViewHolder) convertView.getTag();
      }
      convertView.setBackgroundColor(Color.rgb(54, 54, 54));
      ContentValues cv = child.get(groupPosition).get(childPosition);
      String name = cv.getAsString("name");
      String number = cv.getAsString("number");
      String date = cv.getAsString("date");
      int type = cv.getAsInteger("type");
      holder.name.setText(name);
      holder.number.setText(number);
      holder.date.setText(date);
      holder.count.setVisibility(View.GONE);
      setTypeImg(holder.type, type);
      holder.open.setVisibility(View.GONE);
      return convertView;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
      return child.get(groupPosition).size();
    }
    @Override
    public Object getGroup(int groupPosition) {
      return group.get(groupPosition);
    }
    @Override
    public int getGroupCount() {
      return group.size();
    }
    @Override
    public long getGroupId(int groupPosition) {
      return groupPosition;
    }
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
      ViewHolder holder;
      if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.number = (TextView) convertView
            .findViewById(R.id.number);
        holder.date = (TextView) convertView.findViewById(R.id.date);
        holder.count = (TextView) convertView.findViewById(R.id.count);
        holder.type = (ImageView) convertView.findViewById(R.id.type);
        holder.open = (ImageView) convertView.findViewById(R.id.open);
        convertView.setTag(holder);
      } else {
        holder = (ViewHolder) convertView.getTag();
      }
      ContentValues cv = (ContentValues) getGroup(groupPosition);
      String name = cv.getAsString("name");
      String number = cv.getAsString("number");
      String date = cv.getAsString("date");
      holder.name.setText(name);
      holder.number.setText(number);
      holder.date.setText(date);
      holder.type.setVisibility(View.GONE);
      holder.count.setText("(" + getChildrenCount(groupPosition) + ")");
      if (isExpanded) {
        holder.open.setImageResource(R.drawable.min);
      } else {
        holder.open.setImageResource(R.drawable.max);
      }
      return convertView;
    }
    @Override
    public boolean hasStableIds() {
      return false;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
    }
  }
  private class ViewHolder {
    TextView name;
    TextView number;
    TextView date;
    TextView count;
    ImageView type;
    ImageView open;
  }
  private void setTypeImg(ImageView imageView, int type) {
    switch (type) {
    case INCOMING_TYPE:
      imageView.setImageResource(R.drawable.call_in);
      break;
    case OUTGOING_TYPE:
      imageView.setImageResource(R.drawable.call_out);
      break;
    case MISSED_TYPE:
      imageView.setImageResource(R.drawable.call_miss);
      break;
    }
  }
  private class MyAsyncQueryHandler extends AsyncQueryHandler {
    public MyAsyncQueryHandler(ContentResolver cr) {
      super(cr);
    }
    @Override
    protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
      if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        for (int i = 0; i < cursor.getCount(); i++) {
          cursor.moveToPosition(i);
          String name = cursor.getString(cursor
              .getColumnIndex("name"));
          String number = cursor.getString(cursor
              .getColumnIndex("number"));
          int type = cursor.getInt(cursor.getColumnIndex("type"));
          long date = cursor.getLong(cursor.getColumnIndex("date"));
          ContentValues cv = new ContentValues();
          cv.put("name", name);
          cv.put("number", number);
          cv.put("type", type);
          cv.put("date",
              formatTimeStampString(CollLogActivity.this, date));
          addGroupItem(cv);
        }
      }
      if (group.size() > 0) {
        setAdapter();
      }
    }
  }
  private void addGroupItem(ContentValues cv) {
    String number = cv.getAsString("number");
    if (!currentNumber.equals(number)) {
      group.add(cv);
      addChildItem(number);
      currentNumber = number;
    }
  }
  private void addChildItem(String number) {
    ArrayList list = new ArrayList();
    Cursor cursor = getContentResolver().query(uri, projection,
        "number=" + number, null, null);
    if (cursor != null && cursor.getCount() > 0) {
      cursor.moveToFirst();
      for (int i = 0; i < cursor.getCount(); i++) {
        cursor.moveToPosition(i);
        String name = cursor.getString(cursor.getColumnIndex("name"));
        int type = cursor.getInt(cursor.getColumnIndex("type"));
        long date = cursor.getLong(cursor.getColumnIndex("date"));
        ContentValues cv = new ContentValues();
        cv.put("name", name);
        cv.put("number", number);
        cv.put("type", type);
        cv.put("date",
            formatTimeStampString(CollLogActivity.this, date));
        list.add(cv);
      }
    }
    child.add(list);
  }
  private void setAdapter() {
    listAdapter = new MyExpandableListAdapter(CollLogActivity.this);
    listView.setAdapter(listAdapter);
  }
  private void startQuery() {
    queryHandler.startQuery(1, null, uri, projection, null, null,
        "date desc");
  }
  // 处理日期方法
  private static String formatTimeStampString(Context context, long when) {
    Time then = new Time();
    then.set(when);
    Time now = new Time();
    now.setToNow();
    int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT
        | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_CAP_AMPM;
    if (then.year != now.year) {
      format_flags |= DateUtils.FORMAT_SHOW_YEAR
          | DateUtils.FORMAT_SHOW_DATE;
    } else if (then.yearDay != now.yearDay) {
      format_flags |= DateUtils.FORMAT_SHOW_DATE;
    } else {
      format_flags |= DateUtils.FORMAT_SHOW_TIME;
    }
    return DateUtils.formatDateTime(context, when, format_flags);
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android布局layout技巧总结》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android操作json格式数据技巧总结》、《Android资源操作技巧汇总》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。


网站栏目:Android基于BaseExpandableListAdapter实现的二级列表仿通话记录功能详解
文章分享:http://cqcxhl.com/article/gcgsge.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP