- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试构建一个包含行的自定义 View 的列表,每行将包含一个 ImageView 和两个 TextView 。
为此,我扩展了 ArrayAdapter 类(称为 PostersArrayAdapter)并覆盖了 getView() 方法,以便在数据和行布局之间建立正确的连接。
但是,当我尝试使用一些数据构造带有 PosterData 类数组(我的实现)的 PostersArrayAdapter 时,结果是适配器为空,这意味着 getCount() 返回零并且 listView 为空。
谁能告诉我我做错了什么?我依赖于我在这里找到的代码 - http://www.vogella.de/articles/AndroidListView/article.html
非常感谢!
这里是相关代码:(PosterData类只是一个有两个字符串字段的类)
public class PostersListActivity extends ListActivity {
final private int NUM_OF_PICS = 2;
private ContentGetter cg;
private PosterData[] posters;
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cg = new ContentGetter(NUM_OF_PICS);
try
{
posters = cg.parseIndexFile();
int res = cg.DownloadPosterPics(1);
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
// Use our own list adapter
listView = (ListView)findViewById(android.R.id.list);
//listView.setAdapter((ListAdapter) new ArrayAdapter<String>(this,R.layout.list_item,R.id.title,titles));
PostersArrayAdapter postersAdapter = new PostersArrayAdapter(this, posters);
Log.d("PostersListActivity.onCreate()","Number of elementes in the data set of the adapter is " + postersAdapter.getCount());
listView.setAdapter(postersAdapter);
}
public class PostersArrayAdapter extends ArrayAdapter<PosterData> {
private final Activity context;
private final PosterData[] posters;
public PostersArrayAdapter(Activity context, PosterData[] posters) {
super(context, R.layout.list_item);
this.context = context;
this.posters = posters;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
class ViewHolder {
public ImageView logo;
public TextView title;
public TextView names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder will buffer the assess to the individual fields of the row
// layout
ViewHolder holder;
// Recycle existing view if passed as parameter
// This will save memory and time on Android
// This only works if the base layout for all classes are the same
View rowView = convertView;
if (rowView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_item, null, true);
holder = new ViewHolder();
holder.title = (TextView) rowView.findViewById(R.id.title);
holder.names = (TextView) rowView.findViewById(R.id.names);
holder.logo = (ImageView) rowView.findViewById(R.id.logo);
rowView.setTag(holder);
}
else
{
holder = (ViewHolder) rowView.getTag();
}
holder.title.setText(posters[position].getTitle());
holder.names.setText(posters[position].getNames());
holder.logo.setImageResource(R.drawable.icon);
return rowView;
}
}
}
这是我正在使用的 ListView 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/selectPoster"
android:layout_gravity="center_horizontal">
</TextView>
<ListView android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
这是我正在使用的列表元素布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:id="@+id/linearLayout1"
android:layout_height="match_parent"
android:layout_width="wrap_content" >
<ImageView android:id="@+id/logo"
android:src="@drawable/icon"
android:layout_height="wrap_content"
android:layout_width="22px"
android:layout_marginTop="4px"
android:layout_marginRight="4px"
android:layout_marginLeft="4px">
</ImageView>
<LinearLayout android:id="@+id/linearLayout2"
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/title"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px">
</TextView>
<TextView android:id="@+id/names"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
最佳答案
确保 getCount()
已正确实现,因此:
@Override
public int getCount(){
return posters!=null ? posters.length : 0;
}
编辑:通过调用
ArrayAdapter(Context context, int textViewResourceId)
构造函数您没有将对象数组传递给构造函数。
你应该调用
ArrayAdapter(Context context, int textViewResourceId, T[] objects)
构造函数也将对象数组作为参数。
关于android - ArrayAdapter 数据集中的元素数为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7483460/
我想做一个系统,用户可以上传和下载文件。系统将具有一个集中的地形,但在很大程度上依赖于节点将相关数据通过中心节点传输给其他节点我不希望对等端保存整个文件,而是希望它们保存整个数据集的一个压缩的加密部分
我正在 Riverpod Auth 流程样板应用程序中工作。 我想对所有异步功能甚至登录和注销使用通用加载屏幕。目前,如果 Appstate 加载我显示加载屏幕,我有 AppState 提供程序。它可
我有一个 functions.php 文件,其中包括以下功能: function head() { global $brand, $brandName, $logo, $slogan, $si
我有下一个 html 代码 ... 我想选择随机的 div 数组来向它们添加一个事件类,因为我使用这个 jquery 代码 function randOrder() { return
多年来,我创建并调整了一组NAnt脚本以执行完整的项目构建。主脚本采用一个应用程序端点(例如,一个Web应用程序项目),并从源代码控制中对其进行完整的构建。脚本已预先配置了与构建输出位置,源代码控制地
我希望我的 jQuery 插件在 $(window) 选择上调用时表现不同。如何检查 window 是否在集合中?到目前为止我的尝试: >>> $(window) == $(window) false
考虑到我们有 let existingSet = $(); 如何通过 jQuery 将 newElements 添加到该集合中? existingSet = existingSet.add(newEl
我需要在 priority_queue 中保存一个整数集合。但是我需要能够删除这些整数中的一个,即使它不是我容器的第一个元素。我无法使用 std::priority_queue。我选择使用一个集合来根
对于我的网站,我一直在尝试集中所有内容以便在移动设备上显示: http://m.bachatdeals.com 我在移动设备上打开网站后,内容下方有很多空间,我必须捏住 zoon 才能阅读,如何删除下
我计划为我的剑道验证器制定一些自定义规则,并希望在所有验证器之间共享。在我的验证器代码中,我有: rules: { bothorblank: function (input) {
这是我的函数,用于测试两个点 x 和 y 在 MAX_ITERATION 255 之后是否在 mandelbrot 集合中。如果不在,它应该返回 0,如果在,则返回 1。 int isMandelbr
致力于从移动设备扩展到桌面设备的简单网站布局。一切都按预期工作,但由于某种原因,我的 float div 没有集中放置。我已经完成了正常工作,但这次不适合我?有什么想法吗? 这是我的 CSS: /*
我的“div”元素有一个相对宽度,它不是绝对的,所以我不能使用精确的数字来集中。一个不错的解决方案是使用“display: inline-block”: body { text-align:
目前我拥有的所有类都处理它们自己的导入。使用一个典型的例子: [ImportMany] private Lazy[] someOfMyInterfaces { get; set; } public M
我有一个类定义: class Question: title = "" answer = "" def __init__(self, title, answer):
我正在尝试将一个对象 Point2D 插入到一个 Point2D 集合中,但我做不到,似乎该集合适用于 int 和 char 但不适用于对象。 我需要帮助来了解如何将对象插入到集合中???假设我想按
我的应用上有一些弹出窗口,它是全屏的,代码如下: content.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
我们有一个多模块 Quarkus 项目,带有一个公共(public)库和多个应用程序。在通用的 lib 中,我们有各种缓存用于所有应用。 我们希望不必在每个应用程序的所有配置文件中配置保留和容量。 有
这个问题在这里已经有了答案: Nested facets in ggplot2 spanning groups (2 个回答) 去年关闭。 我在 ggplot 中创建了一个图表里面有两个变量 face
我无法集中v-radio-group。这是我得到的:
我是一名优秀的程序员,十分优秀!