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 });
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/08 04:34
2016/12/08 05:55
2016/12/21 09:55