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

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

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

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

1回答

2689閲覧

Android listviewにSectionを追加したい

退会済みユーザー

退会済みユーザー

総合スコア0

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2016/02/08 14:32

Android初心者です。

作成をしたlistviewについて、Sectionを追加したいと考え、こちらの記事を参考にさせていただきました。
ヘッダー行のみ表示したい場合どうすればよいのか教えていただきたいです。
よろしくお願い致します。
下部分に自分が行ったことを記述させていただきました。
http://dev.classmethod.jp/smartphone/android-listview-ios-uitableview/
ヘッダー行とデータ行について記載されていました。
こちらはSectionListFragment.javaの部分をActivityにすることで再現できたのですが、
自分の作った別のlistviewにヘッダー行だけを再現したいと思い、
BaseSectionAdapter.javaをまず引用させていただき

package jp.classmethod;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BaseSectionAdapter<T1, T2> extends BaseAdapter {

/** インデックス行:ヘッダー */
private static final int INDEX_PATH_ROW_HEADER = -1;

/** ビュータイプ:ヘッダー行 */
private static final int ITEM_VIEW_TYPE_HEADER = 0;

/** ビュータイプ:データ行 */
private static final int ITEM_VIEW_TYPE_ROW = 1;

protected Context context;
protected LayoutInflater inflater;

/** ヘッダー行で使用するデータリスト */
protected List<T1> sectionList;

/** データ行で使用するデータリスト */
protected List<List<T2>> rowList;

private List<IndexPath> indexPathList;

public BaseSectionAdapter(Context context, List<T1> sectionList, List<List<T2>> rowList) {
super();
this.context = context;
this.inflater = LayoutInflater.from(context);
this.sectionList = sectionList;
this.rowList = rowList;
this.indexPathList = getIndexPathList(sectionList, rowList);
}

@Override
public int getCount() {
int count = indexPathList.size();
return count;
}

@Override
public Object getItem(int position) {
IndexPath indexPath = indexPathList.get(position);
if (isHeader(indexPath)) {
return sectionList.get(indexPath.section);
} else {
return rowList.get(indexPath.section).get(indexPath.row);
}
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
IndexPath indexPath = indexPathList.get(position);

// ヘッダー行とデータ行とで分岐します。 if (isHeader(indexPath)) { return viewForHeaderInSection(convertView, indexPath.section); } else { return cellForRowAtIndexPath(convertView, indexPath); }

}

/**

  • ヘッダー行のViewを返します。
  • @param convertView
  • @param section
  • @return ヘッダー行のView

*/
public View viewForHeaderInSection(View convertView, int section) {
if (convertView == null) {
convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);
TextView castedConvertView = (TextView) convertView;
castedConvertView.setBackgroundColor(Color.GRAY);
castedConvertView.setTextColor(Color.WHITE);
}
TextView textView = (TextView) convertView;
textView.setText(sectionList.get(section).toString());
return convertView;
}

/**

  • データ行のViewを返します。
  • @param convertView
  • @param indexPath
  • @return データ行のView

*/
public View cellForRowAtIndexPath(View convertView, IndexPath indexPath) {
if (convertView == null) {
convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);
}
TextView textView = (TextView) convertView;
textView.setText(rowList.get(indexPath.section).get(indexPath.row).toString());
return convertView;
}

@Override
public int getViewTypeCount() {
// ヘッダー行とデータ行の2種類なので、2を返します。
return 2;
}

@Override
public int getItemViewType(int position) {
// ビュータイプを返します。
if (isHeader(position)) {
return ITEM_VIEW_TYPE_HEADER;
} else {
return ITEM_VIEW_TYPE_ROW;
}
}

@Override
public boolean isEnabled(int position) {
if (isHeader(position)) {
// ヘッダー行の場合は、タップできないようにします。
return false;
} else {
return super.isEnabled(position);
}
}

/**

  • インデックスパスリストを取得します。
  • @param sectionList
  • @param rowList
  • @return インデックスパスリスト

*/
private List<IndexPath> getIndexPathList(List<T1> sectionList, List<List<T2>> rowList) {
List<IndexPath> indexPathList = new ArrayList<IndexPath>();
for (int i = 0; i < sectionList.size(); i++) {
IndexPath sectionIndexPath = new IndexPath();
sectionIndexPath.section = i;
sectionIndexPath.row = INDEX_PATH_ROW_HEADER;
indexPathList.add(sectionIndexPath);

List<T2> rowListBySection = rowList.get(i); for (int j = 0; j < rowListBySection.size(); j++) { IndexPath rowIndexPath = new IndexPath(); rowIndexPath.section = i; rowIndexPath.row = j; indexPathList.add(rowIndexPath); } } return indexPathList;

}

private boolean isHeader(int position) {
IndexPath indexPath = indexPathList.get(position);
return isHeader(indexPath);
}

private boolean isHeader(IndexPath indexPath) {
if (INDEX_PATH_ROW_HEADER == indexPath.row) {
return true;
} else {
return false;
}
}
}

このrowの部分を削除していきました。
ヘッダー行とデータ行の2種類なので~のところはヘッダー行のみのため、
return 1;としました。
@Override
public Object getItem(int position) {
IndexPath indexPath = indexPathList.get(position);
if (isHeader(indexPath)) {
return sectionList.get(indexPath.section);
} else {
return rowList.get(indexPath.section).get(indexPath.row);
}
}
のelse~の部分をreturn indexpath;
とし、その他のelse分をreturnをそれぞれ入力し赤いエラーはなかったのですが、
実行をした際に、
null object referenceというエラーが出てしまいました。
上に記述したreturn indexpathのみは、return true; 等にしても
入力できた記憶があるのですが、
間違っている箇所はこちらであっているでしょうか。
また、もし完全に考え方が間違っており、違う場所の修正が必要な場合、教えていただきたいです。
長々と申し訳ございません。
よろしくお願い致します。

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

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

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

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

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

guest

回答1

0

ログキャットにnull object referenceが発生した行が書かれているはずです。その行は質問のコードのどこに当たるのでしょうか。
それがわかればなぜnullなのかを考える事で解決すると思いますよ。

投稿2016/02/09 03:06

yona

総合スコア18155

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問