自定义Android列表视图
我对这个优秀的网站和Android的Java编程一无所知。我开始制作一个小的测试应用程序,列出我在镇上最喜欢的地方。我试着在不同的页面上学习一些教程,但是当我在Eclipse中加入我的项目时,总是会给我带来超过一百万个错误,尽管会导入类和其他方法 我想在图像名称discos旁边的示例图像discoque上构建,并在该名称下创建Diskotek较小的文本附加信息自定义Android列表视图,android,list,view,Android,List,View,我对这个优秀的网站和Android的Java编程一无所知。我开始制作一个小的测试应用程序,列出我在镇上最喜欢的地方。我试着在不同的页面上学习一些教程,但是当我在Eclipse中加入我的项目时,总是会给我带来超过一百万个错误,尽管会导入类和其他方法 我想在图像名称discos旁边的示例图像discoque上构建,并在该名称下创建Diskotek较小的文本附加信息 非常感谢所有的帮助一个自定义列表视图总共需要4个基本的东西2个设计(layout.xml)和2个类(.java) 2布局 a) 基本上,
非常感谢所有的帮助一个
自定义列表视图
总共需要4个基本的东西2个设计(layout.xml
)和2个类(.java
)
2布局a) 基本上,具有标题或按钮的
列表视图的容器取决于您
b) 如果每一行都有按钮
,图像
,文本视图
您想要的样子,那么每一行应该是什么样子
2 Java类文件
a) 一个是你肯定会参加的活动。
b) 自定义适配器
,它根据您的要求说明您的活动
中的哪个值将转到哪个视图
(按钮,图像
)
最好的例子就是遵循这个当有人说‘我试着遵循一些教程……’时,我想的第一件事是,它们不起作用,令人难以置信
- 你试过的密码在哪里
- 编辑器上的导入错误有哪些
这将是一个更容易解决的问题
给您一个简单的ListView示例:
首先,根据您的喜好创建一个资源文件:(example.xml)
然后,我们创建一个ListActivity作为示例注意ListActivit不需要通过setContentView设置布局资源,因此我们这里不调用它
public class ExampleListActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//create the adapter
ExampleAdapter mExampleAdapter = new ExampleAdapter(this);
//fill the listView
setListAdapter(mExampleAdapter);
}
}
这应该按原样编译,但出于性能原因,您可能希望查看ViewHolder模式和诸如此类的东西。显然你需要多读一些,但我希望这能作为一个起点有所帮助 帮什么忙?你没有问任何问题,也没有就你遇到的问题向我们提供任何详细信息问题是:我如何才能创建一个像我解释的那样的列表视图编辑正是我所要求的。非常感谢,先生@安德罗:那么请你做+1:我很乐意帮忙
public class ExampleAdapter extends BaseAdapter {
//Let's create some constants first, to fill out the rows
private static final String [] DISCO_NAMES = {"Disco One", "Disco Two", "Disco Three", "Disco Four"};
private static final String [] DISCO_INFO = {"Some Info One", "Some Info Two", "Some Info Three", "Some Info Four"};
private LayoutInflater mInflater;
//Our custom adapter needs a constructor, so you can create from your activity.
public ExampleAdapter (final Context context) {
//for now, let's just get the context, we'll need it to inflate views
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
//this needs to return to the amount of rows you want to display.
//right now we return a fixed value, this could vary based on your needs
return DISCO_NAMES.length;
}
@Override
public Object getItem(int pos) {
//this is useful for knowing what item is at what position
//for now, let's just return the disco name shall we?
return DISCO_NAMES[pos];
}
@Override
public long getItemId(int pos) {
//This returns an id to the item
//personally I don't use this, so you can just return the position
return pos;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
//Ha, here's the important part
//ListViews reuse rows, so let's check if the view (also known as convertview) is new or being reused
if (view == null) {
//this means it's a new view, so we need to inflate it
view = mInflater.inflate(R.layout.example, null);
}
((TextView) view.findViewById(R.id.disco_title)).setText(DISCO_NAMES[position]);
((TextView) view.findViewById(R.id.disco_info)).setText(DISCO_INFO[position]);
//You can also set some images to the imageview on the layout we created earlier
return view;
}
}
public class ExampleListActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//create the adapter
ExampleAdapter mExampleAdapter = new ExampleAdapter(this);
//fill the listView
setListAdapter(mExampleAdapter);
}
}