setOnItemSelectedListener android

时间:2013-11-05 14:13:31

标签: android widget

好吧,我有一个spiner,这是我的微调器

List<String> list = new ArrayList<String>();
list.add("SPBU");
list.add("HOTEL");

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_spinner_item, list);
      dataAdapter
      .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spiner.setAdapter(dataAdapter);

我想创建OnItemSelectedListener方法,因此我可以检测选择是否已更改。 问题是我不知道如何实现,我不明白在方法中使用什么参数(arg0,View arg1,int arg2,long arg3)。这就是我到目前为止的尝试

public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // my code here

            }

2 个答案:

答案 0 :(得分:1)

如果您不知道方法的工作原理,检查文档总是一个好主意。 无论如何,这将有助于你开始;)

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MyActivity extends Activity implements OnItemSelectedListener {

    List<String> list = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        Spinner spinner = (Spinner) findViewById(R.id.spinner1);

        list.add("SPBU");
        list.add("HOTEL");

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);
        spinner.setOnItemSelectedListener(this);
    }


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


    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
        Toast.makeText(getApplicationContext(), "Clicked on: " + list.get(position), Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        //do something
    }

}

编辑: 您还可以创建一个匿名类来处理事件:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

            }

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

            }
        });

答案 1 :(得分:1)

你需要为微调器添加一个监听器,它将调用&#34; onItemSelected&#34;选择项目时的方法。这是一些示例代码:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
     @Override
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
         //stuff here to handle item selection      
     }

     @Override
     public void onNothingSelected(AdapterView<?> parent) {

     }
});