• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:Android開発(switch文)について)

Android開発(switch文)について

foofoo0の回答

  • foofoo0
  • ベストアンサー率42% (39/92)
回答No.1

onBackPressedメソッドで、最初の画面を再構築していますが、 list1は非表示になっているだけなので、それを再表示して やればよいです。 onBackPressedメソッドで、super.onBackPressed()を 呼び出さないと、戻るボタンでアプリを終了することが できなくなります。 ただ、これを呼んでしまうと、list2から戻るときにも 終了してしまうので、今表示されているリストがどちら なのかで、呼ぶか呼ばないかを制御する必要があります。 ザックリとですが、こんな感じになります。 private ListView list2; private void setList2(long id) { ~省略~ list2 = (ListView)this.findViewById(R.id.list2); list2.setAdapter(adapter); list2.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { jumpPage(arg3); } }); list2.setVisibility(View.VISIBLE); } private void jumpPage(long id) { switch ((int)id) { case 0: // List2の1つ目のページへ飛ぶ break; case 1: // List2の2つ目のページへ飛ぶ break; case 2: // List2の3つ目のページへ飛ぶ break; } } @Override public void onBackPressed() { // 表示中のlistによって処理を分ける必要があります // list2が表示されているときの処理 list2.setVisibility(View.GONE); list.setVisibility(View.VISIBLE); // list1が表示されているときの処理 super.onBackPressed(); } list2が押された時にjumpPageメソッドが呼ばれるように して、それぞれに対応したhtmlを表示する処理を追加すれば よいです。 ただし、list1でどれを選択されたかによっても変わってくる ものなので、list2のデータ構造を含めて、考えていかないと 全都道府県に対応しようとすると大変なことになると思いますよ。

regal38
質問者

お礼

いつも、的確なアドバイス ありがとうございます。 ↓こちらだと、どちらも同じページに飛んでしました。 東京→新宿→1つ目のページ(URL) 神奈川→横浜→1つ目のページ(URL) jumpPageメソッドを都道府県分、設置するということでしょうか? private ListView list2; private void setList2(long id) { ~省略~ list2 = (ListView)this.findViewById(R.id.list2); list2.setAdapter(adapter); list2.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { jumpPage(arg3); } }); list2.setVisibility(View.VISIBLE); } private void jumpPage(long id) { switch ((int)id) { case 0: // List2の1つ目のページへ飛ぶ break; case 1: // List2の2つ目のページへ飛ぶ break; case 2: // List2の3つ目のページへ飛ぶ break; } }

関連するQ&A

  • Android開発(Listview)について

    Androidアプリ開発における listviewについて質問させてください。 以下で、東京都,神奈川県,千葉県,埼玉県,茨城県,栃木県,群馬県と表示されています。 東京をクリックしたら、新宿、上野、秋葉原 千葉をクリックしたら、千葉、市川、船橋 といったように、さらに地域を絞っていきたいと考えています。 どのような方法で、実現できるか アドバイスして頂けると助かります。 ■searchActivity.java import android.app.Activity; import android.os.Bundle; import android.widget.*; 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); } } ■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" android:textSize="20sp" android:background="#ffffff" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> ■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" /> 以上、宜しくお願いいたします。

  • Androidアプリ Listviewが

    アドバイスください。初心者です。Listviewを作りたいだけなんです。”データがありません”と表示されてしまいます・・。 ●layout(.xml) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/android:empty" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="データが存在しません"/> </LinearLayout> ●listview(.java) import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; //ListActivityを継承 import com.example.yamato.myapplication001.R; public class listview extends ListActivity { private String[] items = { "a10", "a11", "a21", "b10" }; //(2) // private String[] items = {};(2) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //リストビューを含むレイアウト指定(1) setContentView(R.layout.activity_main_activity001); // ArrayAdapterオブジェクトの生成 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); // Adapterの指定 setListAdapter(adapter); // ListActivity内部にあるListViewオブジェクトを取得する場合 ListView listView = getListView(); // ListViewオブジェクトを取得したので以下のメソッド等が利用できる // 選択する要素の位置の指定 listView.setSelection(3); } } ソースは参考本からです。

  • 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アプリ開発について

    androidアプリ開発初心者です。 やりたいことは、 スタートボタンを押して、自分のお気に入りのページに飛び、(例えばオークションなど)サイトを表示させ、何分間ごとに更新できるループ処理?などができればいいなと考えています。 スタートボタンを押してサイトを表示するまでは何とか作れたのですが、それ以降をどのように作ったらいいのか分かりません。 初心者的な質問で申し訳ないのですが、どのように書いたらよいのか教えてください。 コードはこのようになっています。 <<main.xml>> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="192dp" android:layout_height="wrap_content" android:text="スタート" /> </LinearLayout> <<MainActivity.java>> import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button btn =null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Uri uri =Uri.parse("http://"); Intent intent=new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 長文で申し訳ありません。どうぞ宜しくお願い致します。

  • androidのintentの使い方

    // //問題:LISTを押すと、エラーが出る、Intentの問題ですか? // public class FirstActivity extends Activity { // onCreateメソッド(画面初期表示イベントハンドラ) private static final String TAG ="[HelloWorld]"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);// スーパークラスのonCreateメソッド呼び出し setContentView(R.layout.firstlayout);// レイアウト設定ファイルの指定 ListView listview = (ListView) findViewById(R.id.fruitlist); // ListViewオブジェクト取得 listview.setOnItemClickListener(new ListItemClickListener()); // ListViewオブジェクトにクリックリスナーを関連付け } // アイテムクリックリスナー定義 class ListItemClickListener implements OnItemClickListener { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {// onItemClickメソッド(値選択時イベントハンドラ) ListView listview = (ListView) parent; // ListViewオブジェクト取得 String item = (String) listview.getItemAtPosition(position);// 選択された値取得 Log.d(item, TAG); Intent intent = new Intent(FirstActivity.this,SecondActivity.class);// インテントの生成(呼び出すクラスの指定) startActivity(intent);// 次のアクティビティの起動 } } } -------------------------------------------------------------------------- public class SecondActivity extends Activity { // onCreateメソッド(画面初期表示イベントハンドラ) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // スーパークラスのonCreateメソッド呼び出し setContentView(R.layout.main);// レイアウト設定ファイルの指定 } }

    • ベストアンサー
    • Java
  • 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
  • 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
  • setOnItemSelectedListener

    JavaでAndroidアプリの学習をしています。以下コードが理解できず困っています。 aListView.setOnItemSelectedListener(   new AdapterView.OnItemSelectedListener() {    @Override    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {      ListView listView = (ListView)parent;      String s = (String)listView.getSelectedItem();      aTextView.setText(String.format("%s Selected", s));    }    public void onNothingSelected(AdapterView<?> parent) {    }   } ); リストボックスの選択時にsetOnItemSelectedListenerが呼ばれると思うのですが、 呼ばれた際の()にOnItemSelectedListenerメソッドがあり、中にOverrideメソッドが2つあります。 setOnItemSelectedListener(   new AdapterView.OnItemSelectedListener() { @Override×2 } ) のnew AdapterView.OnItemSelectedListener() の構文の解釈方法を教えてください。 可能でしたら、全文教えてもらえると助かります。 また、メソッド内でnewしてメソッド呼び出しをなんと呼ぶのか解りませんが、参考サイト等ありましたらURLをお願いします。 それでは、アドバイスよろしくお願いいたします。

    • ベストアンサー
    • Java
  • R.javaが自動生成されない

    Androidアプリ開発をEclipseでしていますがR.javaが自動生成されず、 ビルド時にエラーが出てしまいます。 コンパイル時に「Rを変数に解決できません」の主旨のエラーが出ています。 他サイトでの対処方法を参考に以下方法を実行しましたが、 R.javaは自動生成されずに半ばあきらめぎみです… 以下試した方法 --------------------------------------------------------- ○Eclipseのプロジェクトスクリーンを実行する ○「プロジェクト」→「自動的にビルド」にチェックが入っている ○layoutフォルダのXMLの文法エラーはない ○Eclipseの再起動 ○プロジェクトのクリーン ○xmlファイル名はactivity_mainであり大文字はつかっていない ○「ウィンドウ」を選択し「設定」→「Java」→「コンパイラー」 でコンパイラー準拠レベルを変更 --------------------------------------------------------- 以下activity_main.xml ---------------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textSize="12pt" /> <Button android:id="@+id/button01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button01" /> </LinearLayout> ---------------------------------------------------------------------- 以下string.xml ---------------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Botan</string> <string name="hello">ボタンを押してね。</string> <string name="button01">クリック</string> </resources> ---------------------------------------------------------------------- 以下MainActivity.java ---------------------------------------------------------------------- package com.example.botan; import android.os.Bundle; import android.app.Activity; import android.view.*; import android.view.View.OnClickListener; import android.widget.*; public class MainActivity extends Activity { private TextView text1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text1 = (TextView)this.findViewById(R.id.TextView); Button btn1 = (Button)this.findViewById(R.id.burron01); btn1.setOnClickListener(new MyClickAdapter()); } class MyClickAdapter implements OnClickListener { public void onClick(View view){ text1.setText("クリックしました!"); } } }----------------------------------------------------------------------------------- どなたか他にチェックする箇所、 もしくは上記で不適切な記述があることが分かるかた ご教示下さい。 よろしくお願い申し上げます。 補足 以下参考サイト(ソース等はコピペしたので同じはずです) ▼Androidプログラミング日記 http://androidn.hannnari.com/link7-botan.html

    • ベストアンサー
    • Java
  • Android / 型変数へのバインド?

    下記のようなコードがあるのですが、意味が分からないので教えてください arrayAdapter = new ArrayAdapter<Hoge>(this,R.layout.listview); listView.setAdapter(arrayAdapter); 1.< >に入れる内容は? ・ここにはStringなどの型を入れると思っていたのですが、このコードではユーザー定義したHogeクラスを入れています ・< >に入れて良い内容は、どういう風に決められているのでしょうか? ・また、この場合、どういう意味になるのでしょうか? ■Hogeクラス内容 ・プロパティ … データベーステーブルやカラム名を定数宣言 ・メソッド … レコード取得する(?)セッター、ゲッター ・リストビュー … 上記メソッドを使ったリストビュー