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

发布于 2024-10-05 20:40:46 字数 1536 浏览 8 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空‖城人不在 2024-10-12 20:40:46

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

扩展 ArrayAdapter 您可能需要重写方法。

getCount() - 列表中的项目数。

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

getItemViewType(position) - 对于职位,它应该获取什么类型?通常,1.除非您有多种类型。

public long getItemId(intposition) - 您要呈现的项目类型。

真正重要的一个!
public View getView(intposition,ViewconvertView,ViewGroupparent)

  • 位置显然是列表中的当前项目。
  • ConvertView 用于查看
    回收。如果一个项目
    滚动离开屏幕,它的视图是
    持有并等待被重用
    另一个项目将出现在
    屏幕。这有助于提高性能,因为
    你只会有大约8个左右
    屏幕上的相同视图最多
    最初convertView将为null
    因为它不能重用任何东西,除了作为
    用户在屏幕上滚动
    得到他们。 ViewGroup 父级我不是这样
    肯定超出了在视图中使用它的范围
    通货膨胀。

下面是 getView 使用的示例
ViewHolder 模式。视图持有者
有助于使您的滚动更加流畅
因为你不必重做这一切
findViewById 的东西一遍又一遍
再次。每次更新视图时
与新信息一起使用
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. */
  }

}

I remember learning ArrayAdapters and it wasn't easy. There are plenty of tutorials on the internet.

Extending ArrayAdapter you'll probably want to override methods.

getCount() - Number of items in your list.

public int getViewTypeCount() - Number of ways data will be presented by the ArrayList. Usually this is 1. But if your list has multiple types of data in it that has multiple views then you'll have to change this number. An Example is the contacts list. It has 2 types. 1 for the contact itself and the other is the letter category that you see for A's, B's, etc.

getItemViewType(position) - For the position, what type should it get? Usually, 1. Unless you have multiple types.

public long getItemId(int position) - The type of item you're going to present.

The really important one!
public View getView(int position, View convertView, ViewGroup parent)

  • Position obviously is the current item in the list.
  • The convertView is for view
    recycling. What that means if an item
    scrolls off the screen, it's view is
    held and waiting to be reused by
    another item that will come on the
    screen. This helps performance since
    you'll only have about 8 or so of the
    same view on the screen max.
    Initially convertView will be null
    since it can't reuse anything but as
    the user scrolls on the screen you'll
    get them. ViewGroup parent I'm not so
    sure beyond using it in view
    inflation.

Below is an example of getView using
the ViewHolder pattern. ViewHolder
helps makes your scrolling smooth
since you don't have to redo all that
findViewById stuff over and over
again. Each time you update the view
with the new information use the
holder.WidgetName to do it.

/* 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. */
  }

}

假面具 2024-10-12 20:40:46

现在我没有任何参考可以给您,但这就是您可能会做的事情来获得您想要的:

您可能在 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 自定义适配器

编辑:这是我关于它的博客文章以及一个示例项目:http://thetechnib.blogspot.com/2010/12/android-tutorial-custom-adapter-for.html

Right now I am not having any reference to give you, but this is what you might be doing to get what you want:

You might have the ListView in the XML, so instantiate a ListView object in your code:

ListView myList = (ListView)findViewById(R.id.myListView)

As soon as you get reference to it, create a custom class that extends BaseAdapter.

A good idea will be to put this class inside your Activity class so that it can access all the data that your Activity holds.

While extending BaseAdapter, there are some things that you need to do in order to get things working.

I am explaining all of them below, but implementing the getView() method of the BaseAdapter is the most important thing.
This method will be called by the runtime system everytime the ListView draws a row.

So you should be doing all inside this method, that you would want to be done for a single row.

Find the code below:

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
}
}
}

Hope it helped you.

Remember to create the layout of the row in a separate layout file.

If you're trying to get deeper, try this link at CommonsGuy's website. It is an excerpt to his awesome book, that specifically deals with the ListView custom adapters

EDIT: here's my blog post about it and a sample project too: http://thetechnib.blogspot.com/2010/12/android-tutorial-custom-adapter-for.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文