我正在尝试为我的应用程序构建一个自定义适配器,其中主屏幕为ListActivity
,中心为ListView
。对于我如何创建适配器,我绝对无能为力,所以我想知道是否有人知道任何好的教程来引导我完成它。我在哪里可以找到创建自定义ArrayAdapter的好教程?
回答
现在我没有任何参考给你,但是这是你可能会做什么让你想要什么:
您可能必须在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的网站上尝试this link。这是一个摘录他的真棒书,是专门使用ListView自定义适配器
交易编辑:这里是我的博客文章关于它和一个示例项目过于:http://thetechnib.blogspot.com/2010/12/android-tutorial-custom-adapter-for.html
我记得学习了ArrayAdapters,并不容易。互联网上有很多教程。
扩展ArrayAdapter你可能会想要覆盖方法。
getCount() - 列表中的项目数。
public int getViewTypeCount() - 数据将由ArrayList呈现的方式数量。通常这是1.但是如果你的列表有多种类型的数据,它有多个视图,那么你将不得不改变这个数字。一个示例是联系人列表。它有两种类型。 1为联系本身,另一种是你看到的A,B等的字母类别。
getItemViewType(position) - 对于位置,它应该得到什么类型?通常情况下,1.除非你有多种类型。
public long getItemId(int position) - 您将要呈现的项目的类型。
真的很重要! 公共查看getView(INT位置,查看convertView,ViewGroup以及母公司)
- 的位置显然是在列表中的当前项目。
- convertView用于查看 回收。这是什么意思,如果一个项目 滚动屏幕,它的视图是 保持并等待被 重复使用的另一项将在 屏幕上。这有助于提高性能,因为 您只能在屏幕上显示大约8张左右的 相同视图。 最初convertView将为空 ,因为它不能重用任何东西,但作为 用户在屏幕上滚动,你会 得到它们。 ViewGroup的父母我不是这样 肯定无法使用它在视图 通货膨胀。
下面是使用ViewHolder模式的 的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. */
}
}
- 1. 我在哪里可以找到创建电子邮件模板的好教程?
- 2. 我在哪里可以找到最好的钢轨ActiveRecord教程?
- 3. 我可以在哪里找到一个好的Robocode教程?
- 4. 我在哪里可以找到一个好的XMPP(Jabber)教程?
- 5. 我在哪里可以找到关于冒泡的好教程?
- 6. 我在哪里可以找到一个很好的iPhone自学教程?
- 7. 哪里可以找到PhoneGap教程?
- 8. 我在哪里可以找到很好的资源用于创建自定义UI钛应用程式
- 9. 我在哪里可以找到关于iPhone/Objective-C多线程的好教程?
- 10. 我在哪里可以找到多线程教程?
- 11. 我在哪里可以找到GoogleMaps JavaScript API的优秀教程?
- 12. 我在哪里可以找到Prestashop主题系统的教程?
- 13. 我在哪里可以找到Cocoa的NSSearchField和NSTableView教程?
- 14. 我在哪里可以找到Clang的教程?
- 15. 我在哪里可以找到Tkinter的更新教程
- 16. 我在哪里可以找到完整的EJB教程?
- 17. 我在哪里可以找到非常广泛的Drupal教程?
- 18. 我在哪里可以找到Android的UI设计教程
- 19. 我在哪里可以找到一个流利的NHibernate教程?
- 20. 我在哪里可以找到完整的DirectShow教程?
- 21. 我在哪里可以找到Java的JMF教程
- 22. 我可以在哪里找到Mutagen的文档/教程? (Python)
- 23. 我在哪里可以找到适合HTML5的优秀教程?
- 24. 我在哪里可以找到当前的EJB教程?
- 25. 我在哪里可以找到分步的MySQL复制教程?
- 26. 我在哪里可以找到创建Java Web应用程序的好教程Gcloud
- 27. 我在哪里可以找到一个很好的谷歌任务API教程?
- 28. 我在哪里可以找到一个好的wordpress主题开发教程?
- 29. 我在哪里可以找到一个很好的FFT示例实现/教程?
- 30. 我在哪里可以找到一些很好的WS-Security介绍和教程?
好吧,确实正与使用的列表适配器必须是一个标准的数组,或者它可以是一个ArrayList? – Scooter 2010-12-07 02:26:03