简体   繁体   English

在Android上使用适配器的最佳方法是什么?

[英]What's the best way to use Adapters on Android?

What's the best way to use Adapters on Android? 在Android上使用适配器的最佳方法是什么? We can use it for several customized operations. 我们可以将它用于多个定制操作。 By using the adapter we will include some pre-implemented methods. 通过使用适配器,我们将包括一些预先实现的方法。 When should I use these methods? 我什么时候应该使用这些方法? How can we improve our apps' performance by using the Adapter implemented methods? 我们如何通过使用Adapter实现的方法来提高应用程序的性能?

public int getCount() {
        // TODO Auto-generated method stub
        return country.length;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
}

Look out this sample example and the link 看看这个示例和链接

public class GridDemo extends Activity implements AdapterView.OnItemClickListener {
    private TextView selection;
    private static final String[] items={"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat", "placerat", "ante", "porttitor", "sodales", "pellentesque", "augue", "purus"};

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        selection=(TextView)findViewById(R.id.selection);
        GridView g=(GridView) findViewById(R.id.grid);
        g.setAdapter(new ArrayAdapter<String>(this, R.layout.cell, items));
        g.setOnItemClickListener(this);
    }

    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        selection.setText(items[position]);
    }
}

You can get an idea for this Adapters.. 你可以知道这个适配器..

BaseAdapter is a great choice for simply using ListView or GridView . BaseAdapter是简单使用ListViewGridView的绝佳选择。 But if you want to get data from the database, a CursorAdapter is much more well suited for that, though the doc says this is for ListView . 但是如果你想从数据库中获取数据,那么CursorAdapter更适合这种情况,尽管doc说它适用于ListView So a SimpleCursorAdapter would be the other type you'd use if you want both ListView and GridView usage when pertaining to showing data you get from a database. 因此,如果您希望在显示从数据库获取的数据时同时使用ListViewGridViewSimpleCursorAdapter将是您使用的另一种类型。 But of course knowing your implementation would help me better give you the best choice. 但当然知道你的实施会帮助我更好地给你最好的选择。

I should also add since I saw it on one of the comments, ArrayAdapter is basically a BaseAdapter , but the difference being it takes arrays of arbitrary objects aka best suited for arrays straight out of the box. 我也应该添加,因为我在其中一条评论中看到它, ArrayAdapter基本上是一个BaseAdapter ,但区别在于它需要任意对象的数组,最适合直接开箱即用的数组。

getCount() is a must. getCount()是必须的。 It tells ListView, GridView or etc that how many items to display. 它告诉ListView,GridView等要显示多少项。 getItem() and getItemId() in many cases (if you implement getView() by your own code) are actually optional, they are just helper methods. getItem()和getItemId()在很多情况下(如果你自己的代码实现getView())实际上是可选的,它们只是辅助方法。 There methods are basic to an adapter, it doesn't help on performance improvement. 有适配器的基本方法,它对性能改进没有帮助。 I think getView()'s implementation effect performance the most. 我认为getView()的实现效果性能最强。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM