2段のリストビューを作りチェックボックスを各行に配置する方法をご教授ください

1,262 views
Skip to first unread message

成田恭一

unread,
Oct 4, 2010, 10:05:01 PM10/4/10
to android-group-japan
お世話になっております。成田です。

リストビューを2段にしてそのリストにチェックボックスをおきたいのですが、
2段にすることと、チェックボックスをリストにつけることを別々にはできるのですが
一遍にすることができず困っております。

どなたかやり方をご教授願えませんか。

あんざいゆき

unread,
Oct 5, 2010, 12:25:58 PM10/5/10
to android-g...@googlegroups.com
成田さん

 こんばんは。
 あんざいです。

 2段の意味がよくわからなったのですが、
 ExpandableList のことですか?
 添付画像のイメージでしたら、
 一番簡単な方法は、ApiDemo の ExpandableList3.java
 http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList3.html
 の android.R.layout.simple_expandable_list_item_2
 を android.R.layout.simple_list_item_single_choice
 にすれば、レイアウトとしては実現できます。
 ただし、これだけだとラジオボタン(チェックボックスでも同じ)を
 タップしてもチェック状態になりません。
 このあたり ExpandalbeList はちょっとめんどいのです。

 意図したものと違うのに長々書くのもあれなので、
 もし、添付画像が意図したものなら、続きを書きますので。



--
あんざい ゆき
anzai...@gmail.com
twitter : @yanzm


2010年10月5日11:05 成田恭一 <narit...@gmail.com>:

--
このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。
このグループから退会するには、android-group-j...@googlegroups.com にメールを送信してください。
詳細については、http://groups.google.com/group/android-group-japan?hl=ja からこのグループにアクセスしてください。



ExpandableListCapture.png

成田恭一

unread,
Oct 6, 2010, 8:34:24 PM10/6/10
to android-g...@googlegroups.com
あんざいさん。返信ありがとうございます。
2段の意味がわかりずらくてすみません。
2段とはひとつのリストビューの行にテキストを2行つけると言う意味です。
つまり
--------------------------------------------------------------
さくら(独唱)                       |チェックボックス|
森山直太郎                     |                      |
--------------------------------------------------------------
ありがとう                         |チェックボックス|
いきものがかり                  |                     |
-------------------------------------------------------------
                      :
                      : 
このようなリストを作りたいのです

2010年10月6日1:25 あんざいゆき <anzai...@gmail.com>:

飯塚康至

unread,
Oct 6, 2010, 8:52:10 PM10/6/10
to android-g...@googlegroups.com
こんにちは、飯塚です。
リストの各行のレイアウトファイルを作って、SimpleCursorAdapter でセットすればできると思います。

SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.flist_row, // レイアウト
cursor, // カーソル
new String[] {"NAM", "DJ", "GNL", "_id"}, // 表示するデータのカラム名
new int[] {R.id.fbroad_cast_station, R.id.fdj_name,
R.id.fcategory, R.id.fadditional}); // 表示先のViewのID

ではでは

2010年10月7日9:34 成田恭一 <narit...@gmail.com>:

--
以上、宜しくお願いします。

飯塚 康至

あんざいゆき

unread,
Oct 6, 2010, 9:10:11 PM10/6/10
to android-g...@googlegroups.com
ごめん。まちがった。

 Checkable を implements するのは custom view のほうです。

2010年10月7日10:04 あんざいゆき <anzai...@gmail.com>:
成田さん

 あんざいです。
 意味わかりましたー。
 この場合、 Checkable を implements した Adapter を自作しないと
 だめですね(多分)。
 CheckedTextView は TextView を継承したもので、
 1行用なのです。
 
 あとでサンプルコードのせるので、ちょっとまっててください。


2010年10月7日9:34 成田恭一 <narit...@gmail.com>:

あんざいゆき

unread,
Oct 6, 2010, 9:04:22 PM10/6/10
to android-g...@googlegroups.com
成田さん

 あんざいです。
 意味わかりましたー。
 この場合、 Checkable を implements した Adapter を自作しないと
 だめですね(多分)。
 CheckedTextView は TextView を継承したもので、
 1行用なのです。
 
 あとでサンプルコードのせるので、ちょっとまっててください。


2010年10月7日9:34 成田恭一 <narit...@gmail.com>:
あんざいさん。返信ありがとうございます。

成田恭一

unread,
Oct 6, 2010, 9:46:22 PM10/6/10
to android-g...@googlegroups.com
お世話になっております。成田です。

やっぱり自作ですか。

お返事ありがとうございます。

PS。サンプルを提示していただけたら本当に助かります

2010年10月7日9:52 飯塚康至 <iiz...@acl-inc.co.jp>:

あんざいゆき

unread,
Oct 6, 2010, 9:52:49 PM10/6/10
to android-g...@googlegroups.com
成田さん

 あんざいです。

 こんな感じでどうでしょう

---- MyActivity.java ----
package yanzm.example.checkeddoubletextview;

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

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MyActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayList<myTextClass> list = new ArrayList<myTextClass>();

        for(int i=0; i < GENRES.length; i++){
         myTextClass data = new myTextClass();
         data.text1 = GENRES[i];
         data.text2 = GENRES[i] + ":" + GENRES[i];

         list.add(data);
        }
        
        setListAdapter(new MyAdapter2(this,list));

        final ListView listView = getListView();

        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setItemChecked(0, true);
    }

private static final String[] GENRES = new String[] { "Action",
"Adventure", "Animation", "Children", "Comedy", "Documentary",
"Drama", "Foreign", "History", "Independent", "Romance", "Sci-Fi",
"Television", "Thriller" };

class MyAdapter2 extends ArrayAdapter<myTextClass> {
//private LayoutInflater mInflater;

public MyAdapter2(Context context, List<myTextClass> objects) {
super(context, 0, objects);
}

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder2 holder;

if (convertView == null) {
convertView = new CheckedDoubleTextView(getContext());
holder = new ViewHolder2();
holder.text1 = (TextView) convertView.findViewById(R.id.text1);
holder.text2 = (TextView) convertView.findViewById(R.id.text2);

convertView.setTag(holder);
} else {
holder = (ViewHolder2) convertView.getTag();
}
myTextClass ttt = getItem(position);

holder.text1.setText(ttt.text1);
holder.text2.setText(ttt.text2);

return convertView;
}
}

static class ViewHolder2 {
TextView text1;
TextView text2;
}

class myTextClass {
private String text1, text2;
}
}


---- main.xml ----

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
   android:orientation="vertical"
        android:layout_width="wrap_content"
   android:layout_height="fill_parent"
   android:layout_weight="1"
    >
<TextView  
android:id="@+id/text1"
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello"
    />
<TextView  
android:id="@+id/text2"
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
   android:text="@string/hello"
    />    
    </LinearLayout>
    <CheckBox
android:id="@+id/checkmark" 
   android:layout_width="wrap_content" 
     android:layout_height="wrap_content"
     android:gravity="center_vertical"
    />
</LinearLayout>


---- CheckedDoubleTextView.java ----

package yanzm.example.checkeddoubletextview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.ListView;

public class CheckedDoubleTextView extends LinearLayout implements Checkable {
private CheckBox _checkedTextView;

public CheckedDoubleTextView(Context context) {
super(context);

setLayoutParams(new ListView.LayoutParams(
ListView.LayoutParams.FILL_PARENT,
ListView.LayoutParams.WRAP_CONTENT));

View view = ((LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.main, this, false);
_checkedTextView = (CheckBox) view
.findViewById(R.id.checkmark);

addView(view);
}

@Override
public boolean isChecked() {
return _checkedTextView.isChecked();
}

@Override
public void setChecked(boolean checked) {
_checkedTextView.setChecked(checked);
}

@Override
public void toggle() {
setChecked(!isChecked());
}
}




2010年10月7日10:46 成田恭一 <narit...@gmail.com>:

成田恭一

unread,
Oct 6, 2010, 10:14:10 PM10/6/10
to android-g...@googlegroups.com
あんざいさん

成田です。
サンプルありがとうございました。
動作確認をして確かに動く事を確認しました!!

本当にありがとうございます。

2010年10月7日10:52 あんざいゆき <anzai...@gmail.com>:

あんざいゆき

unread,
Oct 7, 2010, 12:21:02 AM10/7/10
to android-g...@googlegroups.com
成田さん

  この場合だと
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  ではなく
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  が適切でした。
 
  あんざい


2010年10月7日11:14 成田恭一 <narit...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages