2
我在Activity
上有Spinner
,当我点击一个项目时什么也没有发生。onItemSelectedListener不起作用
这个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);
}
一些截图:
一见钟情的活动:
一次我按名称搜索,就发现两行:
它是假设设定的姓名,电话号码和地址(农布雷,teléfono,dirección)如果我点击来自微调选项(但没有)
这是第一次我有时间在这个网站上提问。感谢您的支持。
( )检查v.getID()== R.id. button1 – 3xplore
我已经做了调整,但我认为这不是问题。按钮正常工作,问题是当我点击一个微调项目什么也没有发生:c它是假设设置EditText字段上的信息。 – Gabriela