Java 在哪里可以找到创建自定义ArrayAdapter的好教程?

Java 在哪里可以找到创建自定义ArrayAdapter的好教程?,java,android,android-arrayadapter,Java,Android,Android Arrayadapter,我正在尝试为我的应用程序构建一个自定义适配器,它的主屏幕是一个ListActivity,中间有一个ListView。我完全不知道如何创建适配器,所以我想知道是否有人知道有什么好的教程可以指导我学习。我记得学习ArrayAdapters并不容易。网上有很多教程 扩展ArrayAdapter可能需要重写方法 getCount()-列表中的项目数 public int getViewTypeCount()-ArrayList显示数据的方式。通常这是1。但是,如果列表中有多种类型的数据,并且有多个视图,

我正在尝试为我的应用程序构建一个自定义适配器,它的主屏幕是一个
ListActivity
,中间有一个
ListView
。我完全不知道如何创建适配器,所以我想知道是否有人知道有什么好的教程可以指导我学习。

我记得学习ArrayAdapters并不容易。网上有很多教程

扩展ArrayAdapter可能需要重写方法

getCount()-列表中的项目数

public int getViewTypeCount()-ArrayList显示数据的方式。通常这是1。但是,如果列表中有多种类型的数据,并且有多个视图,则必须更改此数字。联系人列表就是一个例子。它有两种类型。1表示联系人本身,另一个是您看到的A、B等字母类别

getItemViewType(position)-对于职位,它应该得到什么类型?通常,1。除非你有多种类型

public long getItemId(int位置)-您要显示的项目类型

真的很重要! 公共视图getView(int位置、视图转换视图、视图组父视图)

  • 位置显然是列表中的当前项
  • convertView用于查看 回收如果一个项目 从屏幕上滚动,其视图为 持有并等待重新使用 另一项将在 屏幕。这有助于提高性能 您将只有大约8个 屏幕上的最大视图相同。 最初convertView将为空 因为它不能重用任何东西,只能作为 用户在屏幕上滚动,您将看到 抓住他们。视图组家长我不是这样的 当然不仅仅是在视图中使用它 通货膨胀
下面是使用 ViewHolder图案。观众 帮助使您的滚动平滑 因为你不必重做这些 一次又一次地寻找视频内容 再一次。每次更新视图时 根据新信息,使用 holder.WidgetName来执行此操作

/* Hypotetical list of names. */
@Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        ListItemWithSideButton view;

        if(convertView == null) {
            view = (ListItemWithSideButton)_inflater.inflate(R.layout.some_line_item, parent, false);
            holder = new ViewHolder();
            view.setTag(holder);

            holder.SomeButton = view.findViewById(R.id.some_button);
            /* Other views. */
        }
        else {
            view = (ListItemWithSideButton) convertView;
            holder = (ViewHolder) convertView.getTag();
        }

            /* Get the item. */
        final String name = getItem(position);

            /* Update the data of the view with the new information. */
        holder.SomeButton.setText(name);

        return view;
    }

    static class ViewHolder {
    Button SomeButton;
    /* Other views. */
  }

}

现在我没有任何参考资料可供您参考,但这是您可能正在做的事情,以获得您想要的:

您可能在XML中有ListView,因此在代码中实例化ListView对象:

ListView myList = (ListView)findViewById(R.id.myListView)
一旦获得对它的引用,就创建一个扩展BaseAdapter的自定义类

一个好主意是将这个类放在Activity类中,这样它就可以访问Activity包含的所有数据

在扩展BaseAdapter时,您需要做一些事情才能使其正常工作

下面我将解释所有这些,但实现BaseAdapter的getView()方法是最重要的。 每次ListView绘制一行时,运行时系统都会调用此方法

所以您应该在这个方法中完成所有的工作,您希望对一行完成这些工作

找到下面的代码:

Class myListActivity extends Activity{
... some code here...
public void onCreate(Bundle savedInstanceState){
.....
myList.setAdapter(new myCustomAdapter());
.....
}

/**
*Custom Adapter class for the ListView containing data
*/
class myCustomAdapter extends BaseAdapter{

TextView userName;
/**
 * returns the count of elements in the Array that is used to draw the text in rows 
   * @see android.widget.Adapter#getCount()
 */
@Override
public int getCount() {
    //return the length of the data array, so that the List View knows how much rows it has to draw   
return DataArr.length;
}

/**
 * @param position The position of the row that was clicked (0-n)
 * @see android.widget.Adapter#getItem(int)
                     */
@Override
public String getItem(int position) {
    return null;
}

/**
 * @param position The position of the row that was clicked (0-n)
 * @see android.widget.Adapter#getItemId(int)
 */
@Override
public long getItemId(int position) {
    return position;
}

/**
 * Returns the complete row that the System draws.
 * It is called every time the System needs to draw a new row;
 * You can control the appearance of each row inside this function.
 * @param position The position of the row that was clicked (0-n)
 * @param convertView The View object of the row that was last created. null if its the first row
 * @param parent The ViewGroup object of the parent view
 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    final int pos = position;
    if(row == null){
                    //getting custom layout to the row
        LayoutInflater inflater=getLayoutInflater();
        row=inflater.inflate(R.layout.list_row, parent, false);
    }
            //get the reference to the textview of your row. find the item with row.findViewById()
    userName= (TextView)row.findViewById(R.id.user_name);
    userName.setText(DataArr[position]);
return row; //the row that ListView draws
}
}
}
希望对你有帮助

请记住在单独的布局文件中创建行的布局

如果你想更深入,可以去CommonsGuy的网站试试。这是他那本很棒的书的节选,这本书专门讨论ListView自定义适配器


编辑:这是我的博客文章,也是一个示例项目:

好的,适配器使用的列表必须是标准数组还是ArrayList?