すごく単純なところなのに詰まったのでメモ。
TextViewに表示させようとして、以下のエラーで落ちた。
android.content.res.Resources$NotFoundException: String resource ID #0x5dc
TextViewに設定しているidの名前も間違ってないし、クリーンもしたし、
eclipseの再起動もしたしなんで???と思っていたら、
原因は「int型をセットしていた」ことだった。
ちょっと疑問には思ったけど、コンパイルエラーにならないからいいやと思っていたらダメだった。
かなり残念すぎるバグ・・・。
java, android, php, cakephp, centOS, mysql, html, css, js, jQuery などなどで、 つまづいたことをとりあえずメモするために作りました。 そのうちフォーマットとかきれいにしたい。
2013年3月14日木曜日
2013年3月12日火曜日
ListViewの中にImageButtonを使うとonItemClickが呼ばれなくなる?
ListViewをカスタマイズして実装しているときに、
リスト1つ1つの中にImageButtonがあると、
リスト全体のクリックイベントも、ImageButtonのクリックイベントも拾ってくれないらしい。
解決策としては、
1. xmlのImageButtonに
android:focusableInTouchMode="false"
android:focusable="false"
を追加する
または
javaで以下を設定する
((ImageButton)v.findViewById(R.id.recommend_checkin)).setFocusableInTouchMode(false);
((ImageButton)v.findViewById(R.id.recommend_checkin)).setFocusable(false);
2. ***_item.xml(リストの中身のレイアウト)の一番外に
android:descendantFocusability="blocksDescendants"
を追加する
例)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:gravity="center" >
<ImageView
android:id="@+id/recommend_product_image"
android:layout_width="250dp"
android:layout_height="250dp"
android:background="#e5fff7"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:gravity="left|center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/recommend_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="@+id/recommend_pickup_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
これで、全体のクリックイベントを拾ってくれるようになる。
さらに、ImageButtonのクリックイベントを拾うためには、
adapterの中でonClickイベントを拾ってやればいいらしい。
参考URL
http://tomstay.hatenablog.jp/entry/20110421/1303391426
http://blog.babukuma.com/2010/01/android-listviewbutton.html
リスト1つ1つの中にImageButtonがあると、
リスト全体のクリックイベントも、ImageButtonのクリックイベントも拾ってくれないらしい。
解決策としては、
1. xmlのImageButtonに
android:focusableInTouchMode="false"
android:focusable="false"
を追加する
または
javaで以下を設定する
((ImageButton)v.findViewById(R.id.recommend_checkin)).setFocusableInTouchMode(false);
((ImageButton)v.findViewById(R.id.recommend_checkin)).setFocusable(false);
2. ***_item.xml(リストの中身のレイアウト)の一番外に
android:descendantFocusability="blocksDescendants"
を追加する
例)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:gravity="center" >
<ImageView
android:id="@+id/recommend_product_image"
android:layout_width="250dp"
android:layout_height="250dp"
android:background="#e5fff7"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:gravity="left|center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/recommend_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="@+id/recommend_pickup_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
これで、全体のクリックイベントを拾ってくれるようになる。
さらに、ImageButtonのクリックイベントを拾うためには、
adapterの中でonClickイベントを拾ってやればいいらしい。
参考URL
http://tomstay.hatenablog.jp/entry/20110421/1303391426
http://blog.babukuma.com/2010/01/android-listviewbutton.html
登録:
投稿 (Atom)