2017-06-27 13 views
0

カスタムListViewからImageView要素にアクセスして背景色を変更しようとしています。 TextViewへのアクセスと作業は問題ありません。しかし、ImageViewを使ってどうしたらいいのか分かりません。助けをお待ちしています。ListViewからImageView-Elementにアクセスする

リストは問題なく表示されます。だから、アダプターはすべて正常です。

list_layout.xml

<ImageView 
    android:id="@+id/friendIcon" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_marginBottom="5dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:layout_marginTop="5dp" 
    android:background="#0d8fe1" 
    android:src="@drawable/transparent_background_96x96" /> 

<TextView 
    android:id="@+id/friendName" 
    android:layout_width="wrap_content" 
    android:layout_height="50dp" 
    android:layout_marginLeft="15dp" 
    android:layout_marginTop="5dp" 
    android:gravity="center" 
    android:text="test" 
    android:textSize="20sp" /> 
</LinearLayout> 

activ_layout.xml(それはとにかく、この場合には関係ない?)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:layout_marginTop="?attr/actionBarSize" 
     tools:context=".activities.FriendsActivity"> 


     <!-- Here we are defining ListView in our XML file--> 
     <ListView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/list"/> 


    </RelativeLayout> 
    </RelativeLayout> 

コードをActivityから

//Declaring Array adapter 
ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(FriendsActivity.this,R.layout.friends_list_layout, R.id.friendName, FriendsActivity.arr); 



//Setting the android ListView's adapter to the newly created adapter 
mListView.setAdapter(countryAdapter); 

for(int i=0; i < mListView.getCount(); i++){ 

    //obviously wrong code. No idea how to iterate through the ListView and getting the backround color to change it 

    ImageView iview = (ImageView) mListView.findViewById(R.id.friendIcon); 
    iview.setBackgroundColor(Color.RED); 
} 
+0

何をしますか?各画像ビューや背景ごとに異なる背景? –

+0

ImageViewごとに異なる背景。私はすでにユーザーIDからカラーコードを作成する関数を持っています。 – hab55

+0

下記の私の答えをチェックしてください。 –

答えて

0

ArrayAdapterの代わりに独自のアダプタを作成する必要があります。

ここでは、実装方法を説明する良いチュートリアルです。 。

WeatherAdapter.javaは、カスタムアダプタであり、クラス内では、public View getView(int position, View convertView, ViewGroup parent)メソッドは、それぞれのリストビュー項目を拡張したものです。 (これはループのようなリストビューのすべての行に対して呼び出されます)

そこのすべての行のimageviewとtextviewにアクセスして、好みの色でそれを膨張させることができます。 `positionは、それが膨張している行の行番号です。

+0

私はこのチュートリアルで自分の目標に達しました[リンク](https://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html) – hab55

+0

あなた自身のアダプターを書くことは正しい方法です。だから私はこの答えを受け入れた。 誰かがあなたの答え+以前のコメントのリンクを読んだら、同様の問題に対する解決策を見つけるでしょう。 – hab55

+0

素晴らしい!このチュートリアルでは例を示しました。うれしいことはあなたのために何が見つかりました:) –

0

私の知る限り、どのようなリストビューにコンテンツを追加するだけですアレイ。

私は各画像ビューにアクセスできる唯一の方法は、カスタムアダプタを作成する(またはカスタムViewHolderでRecyclerViewを使用する)ことと、ゲッターメソッドまたはリスナーを実装することだと考えています。

関連する問題