我正在使用包含圖像和按鈕的自定義Listview。在多個LinearLayout中獲取ImageView id
我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個LinearLayouts。
我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);
但這正顯示出錯誤:目標不能爲null。
你有什麼想法......?
如果代碼是在你的'Adapter',你需要調用'findViewById()'的'View'你」重新爲列表項目充氣。 –
如何做到@MikeM。 – Atula
您需要發佈適配器的代碼。 –