• 締切済み

AndroidウィジェットでTextViewを表示

Androidのウィジェットを作っています。基礎をすっ飛ばしてるので分からないのですがサンプルを見ながら作ってます。そこで今突き当たってる壁は 動的にTextViewをウィジェットの上に並べたいんです。動的に作る場合はソースコードで定義するとあったのでアプローチは間違えてないと思いますが、どうしても分かりません。今は TVAry = new TextView[20]; for(int i=0;i<20;i++){ TVAry[i] = new TextView(context); System.out.println( TVAry[i].getId() ); rview.setOnClickPendingIntent(TVAry[i].getId(), pendingIntent); } こんな感じのコードを書いてみましたが動きませんでした。ちなみにこちらのソースコードを参考ベースにして作ってます。 http://www.android-abc.net/category/12739153-1.html あと別件ですが、Eclipseを使っているのですがウォッチ式の追加の仕方が分かりません。英語なので発見できないのかもしれませんが日本語だと監視という項目だとか・・。inspectでしょうか?そういうのは見当たらなかったです。分かる方いらっしゃいましたらこちらもよろしくお願いします。

みんなの回答

  • teketon
  • ベストアンサー率65% (141/215)
回答No.2

idに0とか入るの? そのIDは他と重複していないの? 再起動した? やりとりが面倒なので、回答はコレでやめます。

last-umauma
質問者

お礼

お返事ありがとうございます。 IDに0に入れていいかわかりませんが、一応10000+i*2とかもやってみました。結果は同じでした。重複してないかどうかは調べる方法が分からないので正確には分かりませんが恐らく重複してないと思います。使えるIDの範囲int型からして使ってない領域の方が広く重複する可能性のほうが低いと思うので・・。根本的に間違えてたらすみません。 再起動は端末の再起動でしょうか?エミュレーターで動作確認をしていますが、毎回動作確認は再起動してます。 お忙しい所ありがとうございました。

全文を見る
すると、全ての回答が全文表示されます。
  • teketon
  • ベストアンサー率65% (141/215)
回答No.1

TextViewにIDを設定するんじゃないですか?

参考URL:
http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q11107722216
last-umauma
質問者

お礼

ご回答ありがとうございます。setIDというのがあるんですね。ちょっとやってみたんですがproblem loading widgetというエラーで動かなくなってしまいます。 for(int i=0;i<20;i++){ TVAry[i] = new TextView(context); TVAry[i].setId(i*2); rview.setTextViewText(i*2, "aaa"); } ソースはこんな感じです。何がいけないのでしょう。よろしくお願いします

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • 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
  • 自作アンドロイドアプリで足し算できない

    プログラミングの初心者ですがアンドロイドアプリを作ってみたいと思って練習しています。 練習用のアプリを作りエミュレーターで表示させたいのですがうまくいきません。 プログラムのどこかが間違っているのでしょうか? eclipse上ではエラーのマークなどは出ておらす、エミュレーターもちゃんと起動されますが、 アプリ起動直後にエミュレーター画面内に強制終了の画面が出て計算結果が表示されません。 OS Windows7 64bit --------------------------------- package in.andante.android.CalAppli; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class CalAppliActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); int i1; int i2; int i3; i1 = 1; i2 = 2; i3 = i1+i2; tv.setText(i3); setContentView(i3); } } よろしくお願いします

    • ベストアンサー
    • Java
  • android getFromLocationが?

    Android初心者です。 下記コードでエラーとなり、解決策が見当たらず、非常に困っています。 どなたがご教授をお願いします。 public class MapViewSample extends MapActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapView = (MapView)findViewById(R.id.MapView01); mapView.setClickable(true); mapView.setBuiltInZoomControls(true); ConcreteOverlay overlay = new ConcreteOverlay(this); List<Overlay> overlayList = mapView.getOverlays(); overlayList.add(overlay); } protected boolean isRouteDisplayed() { return false; } private class ConcreteOverlay extends Overlay { GeoPoint mGeoPoint; Geocoder mGeocoder; ConcreteOverlay(Context context) { mGeocoder = new Geocoder(context, Locale.JAPAN); } public boolean onTap(GeoPoint point, MapView mapView) { mGeoPoint = point; try { TextView textView = (TextView)findViewById(R.id.TextView01); boolean success = false; ★ここでエラー⇒ List<Address>addressList = mGeocoder.getFromLocation(point.getLatitudeE6()/1E6, point.getLongitudeE6()/1E6, 5); for (Iterator<Address> it=addressList.iterator(); it.hasNext();) { Address address = it.next(); String country = address.getCountryName(); String admin = address.getAdminArea(); String locality = address.getLocality(); if(country != null && admin != null & locality != null) { textView.setText(country + admin + locality); success = true; break; } } if(!success) textView.setText("Error"); textView.invalidate(); } catch (Exception e){ Toast.makeText(getApplicationContext(), "ErrorToast", 1000).show(); } return super.onTap(point, mapView); } public void draw(Canvas canvas, MapView mapView, boolean shadow) {          ・・・省略・・・ } } }

    • ベストアンサー
    • Java
  • 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 は何処にあるのでしょうか。 アドバイスよろしくお願いします。  

  • Android開発の最初の段階でつまづく

    アンドロイドのアプリ制作を始めようと思い、なんとか環境を揃え、早速チュートリアルサイトを見ながら始めたのですが、早速、最初のHelloWorldでつまづいてしまいました。 以下ソースコードです。 ********************************************************************************** package helloAndroid.PackageName; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloAndroidActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello,Andloid"); setContentView(tv); } } ********************************************************************************** このように書いたのですが、 The left-hand side of an assignment must be a variable というエラーが下から4行目の tv.setText("Hello,Andloid"); のところで出ます。 調べてはいるのですが、このエラーの意味がいまいちわからず困っています。 アドバイスよろしくお願いします。 私自身はVBを少し触ったことのある程度で、Javaは初めてです。

  • Perl/Tkで変数名を使って一気にウィジェットを配置できますか

    Perl/Tkでforやforeachを使って、ウィンドウ上にウィジェットを一括配置したいのです。 my $window = new MainWindow; for (my $i=0; $i<10; ++$i) {$window->Entry->pack;} みたいにして一括作成はできるのですが、作ったウィジェットを後で使うために、これらのウィジェット名に名前をつけながら配置したいです。 上記のコードの場合、後から ent1->get; のように参照できるようにしたいと思っております。 こういったことは可能でしょうか。またその方法があれば手順を示していただけると助かります。 詳しい人には馬鹿にされそうな文ですが、 $ent$i = $window->Entry->pack;では駄目でした。。。

    • ベストアンサー
    • Perl
  • Android開発 Eclipse(超初心者)

    Eclipseの使い方がわかりません。 eclipseをインストールしてプロジェクトを作成しAVDの仮想デバイスを適当に作成し AVD仮想デバイス ターゲット:Android4.0-APILevel14 SDカード:サイズ 66MiB スキン:ビルトイン WVGA800 package com.example.android.hello.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class Hello15 extends Activity{ //表示用のテキストとボタンを宣言 --- private TextView txtCaption; private Button btnHello; public void OnCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); //レイアウトを作成 LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); //テキストを追加 txtCaption = new TextView(this); txtCaption.setText("Click Button!"); layout.addView(txtCaption); //ボタンを追加 btnHello = new Button(this); btnHello.setText("Button"); layout.addView(btnHello); //イベントを設定 btnHello.setOnClickListener(new OnClickListener(){ public void onClick(View v){ txtCaption.setText("Hello, Android!"); } }); } } サンプルで上記のようなソースを作成して 実行したのですが 以前は Android Application Java Applet Java Application など選択肢が出てたのですが仮想デバイスを設定した 辺りからでなくなり実行して少し経つと ←画面に『android』という文字が →画面にキーボードのようなものがでてきて 3分ぐらいまつと←画面が携帯画面に変化します 上記のプログラムではボタンの生成とメッセージがでてくるはずが それらしきものがありません。これらはどこに出てくるのでしょうか? 動かし方すらよくわかってないのでお解説おねがいいたしもうす;

  • [android]画面に円が表示されない原因

    こんにちは。 以下のサイトを参考に白い画面に赤い円を表示させたいのですが、実際にはエミュレータ上に表示されません。 logcatのエラーもありません。原因と対処方法をご教授ください。 eclipseのバージョンは以下のとおりです。 Eclipse Platform 3.8.0.v20120607-071945-9gF7jI7nG5qByXMVdkhRMWBQlF4PnDCLybDCPQ http://www.hakkaku.net/articles/20091127-594 ★MainActivity.java package com.example.test001_001; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 描画クラスを設定 setContentView(new TestDrawView(getApplication())); } /* 描画用クラス */ class TestDrawView extends View { public TestDrawView(Context c) { super(c); } } // このメソッドで描画を行う protected void onDraw(Canvas c) { // --------- (*1)Check!! c.drawColor(Color.WHITE); Paint p = new Paint(); p.setColor(Color.RED); p.setStyle(Paint.Style.FILL); c.drawCircle(100, 100, 50, p); } }

  • Androidプログラミング 文字列の切り取り

    こんにちは。 eclipseを使用してAndroidアプリを作成しているものです。 今回、文字列の切り取りに挑戦しようと思っているのですが、 調べても、 String str = "123456789"; System.out.println(String.format("取り出し前の文字列 : %s", str)); System.out.println("文字列の3文字目から7文字目を取り出す -> " + str.substring(2,7)); System.out.println("文字列の2文字目から2文字目を取り出す -> " + str.substring(1,2)); System.out.println("文字列の3文字目から3文字分を取り出す -> " + str.substring(2,2+3)); といったものしか出てきません。 eclipseではSystem.out.printは使えませんよね? 実行してももちろんなにも表示されません。 私の調べ方が悪いのかもしれませんが、、、 (「Android 文字列 切り取り」といったようにAndroidというワードは入れています。) 如何せん、プログラミング初心者なこともありまして、 これ以上手の施し方がわからない状態です。 Androidプログラミングに詳しい方、優しい方、 参考になるURLやサンプルコードを載せてくださると 助かります。 よろしくお願いいたします。

  • android HashMapについて助言お願いし

    初心者ですが、どうぞ宜しくお願いします。 下記コードでは、マップに 「 map.put( "Key1", "val1" ); map.put( "key2", "val2" ); 」を追加しているのですが、このデータをキーから取り出しテキストビューに表示しようとしています。 このままではエラーはないのですが、何も表示されない状況です。 初心者の無知な質問となりますが、 テキストビューへの表示方法はどのようにすればよいのでしょうか? 何か別の方法でもよいので、ご教授宜しくお願いいたします。 -------------------------------------------------------------------- <?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:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv1"></TextView> </LinearLayout> ------------------------------------------------------------ package com.test; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class TestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //1)HashMapからMapのインスタンスを生成 HashMap<String, String> map = new HashMap<String, String>(); // キーと値のペアを格納 map.put( "Key1", "val1" ); map.put( "key2", "val2" ); // 指定したキーに対応する値を取得. String val = (String)map.get( "key1" ); // テキストビュー取得 TextView tv = (TextView)findViewById(R.id.tv1); tv.setText(val); } }

    • ベストアンサー
    • Java
このQ&Aのポイント
  • 【MFC-J837DWN】に関して、印刷時の異音やガタツキ、罫線や文字のズレ・印刷不良、警告エラーによる印刷不能などの不具合が発生しています。
  • Windows10 64bitで無線LAN接続を使用し、光回線を利用しています。
  • 関連するソフト・アプリについては特に記載がありません。
回答を見る