android.R.id.text1の場所と要約

このQ&Aのポイント
  • android.R.id.text1は、Androidのリソースとして定義されています。
  • android.R.id.text1は、テキストビューコンポーネントのIDです。
  • android.R.id.text1は、一般的にはListViewやExpandableListViewのアイテムレイアウトで使用されます。
回答を見る
  • ベストアンサー

android.R.id.text1はどこにある

androidアプリ開発 を読みながら、作業しています。 ( 184ページ ) サンプルのコード SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, g_list, android.R.layout.simple_expandable_list_item_1, new String[] { "group_title"}, new int[]{android.R.id.text1 }, c_list, android.R.layout.simple_expandable_list_item_2, new String[] {"child_title", "child_text" }, new int[] { android.R.id.text1, android.R.id.text2 } ); elv.setAdapter(adapter); elv.setOnChildClickListener( new OnChildClickListener() { public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { TextView txt = (TextView) ((TwoLineListItem) v).findViewById(android.R.id.text1); Toast.makeText(AddrListShowActivity.this, txt.getText(), Toast.LENGTH_LONG).show(); return false; } } は、動くのですが、欲張って SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, g_list, android.R.layout.simple_expandable_list_item_1, new String[] { "group_title"}, new int[]{android.R.id.text1 }, c_list, android.R.layout.simple_expandable_list_item_2, new String[] {"child_title", "child_text" }, new int[] { android.R.id.text2, android.R.id.text3 } ); elv.setAdapter(adapter); elv.setOnChildClickListener( new OnChildClickListener() { public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { TextView txt = (TextView) ((TwoLineListItem) v).findViewById(android.R.id.text1); Toast.makeText(AddrListShowActivity.this, txt.getText(), Toast.LENGTH_LONG).show(); TextView txt2 = (TextView) ((TwoLineListItem) v).findViewById(android.R.id.text2); Toast.makeText(AddrListShowActivity.this, txt2.getText(), Toast.LENGTH_LONG).show(); TextView txt3 = (TextView) ((TwoLineListItem) v).findViewById(android.R.id.text3); Toast.makeText(AddrListShowActivity.this, txt3.getText(), Toast.LENGTH_LONG).show(); return false; } } とすると、android.R.id.text3  が原因で動きません。 そもそも、android.R.id.text1 の  text1 は何処にあるのでしょうか。 アドバイスよろしくお願いします。  

質問者が選んだベストアンサー

  • ベストアンサー
noname#217196
noname#217196
回答No.1

ふつうはlayout.xmlファイルで、画面オブジェクトを定義していなければ使えないよ。text3をレイアウトファイルにも追加済みか、スペルミスがないか要確認。

uyama33
質問者

お礼

ありがとうございます。 res - layout の所には、 antivity_main.xml だけがあって その中身は、以下のようになっています。 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ExpandableListView android:id="@+id/elv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </ExpandableListView> </RelativeLayout> よく見ても、無いのです。 amdroid の プログラムでは、C言語の 外部変数 のようなものは無いのでしょうか? それがあれば、簡単なのですが、、、

関連するQ&A

  • Android JAVAへでの質問です

    (←※この部分!) の意味がわからないのでどなたか教えていただけないでしょうか? private EditText edit1; ですが、なぜ private がついているのでしょうか?なくても動きましたが何か意味があるのでしょうか? そしてどうして、この子はここにいるのでしょうか? Button btn1 と一緒に仲良くいてもいいんじゃないでしょうか? ================ import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.view.*; import android.view.View.OnClickListener; import android.widget.*; public class Test2Activity extends Activity { private EditText edit1;(←※この部分!) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); edit1 = (EditText)this.findViewById(R.id.EditText1); Button btn1 = (Button)this.findViewById(R.id.button1); btn1.setOnClickListener(new MyClickA()); } class MyClickA implements OnClickListener { public void onClick(View view){ Editable e = edit1.getText(); int n = Integer.parseInt(e.toString()); Toast toast = Toast.makeText(getApplicationContext(), "数値=" + n, Toast.LENGTH_SHORT); toast.show(); } } }

    • ベストアンサー
    • Java
  • Androidのレイアウトについて

    XMLで作成したTextViewを処理中に色を変えることは出来ないのでしょうか? 下記のようにやれば実装できるのでは?と思ってやってみましたがダメでした、 XMLファイルを使った方法で文字の色を変更する事は可能ですか? hogehoge.xml---------------------------------------------- <TextView android:id="@+id/text" android:textColor="@color/black" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="hogehoge" /> hogehoge.java--------------------------------------------- EditText textView=(EditText)findViewById(R.id.text); textView.setTextColor(Color.RED); ---------------------------------------------------------

  • android studioのlistview

    listviewに他画面から、受け取ったデータを追加する方法について、質問です。 他画面から、データを受け取って、textviewに表示させることは出来たのですが、listに追加することが出来ません。 text = (TextView)findViewById(R.id.++); と text.setText(data.getCharSequenceExtra("key")); という感じでテキストには表示することは可能でした。listに追加していく方法を教えて下さい。 list.add(data.getCharSequenceExtra("key"));じゃ、出来ませよね。。

  • android TableLayout使用時で

    いつもお世話になっています。 android TableLayout使用時のことで質問します。 forでTextViewに値を入れていきたいのですが、このときxmlでTextViewをひとつ作っておいてそのTextViewを使いまわすことはできないのでしょうか? したいのが、 for文で、TableRowに値を入れていきたい。 private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; private final int FP = ViewGroup.LayoutParams.FILL_PARENT; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); TableLayout tableLayout = new TableLayout(this); setContentView(tableLayout); for(int a=0;a<3;a++){ TextView text1 = new TextView(this); text1.setText("user"); TableRow tableRow1 = new TableRow(this); tableRow1.addView(text1); tableLayout.addView(tableRow1, createParam(FP, WC)); } } private TableLayout.LayoutParams createParam(int w, int h){ return new TableLayout.LayoutParams(w, h); } この時表示されたのが、 user user user この方法を、xmlのTextViewを呼ぶ形で実現したいのですが、 xmlでは、 <TableRow> ↓ このTextViewを使いまわしたい。 <TextView android:id="@+id/user" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Test"> </TextView> </TableRow> で、表記しています。 xmlでは、普通に先に記述していないと使えないのでしょうか? つまり、もしTableにTextViewを3つ使いたいなら、TextViewを既に3つ記述していないと使えないものなのでしょうか? よろしくお願いします。

    • ベストアンサー
    • Java
  • androidのサンプルソースについての質問

    このページを参考に学習をしていたのですが http://libro99.appspot.com/index3?id=29&page=2&label=android .javaに追加するぶぶんで text.setText(selradio.getText()); ~~~~ の波線の部分のエラーをどうしても消すことができず 困っています package test.radiobutton; import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; public class radiobuttontest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); RadioGroup group = (RadioGroup)this.findViewById(R.id.group); RadioButton radio1 = (RadioButton)this.findViewById(R.id.radio1); RadioButton radio2 = (RadioButton)this.findViewById(R.id.radio2); radio2.setChecked(true); int selid = group.getCheckedRadioButtonId(); RadioButton selradio = (RadioButton)this.findViewById(selid); text.setText(selradio.getText()); } } main.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="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <RadioGroup android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:text="@string/radio1_label" android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:text="@string/radio2_label" android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup> </LinearLayout> これ以上はかけませんがページの指示通りにやってるつもりです どなたかご教授お願いします

    • ベストアンサー
    • Java
  • Android用のアプリケーション開発

    私は下記の開発環境でAndroid用のアプリケーションを開発しています。 Eclipse Version: 3.6.2 Android 2.3.3 TableLayoutで3行3列のテーブルの、4行目の3列目と5行目の3列目を結合したいですが出来ませんでした。 下記のソースコードの「あいうえおかきくけ」の部分を画像にして2行分の大きさで表示させたいです。 Android用のアプリケーションの開発が初めてですが、ご教授を頂けると幸いです。 下記はソースコードの一部です。 --------------------------------------- //1行目 TextView text1 = new TextView(this); text1.setText("         "); TextView text2 = new TextView(this); text2.setText("投資情報"); TextView text3 = new TextView(this); text3.setText("         "); TableRow tableRow1 = new TableRow(this); tableRow1.addView(text1); tableRow1.addView(text2); tableRow1.addView(text3); tableLayout.addView(tableRow1, createParam(FP, WC)); //2行目 TextView text4 = new TextView(this); text4.setText("先週の情報"); TableRow tableRow2 = new TableRow(this); tableRow2.addView(text4); tableLayout.addView(tableRow2, createParam(FP, WC)); //3行目 TextView text8 = new TextView(this); text8.setText("2011年11月7日"); text8.setWidth(480); text8.setGravity(Gravity.RIGHT); TableRow tableRow3 = new TableRow(this); TableRow.LayoutParams rowLayout3 = new TableRow.LayoutParams(); rowLayout3.span = 3; tableRow3.addView(text8, rowLayout3); tableLayout.addView(tableRow3, createParam(FP, WC)); //4行目 ImageView image1 = new ImageView(this); image1.setImageResource(R.drawable.tyuugoku); TextView text11 = new TextView(this); text11.setText("国名"); TextView text12 = new TextView(this); text12.setText("あいうえおかきくけ"); TableRow tableRow4 = new TableRow(this); TableRow.LayoutParams rowLayout4 = new TableRow.LayoutParams(); rowLayout4.column = 2; tableRow4.addView(image1); tableRow4.addView(text11); tableRow4.addView(text12); tableLayout.addView(tableRow4, createParam(FP, WC)); //5行目 TextView text13 = new TextView(this); text13.setText("記事リード"); TableRow tableRow5 = new TableRow(this); TableRow.LayoutParams rowLayout5 = new TableRow.LayoutParams(); tableRow5.addView(text13); tableLayout.addView(tableRow5, createParam(FP, WC)); ---------------------------------------- 長文になりましたが、どうぞ宜しくお願い致します。

    • ベストアンサー
    • Java
  • 【Android開発】ボタンのテキスト表示

    Android初心者です。現在画面遷移先の画面に配列で設定した質問文、選択肢を表示するアプリを制作しています。質問文はTextView、選択肢は2つのButtonにテキストとして表示する。画面遷移した後にsetQuestion()、setChoiceを呼び出したいんですが実機で試したところ、画面遷移した後に「問題が発生しため、(アプリ名)を終了します」とポップアップが出て、アプリが落ちます。 この2つのメソッドをonCreateに入れるのは間違なのでしょうか?どうすればうまくいきますか?初心者なのでうまく書けてないかも知れません。 よろしくお願いします。 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub); setChoice(); setQuestion();  } int number = 0; //質問番号 //選択肢 Button button1; Button button2; Button button3; Button button4; //質問文を配列に格納 static String question[] = { "質問文1", "質問文2", }; //選択肢 static String choice[][] = { {"1番", "2番", "3番", "4番"}, {"1番", "2番", "3番", "4番"}, }; //問題の回答 static String answer[] = { "2番", "4番" }; //出力 //質問文を表示 public void setQuestion() { String setQuestion = question[number]; // TextView question = (TextView) findViewById(R.id.Question); question.setText(setQuestion); } //選択肢の表示 public void setChoice() { TextView button1 = (Button) findViewById(R.id.button1); TextView button2 = (Button) findViewById(R.id.button2); button1.setText(choice[number][0]); button2.setText(choice[number][1]); } xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Question" android:id="@+id/Question" android:textSize="40dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="81dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:textSize="30dp" android:onClick="choiceClick" android:layout_above="@+id/button2" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:textSize="30dp" android:onClick="choiceClick" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/button1" android:layout_alignStart="@+id/button1" />

  • AlertDialogでのURL画像表示

    現在、アンドロイド開発で「AlertDialog.Builder」を使用し、URL画像を表示するコーディングを試みております。 しかし、「R.id.image」の部分をURL画像で指定するとエラーが出力してしまいます。 LayoutInflater inflater = LayoutInflater.from(AlerDialog.this); View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText(country_name + "\n" + sex + "\n" + receipt_end_date); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.icon); new AlertDialog.Builder(AlerDialog.this) .setTitle(title) .setCancelable(false) .setPositiveButton("Look!", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //webview.loadUrl(URL_ESTIMATE_USER_HISTORY); //BaseActivity.this.finish(); } }) .setNegativeButton("Close", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }) .setView(layout) .show(); 解決できずに1週間ほどすぎ、現在に至っております。 何かヒントを頂ければ有り難いです。 宜しくお願いします。

    • ベストアンサー
    • Java
  • Javaの質問です(引数の中でメソッド定義?)

    お世話になります。 現在、Javaについて勉強しておりまして一つ不明な点があり投稿させていただきました。 まずはソースをご覧ください。 ---------------------------------------------------------------------------------------------- button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Main.this, Editor.class); TextView textView = (TextView)findViewById(R.id.TextView01); CharSequence text = textView.getText(); intent.putExtra("TEXT", text + "000"); startActivityForResult(intent, SHOW_EDITOR); } }); ---------------------------------------------------------------------------------------------- 上記はサンプルプログラムから一部抜粋したものですが button.setOnClickListenerの引数でnew演算子を使用しており、 尚かつonClickメソッドをオーバーライドして定義しています。 C++開発経験者の私としては非常に見づらいのですが、こんな書き方って結構使うんでしょうか? これがイマイチ理解できていません。 この書き方をネットで探しても見つからなかったため、こちらでご質問させていただきました。 どなたかご教授願います。

  • Android開発(switch文)について

    Android開発(switch文)について Androidアプリ開発における listviewについて質問させてください。 現在、"東京","神奈川","千葉","埼玉","茨城","栃木","群馬" 画面から東京を選び、"新宿","上野","秋葉原"が表示されています。 public void onBackPressed を追加したことにより 一つ前の画面に戻る事ができたのですが このような記述で問題ないでしょうか? もっとシンプルなやり方があれば、アドバイス願います。 また、"新宿","上野","秋葉原"画面から 新宿を選んだ時は、shinjyuku.html 上野を選んだときは、ueno.html 秋葉原を選んだ時は、akihabara.html を表示したいのですが どのタイミングで、switch文を入れれば良いのでしょうか? ■searchActivity.java import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.*; import android.widget.AdapterView.OnItemClickListener; public class searchActivity extends Activity { private ListView list; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); String[] arr = {"東京","神奈川","千葉","埼玉","茨城","栃木","群馬"}; // コンポーネントの設定 list = (ListView)this.findViewById(R.id.list); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list, arr); list.setAdapter(adapter); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { setList2(arg3); } }); } private void setList2(long id) { String[] tokyo = {"新宿","上野","秋葉原"}; String[] kanagawa = {"横浜","桜木町","関内","新横浜"}; String[] chiba = {"千葉","市川","船橋"}; String[] saitama = {"大宮","浦和","蕨","川口・西川口"}; ArrayAdapter<String> adapter = null; switch ((int)id) { case 0: adapter = new ArrayAdapter<String>(this, R.layout.list, tokyo); break; case 1: adapter = new ArrayAdapter<String>(this, R.layout.list, kanagawa); break; case 2: adapter = new ArrayAdapter<String>(this, R.layout.list, chiba); break; case 3: adapter = new ArrayAdapter<String>(this, R.layout.list, saitama); break; } list.setVisibility(View.GONE); ListView list2 = (ListView)this.findViewById(R.id.list2); list2.setAdapter(adapter); list2.setVisibility(View.VISIBLE); } //ここを追加 @Override public void onBackPressed() { //メニュー画面に戻る(start) setContentView(R.layout.main); String[] arr = {"東京","神奈川","千葉","埼玉","茨城","栃木","群馬"}; list = (ListView)this.findViewById(R.id.list); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list, arr); list.setAdapter(adapter); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { setList2(arg3); } }); } } ■main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="20sp" android:background="#ffffff"> <ListView android:id="@+id/list" android:visibility="visible" android:layout_height="fill_parent" android:layout_width="fill_parent"/> <ListView android:id="@+id/list2" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </RelativeLayout> ■list.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="20sp" android:paddingTop="50.0px" android:paddingLeft="20.0px" android:background="#ffffff" android:textColor="#000000" /> 以上、宜しくお願いいたします。