我的Activity上有一个Spinner,当我点击一个项目时,没有任何反应。
此Activity用于搜索电话联系人。它可以在SQLite数据库中按名称或电话号码进行搜索。正如你可以想象的,如果我按名字搜索,select可以找到两个同名的联系人,如果发生了这种情况,我想把选项放在微调控件上,以选择我想要看到的联系人。一旦我选择了cootact,就应该在EditText字段上设置信息,但这并没有发生。
代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buscar);
Conexion=new BaseDeDatos(this, "BDCONTACTOS", null, 1);
alerta = new AlertDialog.Builder(this).create();
if(Conexion==null){
alerta.setMessage("LA conexion NO se ha hecho");
alerta.show();
return;
}
BD = Conexion.getWritableDatabase();
nombreTxt = (EditText)findViewById(R.id.editText1);
telefonoTxt = (EditText)findViewById(R.id.editText2);
direccionTxt = (EditText)findViewById(R.id.editText3);
buscarBtn = (Button)findViewById(R.id.button1);
limpiarBtn = (Button)findViewById(R.id.button2);
miSpinner = (Spinner)findViewById(R.id.spinner1);
adp = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_dropdown_item,arrayList1);
miSpinner.setAdapter(adp);
miSpinner.setDropDownVerticalOffset(50);
miSpinner.setOnItemSelectedListener(this);
buscarBtn.setOnClickListener(this);
limpiarBtn.setOnClickListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
int index = miSpinner.getSelectedItemPosition();
nombreTxt.setText(registros[index][0]);
telefonoTxt.setText(registros[index][1]);
direccionTxt.setText(registros[index][2]);
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
public void onClick(View v) {
if(v==buscarBtn){
BD = Conexion.getWritableDatabase();
if(BD==null){
alerta.setTitle("Mensaje");
alerta.setMessage("La base de datos no está abierta");
alerta.show();
return;
}
nombre= nombreTxt.getText().toString();
telefono=telefonoTxt.getText().toString();
direccion=direccionTxt.getText().toString();
if(nombre.equals("")&&telefono.equals("")){
alerta.setTitle("Mensaje");
alerta.setMessage("Se necesita nombre ó teléfono para buscar");
alerta.show();
return;
}
Cursor c;
if(nombre.equals("")&&!telefono.equals("")){
String telefono=telefonoTxt.getText().toString();
c = BD.rawQuery("SELECT Nombre,Telefono,Direccion FROM MisContactos WHERE Telefono='"+telefono+"'", null);
}
if(telefono.equals("")&&!nombre.equals("")){
String nombre=nombreTxt.getText().toString();
c = BD.rawQuery("SELECT Nombre,Telefono,Direccion FROM MisContactos WHERE Nombre='"+nombre+"'", null);
}else{
String telefono=telefonoTxt.getText().toString();
String nombre=nombreTxt.getText().toString();
c = BD.rawQuery("SELECT Nombre,Telefono,Direccion FROM MisContactos WHERE Nombre='"+nombre+"' and Telefono='"+telefono+"'", null);
}
int count=c.getCount();
alerta.setMessage("Registros encontrados ="+count);
alerta.show();
if(count==0)
return;
registros=new String[count][3];
if (c.moveToFirst()) {
//Recorremos el cursor hasta que no haya más registros
int i=0;
do {
// nombreTxt.setText(c.getString(0));
registros[i][0]=c.getString(0);
//telefonoTxt.setText(c.getString(1));
registros[i][4]=c.getString(1);
//direccionTxt.setText(c.getString(2));
registros[i][5]=c.getString(2);
i++;
} while(c.moveToNext());
}
if(miSpinner.getAdapter().getCount()>0){
arrayList1=new ArrayList<String>();
adp = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_dropdown_item,arrayList1);
miSpinner.setAdapter(adp);
}
for(int j=0;j<count;j++)
arrayList1.add("Ver contacto "+(j+1));
return;
}
if(v==limpiarBtn){
limpiar();
return;
}
}
public void limpiar(){
nombreTxt.setText("");
telefonoTxt.setText("");
direccionTxt.setText("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.buscar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}以下是一些截图:
第一眼看到的活动:

一旦我按名称进行搜索,它就会找到两行:

如果我在微调器中点击一个选项,它应该设置姓名,电话号码和地址(nombre,teléfono,dirección) (但它没有)

这是我第一次在这个网站上提问。谢谢您一直鼓励我。
发布于 2015-06-04 02:36:31
这是在您的Button Click中尝试的简单方法
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();或
当选择某个对象时,微调器应该触发一个"OnItemSelected“事件:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});https://stackoverflow.com/questions/30627790
复制相似问题