2017-11-30 10 views
0

アプリケーション内で一定のApp:私は適用必要、私はクラスの数を最小限にするためにアプリケーションの定数とアンドロイドのアプリケーションに</p> <p>例ListViewコントロールとメニューオプションを作成し

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     context=this; 
     lv=(ListView) findViewById(R.id.listView); 
     lv.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,prgmNameList)); 
     lv.setOnItemClickListener(this); 
    } 

OnClickListener

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     switch (position) 
     { 
      case 0: 
       Intent intent = new Intent(this, First.class); 
       intent.putExtra(AppConstants.MAIN_ACTIVITY_TAG,lv.getItemAtPosition(position).toString()); 
       startActivity(intent); 
       break; 

     } 

しかしをアプリ定数をメニューオプションにも割り当てます しかし、私はそれを行う方法を知らない:

ここでは10

は、メニューオプションのためのコード

// create action options for options 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.news, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 

     int id = item.getItemId(); 
     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_about) { 
      Intent intent = new Intent(this,Extras.class); 
      startActivity(intent); 
     } 
     if (id == R.id.action_advertise) { 
      return true; 
     } 
     if (id == R.id.action_contact) { 
      return true; 
     } 
     if (id == R.id.action_help) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

私がリストビューとしてではなく、メニューオプションのために同じことをやりたいです。
この問題を解決するのを手伝ってください。

+0

使用string.xmlをします。 – ADM

+0

詳細を教えてください、私は混乱しています –

+0

正確に何の問題がありますか? xmlで文字列定数を使用しますか? – ADM

答えて

0

文字列をstring.xmlに挿入するだけです。

<string name="menu_name">Show result</string> 

あなたはxmlで直接アクセスします。

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".ContainerActivity"> 

<item 
    android:id="@+id/menu_action_show" 
    android:layout_width="wrap_content" 
    android:icon="@drawable/youricon" 
    android:title="@string/menu_name" 
    app:showAsAction="always"/> 

また、コンテキストにJavaコードを使用して、すべてのリソースにアクセスすることができます

String menu_name=getString(R.id.menu_name); 
+0

大変お世話になりました –

関連する問題