码迷,mamicode.com
首页 > 编程语言 > 详细

网络获取数组信息,根据姓名按首字母排序 添加侧滑删除。

时间:2016-06-04 16:20:18      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

package jianlemeistaff.app.jlm.com.jianlemeistaff.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.chauthai.swipereveallayout.SwipeRevealLayout;
import com.zhy.autolayout.AutoFrameLayout;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import jianlemeistaff.app.jlm.com.jianlemeistaff.R;
import jianlemeistaff.app.jlm.com.jianlemeistaff.bean.Staff;
import jianlemeistaff.app.jlm.com.jianlemeistaff.control.StaffInfoUtil;
import jianlemeistaff.app.jlm.com.jianlemeistaff.utils.BasicNameValuePair;
import jianlemeistaff.app.jlm.com.jianlemeistaff.utils.NameValuePair;
import jianlemeistaff.app.jlm.com.jianlemeistaff.widget.LetterBaseListAdapter;
import jianlemeistaff.app.jlm.com.jianlemeistaff.widget.LetterListView;

/**
 * Created by lixu on 2016/6/2.
 */

public class BossMineContactsActivity extends FragmentActivity implements View.OnClickListener {
    private Activity context;
    private LetterListView mlLetterListView;
    private ImageView back, add;
    private TestAdapter mTestAdapter;
    private RadioButton yixian, houqin, guanlizhe;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.bossmine_contacts);
        context = this;
        setTitleView();
        initview();
        mTestAdapter = new TestAdapter();

        StaffInfoUtil.getStaffListByBossId(context, new Response.Listener<String>() {

            @Override
            public void onResponse(String s) {
                setDataOrder(JSON.parseArray(s, Staff.class));

            }

        });


        mlLetterListView.setAdapter(mTestAdapter);

        yixian.setTextColor(Color.WHITE);
        houqin.setTextColor(Color.BLACK);
        guanlizhe.setTextColor(Color.BLACK);


    }


    private void setTitleView() {
        TextView title = (TextView) findViewById(R.id.title_name);
        title.setText("员工列表");
        findViewById(R.id.title).setBackgroundColor(Color.parseColor("#548269"));


    }

    private void initview() {
        mlLetterListView = (LetterListView) findViewById(R.id.letterListView);
        back = (ImageView) findViewById(R.id.left);
        add = (ImageView) findViewById(R.id.right);

        yixian = (RadioButton) findViewById(R.id.bossmine_yixian_btn);
        houqin = (RadioButton) findViewById(R.id.bossmine_houqin_btn);
        guanlizhe = (RadioButton) findViewById(R.id.bossmine_guanlizhe_btn);

        yixian.setOnClickListener(this);
        houqin.setOnClickListener(this);
        guanlizhe.setOnClickListener(this);


        add.setBackgroundResource(R.drawable.boss_mine_add);

        add.setOnClickListener(this);
        back.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left:
                finish();
                break;
            case R.id.right:
                startActivityForResult(new Intent(context, AddStaffActivity.class), 834);
                break;

            case R.id.bossmine_yixian_btn:
                yixian.setTextColor(Color.WHITE);
                houqin.setTextColor(Color.BLACK);
                guanlizhe.setTextColor(Color.BLACK);
                break;
            case R.id.bossmine_houqin_btn:
                yixian.setTextColor(Color.BLACK);
                houqin.setTextColor(Color.WHITE);
                guanlizhe.setTextColor(Color.BLACK);
                break;
            case R.id.bossmine_guanlizhe_btn:
                yixian.setTextColor(Color.BLACK);
                houqin.setTextColor(Color.BLACK);
                guanlizhe.setTextColor(Color.WHITE);
                break;


        }
    }

    /**
     * 这里 使用一个简单的 NameValuePair 对象,做为测试
     */
    class TestAdapter extends LetterBaseListAdapter<NameValuePair> {
        private LayoutInflater inflater;
        /**
         * 字母对应的key,因为字母是要插入到列表中的,为了区别,所有字母的item都使用同一的key.
         **/
        private static final String LETTER_KEY = "letter";

        public TestAdapter() {
            super();
            inflater = LayoutInflater.from(getApplicationContext());

        }

        public void setlist(List<Staff> list) {

            List<NameValuePair> dataList = new ArrayList<NameValuePair>();
            for (int i = 0; i < list.size(); i++) {
                NameValuePair pair = new BasicNameValuePair(String.valueOf(i), list.get(i).getName(), list.get(i).getTel(), list.get(i).getPhoto());
                dataList.add(pair);
            }
            setContainerList(dataList);
            notifyDataSetChanged();
        }

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

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

        @Override
        public String getItemString(NameValuePair t) {
            return t.getValue();
        }


        @Override
        public NameValuePair create(char letter) {
            return new BasicNameValuePair(LETTER_KEY, String.valueOf(letter),null,null);
        }

        @Override
        public boolean isLetter(NameValuePair t) {
            // 判断是不是字母行,通过key比较,这里是NameValuePair对象,其他对象,就由你自己决定怎么判断了.
            return t.getName().equals(LETTER_KEY);
        }

        @Override
        public View getLetterView(int position, View convertView, ViewGroup parent) {
            // 这里是字母的item界面设置.
            if (convertView == null) {
                convertView = new TextView(context);
                ((TextView) convertView).setGravity(Gravity.CENTER_VERTICAL);
                convertView.setBackgroundColor(getResources().getColor(android.R.color.white));
            }
            ((TextView) convertView).setText(list.get(position).getValue());
            ((TextView) convertView).setTextColor(Color.WHITE);
            ((TextView) convertView).setBackgroundColor(Color.parseColor("#548269"));

            return convertView;
        }

        @Override
        public View getContainerView(final int position, View convertView, ViewGroup parent) {
            ViewHolder mViewdata = null;
            // 这里是其他正常数据的item界面设置.
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.bossmine_contacts_list, null);
                mViewdata = new ViewHolder(convertView);
                convertView.setTag(mViewdata);
            } else {
                mViewdata = (ViewHolder) convertView.getTag();
            }
            mViewdata.name.setText(list.get(position).getValue());
            mViewdata.callphone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "正在拨打!" + list.get(position).getPhoneNumber() + "----" + position, Toast.LENGTH_SHORT).show();
                }
            });

            return convertView;
        }

        private class ViewHolder {
            ImageView icon;
            TextView name;
            SwipeRevealLayout swipeLayout;
            AutoFrameLayout callphone;


            public ViewHolder(View convertView) {
                callphone = (AutoFrameLayout) convertView.findViewById(R.id.callphone);
                icon = (ImageView) convertView.findViewById(R.id.bossmine_icon);
                name = (TextView) convertView.findViewById(R.id.bossmine_name);
                swipeLayout = (SwipeRevealLayout) convertView.findViewById(R.id.swipe_layout);

            }

        }


    }

    private String getAlphabet(String str) {
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        // 输出拼音全部小写
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        // 不带声调
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        String pinyin = null;
        try {
            pinyin = (String) PinyinHelper.toHanyuPinyinStringArray(str.charAt(0),
                    defaultFormat)[0];
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return pinyin.substring(0, 1);
    }

    /*将字符按首字母排序*/
    private void setDataOrder(List<Staff> data) {
        String[] arrays = new String[data.size()];
        for (int i = 0; i < data.size(); i++) {
            arrays[i] = data.get(i).getName();
        }
        for (int i = 0; i < arrays.length; i++) {
            String str = arrays[i];
            String alphabet = str.substring(0, 1);
            /*判断首字符是否为中文,如果是中文便将首字符拼音的首字母和&符号加在字符串前面*/
            if (alphabet.matches("[\\u4e00-\\u9fa5]+")) {
                str = getAlphabet(str) + "&" + str;
                arrays[i] = str;
            }
        }

    /*设置排序语言环境*/
        Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
        Arrays.sort(arrays, com);
        /*遍历数组,去除标识符&及首字母*/
        List<Staff> newdata = new ArrayList<>();
        for (int i = 0; i < arrays.length; i++) {
            String str = arrays[i];
            if (str.contains("&") && str.indexOf("&") == 1) {
                arrays[i] = str.split("&")[1];
                for (int j = 0; j < data.size(); j++) {
                    if (data.get(j).getName().equals(arrays[i])) {
                        newdata.add(data.get(j));
                    }
                }
            }
        }
        mTestAdapter.setlist(newdata);

    }


}

技术分享

网络获取数组信息,根据姓名按首字母排序 添加侧滑删除。

标签:

原文地址:http://www.cnblogs.com/labixiaoxin/p/5558961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!