ListViewにTextを2つ入れる方法

487 views
Skip to first unread message

Stone

unread,
Oct 5, 2010, 11:26:24 PM10/5/10
to 日本Androidの会
Strone@調布、東京です

ListViewに文字サイズの違うTextを2つ入れたいのですが方法がわかりません。
以下のようなxmlを作ったんですが、エラーが出ます。
どなたかよろしくお願いします。


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/rowtext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14pt"
android:scrollingCache="false"
android:textColor="@color/black"
/>
<TextView
android:id="@+id/rowtext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12pt"
android:scrollingCache="false"
android:textColor="@color/black"
/>
</LinearLayout>

Stone

unread,
Oct 5, 2010, 11:38:12 PM10/5/10
to 日本Androidの会
Stoneです。

ソースはこんなかんじです。

class MyAdapter extends ArrayAdapter<myTextClass>
{
private LayoutInflater iInflater;


public MyAdapter(Context context, int textViewResourceId,
List<myTextClass> objects)
{
super(context, textViewResourceId, objects);
iInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


// 1行分のViewを取得/作成するとこ
public View getView(int aPos, final View aView, ViewGroup
aParent)
{
View view = aView;
if (view == null)
{
view = iInflater.inflate(R.layout.row_2text, null);
}


TextView txtView1 =
(TextView)view.findViewById(R.id.rowtext1);
TextView txtView2 =
(TextView)view.findViewById(R.id.rowtext2);

myTextClass ttt=getItem(aPos);

txtView1.setText(ttt.text1);
txtView2.setText(ttt.text2);

return view;
}

}

class myTextClass{
private String text1,text2;

}


えらーはこんな感じ

10-06 12:33:43.599: ERROR/ArrayAdapter(8048): You must supply a
resource ID for a TextView
10-06 12:33:43.599: WARN/dalvikvm(8048): threadid=3: thread exiting
with uncaught exception (group=0x2aadda10)
10-06 12:33:43.599: ERROR/AndroidRuntime(8048): Uncaught handler:
thread main exiting due to uncaught exception
10-06 12:33:43.599: ERROR/AndroidRuntime(8048):
java.lang.IllegalStateException: ArrayAdapter requires the resource ID
to be a TextView

あんざいゆき

unread,
Oct 6, 2010, 12:17:45 AM10/6/10
to android-g...@googlegroups.com
Stone さん

 あんざいです。

 こんな感じでどうですか?

 ListView では ViewHolder を使った方がいいです。
 あと、xml ですが、フォント指定では pt ではなく sp を
 つかいましょう。デバイスの解像度の違いを吸収してくれます。


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

public MyAdapter2(Context context, List<myTextClass> objects) {
super(context, 0, objects);
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

// 1行分のViewを取得/作成するとこ
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder2 holder;

   if (convertView == null) {
    convertView = mInflater.inflate(R.layout.row_2text, 
                                     parent, false);
     holder = new ViewHolder2();
     holder.text1 = (TextView) convertView.findViewById(R.id.rowtext1);
     holder.text2 = (TextView) convertView.findViewById(R.id.rowtext2);

     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;
}

2010年10月6日12:38 Stone <ish...@cap.bekkoame.ne.jp>:

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




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

Stone

unread,
Oct 6, 2010, 12:34:53 AM10/6/10
to 日本Androidの会
わ!うまくいきました。
ありがとうございます。
> > このグループから退会するには、android-group-j...@googlegroups.com<android-gro-up-japan%2Bunsu...@googlegroups.com>にメールを送信してください。
> > 詳細については、http://groups.google.com/group/android-group-japan?hl=jaからこのグループにアクセスしてください。
>
> --
> あんざい ゆき
> anzai.y...@gmail.com
> twitter : @yanzm- 引用テキストを表示しない -
>
> - 引用テキストを表示 -
Reply all
Reply to author
Forward
0 new messages