Here is the Simple Code for alphabet indexing in ListView, which uses 2 java program files
1.The MainActivity file where we handling the indexing of listview
2.The CustomAdapter.java file which is the subclass of BaseAdapter for listing the elements.
MainActivity.java
public class MainActivity extends Activity implements View.OnClickListener
{
private Map<String, Integer> mapIndex;
private String[] fruits;
private ListView fruitsList;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
fruitsList.setAdapter(new CustomAdapter(this, Arrays.asList(fruits)));
getIndexList(fruits);
displayIndex();
}
private void init()
{
fruitsList = (ListView)findViewById(R.id.list_fruits);
fruits = getResources().getStringArray(R.array.fruits_array);
Arrays.sort(fruits);
}
private void getIndexList(String[] fruits)
{
mapIndex = new LinkedHashMap<String, Integer>();
for(int i=0; i<fruits.length; i++)
{
String fruit = fruits[i];
String index = fruit.substring(0,1);
if(mapIndex.get(index) == null)
mapIndex.put(index, i);
}
}
private void displayIndex()
{
LinearLayout indexLayout = (LinearLayout)findViewById(R.id.side_index);
List<String> indexList = new ArrayList<String>(mapIndex.keySet());
TextView textView;
for(String index : indexList)
{
textView = (TextView) getLayoutInflater().inflate(R.layout.alphabetindicator, null);
textView.setText(index);
textView.setOnClickListener(this);
indexLayout.addView(textView);
}
}
@Override
public void onClick(View v) {
TextView selectedTextView = (TextView) v;
fruitsList.setSelection(mapIndex.get(selectedTextView.getText()));
}
}
CustomAdaper.java
public class CustomAdapter extends BaseAdapter
{
private Context context;
private List<String> listItem;
private LayoutInflater layoutInflater;
CustomAdapter(Context context, List<String> listItem) {
this.context = context;
this.listItem = listItem;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.indexindicator, parent, false);
viewHolder.textView = (TextView) convertView.findViewById(R.id.listitem);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(listItem.get(position));
return convertView;
}
static class ViewHolder {
TextView textView;
}
@Override
public int getCount() {
return listItem.size();
}
@Override
public Object getItem(int position) {
return listItem.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
}
activity_main.xml
<LinearLayout>
<ListView
android:id="@+id/list_fruits"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:scrollbars="none">
</ListView>
<LinearLayout
android:id="@+id/side_index"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@color/white"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
alphabetIndicator.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/side_list_item"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="3dp"
android:textSize="14sp" />
IndexIndicator.xml
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/listitem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="3dp"
android:textColor="#000000"
android:textSize="14sp" />
</LinearLayout>
Finally, Include a string array in strings.xml file (Replace / Increase the item based on your needs)
<string-array name="fruits_array">
<item>Cantalope</item>
<item>Date</item>
<item>Grape</item>
<item>Gooseberry</item>
<item>Guava</item>
<item>Honeydew melon</item>
<item>Elderberry</item>
<item>Fig</item>
<item>Grapefruit</item>
</string-array>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…