2014-02-25 30 views
0

這裏是代碼: 幫助我們,我只是新來的。非常感謝!我很難找到解決方案..非常感謝,非常感謝。 它應該鏈接到相同的XML佈局。我有一個動態的微調,如果我點擊微調項目,我想鏈接到另一個佈局

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    db = new DatabaseHelper(MainActivity.this); 

    edittext = (EditText) findViewById(R.id.editText1); 
    btnAdd = (Button) findViewById(R.id.btnadd); 
    spinner = (Spinner) findViewById(R.id.spinner1); 

    btnAdd.setOnClickListener(this); 
    spinner.setOnItemSelectedListener(this); 


    /* when spinner item is clicked it should linked to another layout.. it should link in add_student layout 

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3) { 
       String str = (String) arg0.getSelectedItem(); 

          //here print selected value... 
       System.out.println("String is :: " + str); 

          //And StartActivity here... 

        Intent intent = new Intent(MainActivity.this,StudentActivity.class); 
        startActivity(intent); 

      } 

      @Override 
      public void onNothingSelected(AdapterView<?> arg0) { 
       // TODO Auto-generated method stub 

      } 
     }); 
    */ 

} 


} 
+0

你的概率是多少? –

+0

在這裏告訴你的問題。你需要什麼? – Piyush

+1

轉到[this](http://stackoverflow.com/questions/19948487/android-how-to-get-value-from-spinner-and-based-on-value-to-redirect-to-anothe/19948697 #19948697)頁面。 它可能會幫助你.. –

回答

0

如果你想添加自定義佈局,微調比你應該使用這個..

ArrayAdapter<String> adapter = new ArrayAdapter<String>(YourActivityName.this, R.layout.yourcusomlayout, urarraylistValue); 
    spinner.setAdapter(adapter); 

如果你要離開一個活動到另一個使用這種微調上

項目點擊
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
       String str = spinner.getItemAtPosition(arg2).toString(); 
       Intent intent = new Intent(MainActivity.this,StudentActivity.class); 
       intent.putExtra("myvalue" , str); 
       startActivity(intent); 

     } 


     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 

現在在您的StudentActivity中按如下方式檢索oncreate()方法上的數據。

Intent n = getIntent(); 

    String myData = n.getStringExtra("myvalue"); 

現在,您可以在選定的微調器中看到您的值,您可以在TextView中設置該微調器或在日誌中打印。

+0

非常感謝您的回答..嗯我應該在哪裏分配「myData」謝謝! – user3349819

+0

你可以將它分配給TextView,EditText或Button,否則在日誌中打印。 – Piyush

+0

謝謝! @PiyushGupta「myData」的功能是什麼? – user3349819

1

嘗試: 下面將爲你所選擇的項目位置

spinner.getSelectedItemPosition(); 

以下爲聽衆:

public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) { 
      int item = spinner.getSelectedItemPosition(); 
       //Put if else or switch case here to start an appropriate intent 


     } 

Official Docs

+0

嘿,請介意評論爲什麼這是downvoted? – Skynet

+0

謝謝@NunChai的答案^^ – user3349819

0

你想要什麼?您添加其他佈局或其他活動。

如果添加布局,請嘗試下面的代碼。

ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.yourlayout, yourvalues); 
spinner.setAdapter(ad); 
+0

我會試試這個,謝謝@karthik – user3349819

相關問題