2016-06-10 30 views
-2

我正在使用包含圖像和按鈕的自定義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

你有什麼想法......?

+0

如果代碼是在你的'Adapter',你需要調用'findViewById()'的'View'你」重新爲列表項目充氣。 –

+0

如何做到@MikeM。 – Atula

+0

您需要發佈適配器的代碼。 –

回答

-1

嘗試自定義您的適配器 請檢查鏈路適配器

http://www.vogella.com/tutorials/AndroidListView/article.html

+0

什麼是你得到的錯誤,發佈logcat – SaravInfern

+1

鏈接只有答案[不認爲是好的做法](http://meta.stackexchange.com/questions/7515/why-is-linking-bad)堆棧溢出。 「答案」這個問題實際上應該包含一個答案,或者至少包含內容的總結以及它與問題的關係。 –

+0

結果爲空 – Atula