2016-11-18 63 views
0

我的定義適配器如下: -部分索引不列表視圖顯示

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> 

請讓我知道如果我失去了一些東西。這是我的第一個自定義適配器,所以我可能做錯了什麼。

+0

也許這個鏈接可以幫助您與您的問題: - http://stackoverflow.com/questions/12559843/how-to-implement- A-列表視圖與 - fastscroll-和albhabet-索引 – Bhavnik

回答

0

只需要用下面的結構,馬邑知道爲什麼youimplement 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; 
    } 
}