[英]How to call activity from the items of the spinner
I have a page which consist of a spinner and a submit button. 我有一个页面,其中包含一个微调框和一个提交按钮。 What I want to achieve is when user selects an item in the list and click on submit, it should take him to an other layout having a webview.
我要实现的是,当用户在列表中选择一个项目并单击“提交”时,它将带他进入具有Webview的其他布局。 Each item in the spinner should open different .html page in the layout.
微调器中的每个项目都应在布局中打开不同的.html页面。
What I have now is the item is being selected from the spinner, but I'm not sure how to perform onclick listener to it... 我现在所拥有的是正在从微调器中选择的项目,但是我不确定如何对其执行onclick侦听器...
code for main activity.java is: 主要activity.java的代码是:
public class beef extends Activity {
private Spinner spinner1;
private ImageButton btnSubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beef);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
//get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnSubmit = (ImageButton) findViewById(R.id.imageButton1);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(v.getId() == btnSubmit.getId())
{
Intent intent = new Intent(beef.this,display.class);
intent.putExtra("urlpath", "animalbites.html");
startActivity(intent);
}
}
});
}
} }
code of CustomOnItemSelectedListener.java is: CustomOnItemSelectedListener.java的代码是:
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if (arg2 == 0) // First item selected
{
//Here I need to give an id for the .html file
}
else if (arg2 == 1) // Second
{
//Here I need to give an id for the .html file
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
and the display.java is: 而display.java是:
public class display extends Activity implements OnClickListener {
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent intent=getIntent();
String mUrl=intent.getStringExtra("urlpath");
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/www/"+mUrl);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
i am not exactly getting your problem, but i ll answer according to what i got: 我并没有完全解决您的问题,但是我会根据得到的回答:
Why are you concerned of adding an onClickListener to the spinner? 为什么要担心向旋转器添加onClickListener?
Just add onClickListener to the button and on the click of that button do: 只需将onClickListener添加到按钮上,然后单击该按钮即可:
String selecteditemName=(String)spinner1.getSelectedItem();
//spinner1 having items : "file1" , "file2"
Intent i=new Intent(this, yournewActivity.class);
i.putExtra("selected_item", selecteditemName);
startActivity(i);
Now in that activity (yournewActivity) containing the webview use this code in onCreate() method: 现在,在包含网络视图的活动(yournewActivity)中,在onCreate()方法中使用以下代码:
String item;
Intent i=getIntent();
item = i.getStringExtra("selected_item");
Now you have your spinner selection in the String 'item', you can now do whatever you want: 现在,您可以在String'item'中选择微调器,现在您可以执行任何操作:
My point is that : You should use your spinner selection as ID for populating the webview. 我的观点是:您应该使用微调框选项作为ID来填充Web视图。
If you still want to use any IDs other than the ones present in the spinner then make an extra java class Idgetter, 如果您仍然想使用微调器中存在的ID以外的其他ID,请制作一个额外的Java类Idgetter,
class idgetter
{
public static String getID(String name)
{
if(name.equals("file1"))
return "requiredfile1.html";
else if(name.equals("file2"))
return "requiredfile2.html";
}
}
Now, Do exactly as the above code says and after you have the spinner selection in the variable item , use: 现在,完全按照上面的代码所述进行操作,并在变量item中选择了微调框之后,使用:
String id=idgetter.getID(item); //in younewActivity
You are finally done. 您终于完成了。
By using Intent's putExtra method. 通过使用Intent的putExtra方法。
Intent mIntent = new Intent(this, display.class);
mIntent.putExtra("urlpath", "put value in here");
startActivity(mIntent);
Populate the spinner with number of html pages and Use spinner.getSelectedItem().toString() inside the addListenerOnButton() method. 在spinner中填充html页面数,并在addListenerOnButton()方法内使用spinner.getSelectedItem()。toString()。 By doing do, you will get the selected html page and pass it using intent to the next layout.
这样做,您将获得所选的html页面,并使用intent将其传递到下一个布局。
CODE: 码:
`public class beef extends Activity {
公共类牛肉扩展了活动{
private Spinner spinner1;
私人Spinner spinner1; private ImageButton btnSubmit;
私人ImageButton btnSubmit;
int final websiteA = 1;
int最终网站A = 1; int final websiteB = 2;
int最终网站B = 2; int final websiteC = 3;
int最终网站C = 3;
String selectedHtmlPage = "";
字符串selectedHtmlPage =“”;
.........
.........
.........
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
switch(arg2)
{
case websiteA :
selectedHtmlPage = "websiteA.html";
break;
case websiteB :
selectedHtmlPage = "websiteB.html";
break;
case websiteC :
selectedHtmlPage = "websiteC.html";
break;
}
}
public void addListenerOnButton()
{
btnSubmit = (ImageButton) findViewById(R.id.imageButton1);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
if(v.getId() == btnSubmit.getId())
{
Intent intent = new Intent(beef.this,display.class);
intent.putExtra("urlpath", selectedHtmlPage);
startActivity(intent);
}
}
});
}
}` }`
Solved it, its simple. 解决了它,很简单。 Here is what I did:
这是我所做的:
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(this);
}
- - -
@Override
public void onClick(View v) {
if(v.getId() == btnSubmit.getId())
{
Intent intent = new Intent(beef.this,display.class);
intent.putExtra("urlpath", mLink);
startActivity(intent);
}
}
});
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(arg2==0){
mLink="Beef html/BBQ_Meatballs_Recipes.html";
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.