【发布时间】:2016-10-11 03:05:05
【问题描述】:
我正在使用包含图像和按钮的自定义列表视图。
我的 listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
</LinearLayout>
我在这里处理 3 个线性布局。
我的button_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"
android:id="@+id/parent"
>
<LinearLayout
android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/child1"
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/image"
/>
<TextView
android:id="@+id/listarray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:id="@+id/child2">
<ImageButton
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/one"
/>
<ImageButton
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/two"
/>
<ImageButton
android:id="@+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
适配器代码
tweetArray 包含我的列表。
ArrayAdapter<String> arrayAdapter =new ArrayAdapter<String>(this,R.layout.button_view, R.id.listarray, tweetArray);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(arrayAdapter);
问题:我的问题是如何获得ImageView 的ID。
试过了:我尝试过简单的方法
ImageView user_picture = (ImageView) findViewById(R.id.image);
Picasso.with(getApplicationContext()).load("http://www.w3schools.com/css/trolltunga.jpg).into(user_picture);
但这显示错误:目标不能为空。
你有什么想法吗...?
【问题讨论】:
-
如果该代码在您的
Adapter中,您需要在为列表项充气的View上调用findViewById()。 -
怎么做@MikeM。
-
您需要为您的
Adapter发布代码。 -
downvoters 也请您分享原因。这可能会有所帮助。
-
这不是您使用自定义项目布局创建
ListView的方式。您需要实现一个自定义的Adapter类。看看this post。
标签: android android-layout listview picasso