我的代码中有一个微调器,我想在选中时获取数组中元素的位置。这是我的代码运行完美。在选择中我存储了元素的字符串值,但我也想要元素的位置计数
public class MainActivity extends Activity implements OnItemSelectedListener {
final Context context = this;
private Button button;
private String selection;
private String[] states = new String[]{
"Gujrat","Jammu and Kashmir","Kerala","Karnataka","Lakshadweep","Maharashtra","Manipur","Mizoram",
"Nagaland","New Delhi","Rajasthan","Tami Nadu","West Bengal"
};
ArrayList<String> categoryList = new ArrayList<String>();
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int currentMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
//make it fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//fix portrait orientation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.menu);
Button dialogButton = (Button) dialog.findViewById(R.id.btncross);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Spinner spin = (Spinner)dialog.findViewById(R.id.spinState);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, states);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter_state);
spin.setOnItemSelectedListener(MainActivity.this);
}
});
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView tv = (TextView)arg1;
selection = tv.getText().toString();
Log.v(TAG, "index=" + selection);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:2)
在代码中:
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView tv = (TextView)arg1;
selection = tv.getText().toString();
Log.v(TAG, "index=" + selection);
}
arg2 是微调器的位置
答案 1 :(得分:1)
在setOnItemSelectedListener(new OnItemSelectedListener()
public abstract void onItemSelected (AdapterView<?> parent, View view, int position, long id)
<强>参数强>
parent 发生选择的AdapterView
查看已点击的AdapterView中的视图
位置视图在适配器中的位置
id 所选项目的行ID
答案 2 :(得分:1)
int arg2 of `onItemSelected()` is the position of selected items.
所以你可以像
一样使用它@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selection = states[arg2];
Log.v(TAG, "index=" + arg2);
Log.v(TAG, "selction=" + selection);
}
答案 3 :(得分:1)
spin.getSelectedItemPosition();
答案 4 :(得分:0)
int arg2
获取所选商品ID
仅更新此方法,
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.v(TAG, "index=" + spin.getItemIdAtPosition(int2));
}