Section Indexer未在listview中显示

时间:2016-11-18 04:17:11

标签: android listview android-adapter

我的自定义适配器如下: -

public class ListAdapter extends ArrayAdapter<Person> implements SectionIndexer {
        private HashMap<String, Integer> alphaIndexer;
        private String[] sections;

        public ListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }    
        public ListAdapter(Context context, int resource, ArrayList<Person> items) {
            super(context, resource, items);
            alphaIndexer = new HashMap<String, Integer>();
            for (int i = 0; i < items.size(); i++)
            {
                Log.d("State",items.get(i).getState().substring(0, 1).toUpperCase());
                String s = items.get(i).getState().substring(0, 1).toUpperCase();
                alphaIndexer.put(s, i);
            }    
            Set<String> sectionLetters = alphaIndexer.keySet();
            ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
            Collections.sort(sectionList);
            sections = new String[sectionList.size()];
            sectionList.toArray(sections);
        }
        public int getPositionForSection(int section)
        {
            return alphaIndexer.get(sections[section]);
        }    
        public int getSectionForPosition(int position)
        {
            return 1;
        }    
        public Object[] getSections()
        {
            return sections;
        }    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {    
            View v = convertView;    
            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.bystate_itemview, null);
            }    
            Person p = getItem(position);    
            if (p != null) {
                ImageView im1= (ImageView) v.findViewById(R.id.legislator_image);
                TextView tt1 = (TextView) v.findViewById(R.id.firstname);
                TextView tt2 = (TextView) v.findViewById(R.id.details);
                ImageView im2 = (ImageView) v.findViewById(R.id.getDetails);

                if (im1 != null){
                    String url = p.getImage();
                    Picasso.with(getContext()).load(url).fit().centerCrop().into(im1);
                }

                if (tt1 != null) {
                    String name= p.getLastname()+", "+p.getFirstname();
                    tt1.setText(name);
                }
                if (tt2 != null) {
                    String details="";
                    if(p.getDistrict()!="NA")
                        details= "("+p.getParty()+")"+p.getState()+" - "+"District "+p.getDistrict();
                    else
                        details= "("+p.getParty()+")"+p.getState()+" - "+"District 0";
                    tt2.setText(details);
                }
                if (im2 != null) {
                    Drawable myDrawable = getResources().getDrawable(R.drawable.right);
                    im2.setImageDrawable(myDrawable);
                }
            }  
            return v;
        }
    }

我已经按照教程实现了这个自定义适配器。我已经实现了部分索引器。但问题是部分索引器没有显示在应用程序中。我试图根据人的每个州的第一个字母索引东西。我希望按字母顺序排列的列表显示在列表视图中,这样当我点击某个特定的字母时,它必须显示属于该状态的人。

我的xml文件: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/state_listView"
        android:layout_weight="1"
        android:layout_marginTop="35dp"
        android:fastScrollEnabled="true"/>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:layout_marginTop="40dp"
    android:background="@drawable/gradient">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1">
        <ImageView
            android:layout_width="55dp"
            android:layout_height="49dp"
            android:layout_margin="5dp"
            android:id="@+id/legislator_image"
            android:layout_weight="0.19"
            android:layout_alignParentLeft="true"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginLeft="80dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="bold"
                android:text="Medium Text"
                android:id="@+id/firstname"
                android:layout_marginTop="3dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/details"
                android:layout_marginTop="3dp" />
        </LinearLayout>
        <ImageView
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_marginTop="20dp"
            android:layout_marginRight="3dp"
            android:layout_alignParentRight="true"
            android:id="@+id/getDetails"/>
    </RelativeLayout>
</LinearLayout>

如果我错过了什么,请告诉我。这是我的第一个自定义适配器,所以我可能做错了。

1 个答案:

答案 0 :(得分:0)

使用下面的结构,Mayi知道你为什么要实现sectionIndex

 public class CustomeAdapter extends BaseAdapter {


    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        return null;
    }
}