質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

2530閲覧

android studio listviewのヘッダを付けたセルがさわれない

kosuke4649

総合スコア12

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2016/12/08 00:56

編集2016/12/08 06:39

androidstudio2.1.3のアプリで、ヘッダーに都道府県名、その下に
都市名と人口をリストで表示させる画面を作成したいと思っています。
例(数値は適当です)
東京都
中央区
人口:50万人
新宿区
人口:55万人
大阪府
中央区
人口:40万人
北区
人口:35万人
そこで、(http://u1aryz.blogspot.jp/2011/03/listview_27.html)で
説明していただいている内容をもとに、Activityとレイアウト2つ
(リストビュー設定のxmlと、セル一行分のxml)を作成しました。
MyArrayAdapter

java

1import android.content.Context; 2import android.view.LayoutInflater; 3import android.view.View; 4import android.view.ViewGroup; 5import android.widget.ArrayAdapter; 6import android.widget.TextView; 7 8import java.util.List; 9 10class BindData{ 11 String prefecture, city, population; 12 13 public BindData(String prefecture, String city, String population){ 14 this.prefecture = prefecture; 15 this.city = city; 16 this.population = population; 17 } 18} 19 20class ViewHolder{ 21 TextView prefecture; 22 TextView city; 23 TextView population; 24} 25 26public class MyArrayAdapter extends ArrayAdapter<BindData> { 27 28 private LayoutInflater inflater; 29 30 public MyArrayAdapter(Context context, List<BindData> objects) { 31 super(context, 0, objects); 32 33 this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 34 } 35 36 @Override 37 public boolean isEnabled(int position){ 38 39 int prev_position = position - 1; 40 String prev_pref = null; 41 if(position != 0){ 42 prev_pref = getItem(prev_position).prefecture; 43 } 44 String current_pref = getItem(position).prefecture; 45 46 if(position != 0 && current_pref.equals(prev_pref)){ 47 return true; 48 }else{ 49 return getItem(position).prefecture == null; 50 } 51 } 52 53 @Override 54 public View getView(int position, View convertView, ViewGroup parent) { 55 ViewHolder holder; 56 if (convertView == null) { 57 convertView = inflater.inflate(R.layout.course_select_row, parent, false); 58 holder = new ViewHolder(); 59 holder.prefecture = (TextView)convertView.findViewById(R.id.row_prefecture); 60 holder.city = (TextView)convertView.findViewById(R.id.row_city); 61 holder.download_date = (TextView)convertView.findViewById(R.id.row_population); 62 convertView.setTag(holder); 63 } else { 64 holder = (ViewHolder)convertView.getTag(); 65 } 66 67 BindData data = getItem(position); 68 69 //一番先頭、または一つ上の都道府県名と同じ都道府県名の場合 70 if(position == 0 ||(!isEnabled(position))){ 71 holder.prefecture.setText(data.prefecture); 72 holder.golfname.setText(data.city); 73 holder.download_date.setText(data.population); 74 }else{//それ以外 75 holder.prefecture.setVisibility(View.GONE); 76 holder.golfname.setText(data.city); 77 holder.download_date.setText(data.population); 78 } 79 return convertView; 80 } 81}

Adapterを作成しているActivity

java

1 //MyArrayAdapterのインスタンスを作成 2 MyArrayAdapter adapter1 = new MyArrayAdapter(this, bindDataList); 3 lv = (ListView)findViewById(R.id.listView2); 4 lv.setAdapter(adapter1);

セル

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="horizontal" 4 android:layout_width="fill_parent" 5 android:layout_height="wrap_content" 6 android:weightSum="1"> 7 8 <LinearLayout 9 android:orientation="vertical" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_weight="1.05"> 13 14 <TextView 15 android:background="#dcdcdc" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:text="都道府県" 19 android:id="@+id/row_prefecture" 20 android:textColor="@android:color/black" /> 21 22 <TextView 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:text="都市名" 26 android:id="@+id/row_city" 27 android:textColor="@android:color/black" /> 28 29 <LinearLayout 30 android:orientation="horizontal" 31 android:layout_width="match_parent" 32 android:layout_height="wrap_content" 33 android:layout_gravity="center_horizontal"> 34 35 <TextView 36 android:layout_width="wrap_content" 37 android:layout_height="match_parent" 38 android:text="人口:" 39 android:id="@+id/population" 40 android:layout_gravity="center" 41 android:textColor="@android:color/black" /> 42 43 <TextView 44 android:layout_width="wrap_content" 45 android:layout_height="match_parent" 46 android:id="@+id/row_population" 47 android:layout_weight="0.09" 48 android:textColor="@android:color/black" /> 49 </LinearLayout> 50 51 </LinearLayout> 52 53 54</LinearLayout>

以上のようなコードを書くと、表示はされるのですが
都道府県ヘッダーのすぐ下のセルがさわれない(クリックイベントが
発生しない)し、セル同士をわける下線もひかれない状態になっています。

ヘッダーのすぐ下の部分もセルとして認識させるには、どのように
コーディングすればよいのでしょうか。ご存知の方いらっしゃいましたら、
教えていただけませんか。

追記
セルをさわったときにおこるイベントも作成済みで、都道府県のすぐ下の
分以外はちゃんと反応します。以下コードを上げさせていただきます。

java

1lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 2 public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { 3 4 ListView list = (ListView)parent; 5 6 //選択された都市から都市IDを取得。 7 BindData bindData = (BindData)list.getItemAtPosition(pos); 8 9 //都市詳細画面へ遷移 10 Intent intent = new Intent(); 11 intent.putExtra("city", bindData.city); 12 13 intent.setClassName("jp.co.sample.myappli", 14 "jp.co.sample.myappli.CityListActivity"); 15 16 startActivity(intent); 17 } 18 });

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ヘッダーとは自作のヘッダーですね。
あなたは東京都と中央区は別のpositionになると考えていませんか?

1行の中をあなたが2つに分けているだけなので東京都と中央区は同じpositionになります、そのためisEnableで東京都のクリックを無反応にすると中央区も無反応になります。
また、もともと1行なので区切り線もありません。

解決するには中央区や人口のTextView方にクリックリスナーを付けてしまうか、データ構造を見直すことが考えられます。

投稿2016/12/08 02:00

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kosuke4649

2016/12/08 04:34

回答ありがとうございます。 たびたびですみませんが、 アイテムクリック時のイベントを追記させていただいたように コーディングしているのですが、TextViewの方にクリックリスナーを つけるためにはどうすればよいのでしょうか。 lv.findViewById(R.id.row_dldate)(new AdapterView.OnItemClickListener(){ }); と書くと Class 'onItemClickListenerから派生したクラス'must either be decleared abstract or implement abstract method 'onItemClick.... というコンパイルエラーになってしまいます。
yona

2016/12/08 05:55

・独自インターフェースを作成し、BindDataを引数に持つメソッドを定義してください。 ・MyArrayAdapterに上記のインターフェースのフィールドを作成し、アクセスメソッドを作成してください。 ・MyArrayAdapterでTextViewにOnClickListenerを設定してください。 ・TextViewに設定したOnClickListenerの中で独自作成したインターフェースのメソッドを呼び出してください。 ・MyArrayAdapterの呼び出し側で独自インターフェースの実装とMyArrayAdapterへ独自インターフェースをアクセスメソッドで設定する処理を書いてください。
kosuke4649

2016/12/21 09:55

色々試してみた結果、isEnabledを自作の関数に 名前を変えることでうまくいきました。 どうやら、isEnabledはクラス内で呼び出さなくても、 自動的に呼び出されてセルに対してさわれる、触れないを 設定する関数だったためみたいです。 助言いただきましてありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問