カスタム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);
}
何をしますか?各画像ビューや背景ごとに異なる背景? –
ImageViewごとに異なる背景。私はすでにユーザーIDからカラーコードを作成する関数を持っています。 – hab55
下記の私の答えをチェックしてください。 –