英文:
how to open a new activity on spinner dropdown click
问题
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.spinner);
flags = findViewById(R.id.flag);
spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
CountryData.countryNames));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
flags.setImageResource(CountryData.countryflag[spinner.getSelectedItemPosition()]);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
英文:
If someone could help thx.
I have created the spinner part but now whenever I click on the dropdown I need a new activity to open. I was confused where I need to put the if else/ switch case in the Spinner.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.spinner);
flags = findViewById(R.id.flag);
spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
CountryData.countryNames));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
flags.setImageResource(CountryData.countryflag[spinner.getSelectedItemPosition()]);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
答案1
得分: 1
我认为你可以这样做:
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
{
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> activityList = new ArrayList<>(); // list of string which will be added to Spinner
activityList.add("选择活动");
activityList.add("活动 1");
activityList.add("活动 2");
spinner = findViewById(R.id.spinner);
spinner.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item,
activityList
));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
{
Log.i("MyTag", "选择的项目在位置 " + i);
if (i==0) // 检查所选项目的位置并打开活动
{
// 什么都不做
}
else if (i==1)
{
openActivity(NewActivity.class);
}
else
{
openActivity(NewActivitySecond.class);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView)
{
}
});
}
private void openActivity(Class x) // 打开活动的函数
{
Intent intent = new Intent(this, x);
startActivity(intent);
}
}
英文:
I think You can do it like this:
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
{
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> activityList = new ArrayList<>(); // list of string which will be added to Spinner
activityList.add("Select Activity");
activityList.add("Activity 1");
activityList.add("Activity 2");
spinner = findViewById(R.id.spinner);
spinner.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item,
activityList
));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
{
Log.i("MyTag", "Item selected at position " + i);
if (i==0) // check position of selected item and open activity
{
// do nothing
}
else if (i==1)
{
openActivity(NewActivity.class);
}
else
{
openActivity(NewActivitySecond.class);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView)
{
}
});
}
private void openActivity(Class x) //funtion which opens activity
{
Intent intent = new Intent(this, x);
startActivity(intent);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论