Android Open Source - APO-DZ-Android Alphabetical Adapter






From Project

Back to project page APO-DZ-Android.

License

The source code is released under:

MIT License

If you think the Android project APO-DZ-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.upennapo.app.adapter;
//w w w.j a v  a  2s  . c o m
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import org.upennapo.app.R;
import org.upennapo.app.model.Brother;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class AlphabeticalAdapter extends ArrayAdapter<Brother> implements SectionIndexer {

    private HashMap<String, Integer> mAlphaIndexer;
    private String[] mSections;

    public AlphabeticalAdapter(Context context, List<Brother> brothers) {
        super(context, R.layout.item_brother, R.id.txt_name, brothers);
        setupSections(brothers);
    }

    private void setupSections(List<? extends Brother> data) {
        mAlphaIndexer = new HashMap<String, Integer>();
        for (int i = 0; i < data.size(); i++) {
            String s = data.get(i).toString().substring(0, 1).toUpperCase();
            if (!mAlphaIndexer.containsKey(s))
                mAlphaIndexer.put(s, i);
        }

        Set<String> sectionLetters = mAlphaIndexer.keySet();
        ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
        Collections.sort(sectionList);
        mSections = new String[sectionList.size()];
        for (int i = 0; i < sectionList.size(); i++)
            mSections[i] = sectionList.get(i);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.item_brother, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.nameLabel = (TextView) convertView.findViewById(R.id.txt_name);
            viewHolder.pledgeClassLabel = (TextView) convertView.findViewById(R.id.txt_pledge_class);
            viewHolder.phoneNumLabel = (TextView) convertView.findViewById(R.id.txt_phone);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final Brother brother = getItem(position);
        viewHolder.nameLabel.setText(brother.toString());
        viewHolder.pledgeClassLabel.setText(brother.Pledge_Class + " | " + brother.gradAbbreviation());
        viewHolder.phoneNumLabel.setText(brother.Phone_Number);
        return convertView;
    }

    public int getPositionForSection(int section) {
        return mAlphaIndexer.get(mSections[section]);
    }

    public int getSectionForPosition(int position) {
        return 0;
    }

    public Object[] getSections() {
        return mSections;
    }

    private static class ViewHolder {
        TextView nameLabel;
        TextView pledgeClassLabel;
        TextView phoneNumLabel;
    }
}




Java Source Code List

org.upennapo.app.activity.AlumModeActivity.java
org.upennapo.app.activity.DirectoryActivity.java
org.upennapo.app.activity.DirectoryDetailsActivity.java
org.upennapo.app.activity.EasterEggActivity.java
org.upennapo.app.activity.LoginActivity.java
org.upennapo.app.activity.MainActivity.java
org.upennapo.app.activity.WebActivity.java
org.upennapo.app.adapter.AlphabeticalAdapter.java
org.upennapo.app.adapter.LinkAdapter.java
org.upennapo.app.fragment.AlumDirectoryFragment.java
org.upennapo.app.fragment.BrotherStatusFragment.java
org.upennapo.app.fragment.DirectoryFragment.java
org.upennapo.app.fragment.LinkListFragment.java
org.upennapo.app.fragment.NavigationDrawerFragment.java
org.upennapo.app.fragment.WebFragment.java
org.upennapo.app.model.Brother.java
org.upennapo.app.model.DataManager.java
org.upennapo.app.model.User.java