Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android字母表部分列表视图,如联系人应用程序列表视图_Android - Fatal编程技术网

Android字母表部分列表视图,如联系人应用程序列表视图

Android字母表部分列表视图,如联系人应用程序列表视图,android,Android,现在有谁能帮我看一下我的观点吗。请帮帮我,我太糊涂了 这是我的代码:------- phonebooklistview.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_hei

现在有谁能帮我看一下我的观点吗。请帮帮我,我太糊涂了

这是我的代码:-------

phonebooklistview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/searchTxtBox"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="65dp"
        android:hint="@string/searchHintTxt"
        android:singleLine="true"
        android:drawableLeft="@android:drawable/ic_search_category_default"
        android:drawablePadding="0dp"
        android:text="" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fadingEdge="vertical"
        android:fastScrollEnabled="true"
        android:padding="2dp"
        android:layout_below="@+id/searchTxtBox" >
    </ListView>

    <TextView
        android:id="@+id/phoneBookEmptyView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_gravity="center_horizontal"
        android:text="@string/phoneBookEmptyMsg"
        android:textColor="@color/white"
        android:layout_below="@+id/searchTxtBox"
        android:layout_marginTop="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

和在PhoneBookList.java文件中

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.phonebooklistview);

    listView = getListView();
    adapter = new ItemsAdapter(this);
    adapter.notifyDataSetChanged();
    listView.setAdapter(adapter);
    listView.setTextFilterEnabled(true);
    //listView.setFastScrollEnabled(true);
    listView.setOnItemLongClickListener(this);
    listView.setItemsCanFocus(false);
    listView.setEmptyView(findViewById(R.id.phoneBookEmptyView));
    registerForContextMenu(listView);

}



private class ItemsAdapter extends BaseAdapter implements SectionIndexer
{
    HashMap<String, Integer> alphaIndexer;
    String[] sections;
      private LayoutInflater inflater;
      String[][] items;

      public ItemsAdapter(Context context)
      {
          inflater = LayoutInflater.from(context);
          this.items = phoneBookDataArr;

            alphaIndexer = new HashMap<String, Integer>();
            int size = items.length;

            for (int x = 0; x < size; x++)
            {
                String name=items[x][2];
                String ch =  name.substring(0, 1);
                ch = ch.toUpperCase();
                alphaIndexer.put(ch, x);

                Log.e(TAG,"alphaIndexer="+ch);
            }

            Set<String> sectionLetters = alphaIndexer.keySet();
            ArrayList<String> sectionList = new ArrayList<String>(sectionLetters); 
            Collections.sort(sectionList);
            sections = new String[sectionList.size()];
            sectionList.toArray(sections);
      }

      //@Override
      public View getView(final int position, View convertView, ViewGroup parent)
      {
    // here my custom listviewcontentview goes.
      }

      public int getCount()
      {
          return items.length;
      }

      public Object getItem(int position)
      {
          return position;
      }

      public long getItemId(int position)
      {
          return 0;
      }

      public int getPositionForSection(int section)
      {
          Log.e(TAG,"getPositionForSection="+section);
          return alphaIndexer.get(sections[section]);
      }

      public int getSectionForPosition(int position)
      {
          Log.e(TAG,"getSectionForPosition="+position);
          return 1;
      }

      public Object[] getSections()
      {
          Log.e(TAG,"getSections="+sections.length);
          for (String str : sections)
          {
            Log.e(TAG,str);
          }
          return sections;
      }
}
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.phonebooklistview);
listView=getListView();
适配器=新项目适配器(此);
adapter.notifyDataSetChanged();
setAdapter(适配器);
setTextFilterEnabled(true);
//setFastScrollEnabled(true);
setOnItemLongClickListener(这个);
setItemsCanFocus(false);
setEmptyView(findViewById(R.id.phoneBookEmptyView));
registerForContextMenu(列表视图);
}
私有类ItemsAdapter扩展BaseAdapter实现SectionIndexer
{
HashMap字母索引器;
字符串[]段;
私人充气机;
字符串[][]项;
公共项适配器(上下文)
{
充气器=充气器。从(上下文);
this.items=phoneBookDataArr;
alphaIndexer=新HashMap();
int size=items.length;
用于(int x=0;x
添加搜索栏 1:对搜索栏使用编辑文本
2:将其注册到addTextChangedListener
3:实现onTextChanged(…)方法以跟踪用户输入的字符序列4:使用getText()方法获取currentSearchName。迭代联系人列表以查找匹配的搜索。将匹配的结果存储在列表中,并使用内容适配器显示列表

下面的链接提供了详细的实现

添加标签和侧栏: 1:在ContentAdapter类中添加边栏工具节索引器
2:迭代联系人列表以获取所有姓名的首字母缩写
3:执行setSection(..)方法来设置标签
4:实现getPositionForSection(..)以在用户单击侧边栏时转到与首字母相对应的标签

边栏的实现可以在下面的链接中找到。 ?

您可以检查