のプロパティを持つファイルxml
があり、最初にロードされたリストビューで正常に機能します。これをクリックすると、のすべての行にあり、メソッドが実行されます。 ImageView
android:onClick="playSound"
Activity
ImageView
listview
playSound
問題は、この上部に、にロードされたコンテンツActivity
をフィルタリングしたり、要素/行をフィルタリングしたり、たとえば名前で検索したりすることです(辞書のように)。何かを書いたときに要素が少ない場合、同じ行をクリックすると、強制終了の例外が表示されます。EditText
listview
listview
ImageView
NoSuchMethodException: playSound
の中onClick
にメソッドが見つからないようですが、そこにあります。 playSound
Activity
ザActivity
public class ListaCaracteres extends Activity{
private Context context;
private Cursor c;
private SQLiteDatabase hanyuDB;
private HanyuSQLHelper hanyuDBHelper;
private AdapterListaCaracteres adapter;
private ListView listaCaracteres;
private EditText buscador;
private ImageView play;
private MediaPlayer mediaPlayer;
private String texto = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(es.hsk.ap.R.layout.lista_caracteres);
/*
* Query a la base de datos, obtenemos todos los lugares en Cursor
*/
try{
hanyuDBHelper = new HanyuSQLHelper(this);
hanyuDB = hanyuDBHelper.getReadableDatabase();
c = hanyuDB.rawQuery(" SELECT * FROM Caracteres ", null);
}
catch(SQLiteException ex)
{
Toast mensajeError=Toast.makeText(getApplicationContext(), "Error accediendo a los datos", Toast.LENGTH_SHORT);
mensajeError.show();
Log.e("LogLugares", "Error en getReadableDatabase()", ex);
}
/*
* Utilizamos nuestro Adapter AdapterListaCaracteres con el contenido del Cursor
*/
adapter = new AdapterListaCaracteres(this,c, true);
listaCaracteres = (ListView)findViewById(es.hsk.ap.R.id.listaCaracteres);
listaCaracteres.setAdapter(adapter);
/*
* Localizamos y damos funciona al buscador
*/
buscador = (EditText)findViewById(R.id.buscador);
buscador.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
texto = buscador.getText().toString();
c = hanyuDB.rawQuery(
" SELECT * FROM Caracteres WHERE significado LIKE '%"
+ texto + "%'", null);
AdapterListaCaracteres adapter2 = new AdapterListaCaracteres(getApplicationContext(), c, false);
listaCaracteres.setAdapter(adapter2);
return false;
}
});
/*
* Ponemos acción al clic sobre un elemento del listview
*/
listaCaracteres.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
/*
* Al hacer clic se inicia activity MostrarCaracter
* El Intent enviado lleva como parámetro el id del objeto a mostrar
*/
if(texto!=null){
c = hanyuDB.rawQuery(
" SELECT * FROM Caracteres WHERE significado LIKE '%"
+ texto + "%'", null);
c.moveToPosition(position);
position=c.getInt(0)-1;
}
Intent intent=new Intent();
intent.setClass(getApplicationContext(), MostrarCaracter.class);
intent.putExtra("idObjeto", position);
startActivity(intent);
}
});
}
/*
* Método para reproducir sonido de carácter
*/
public void playSound(View view){
int posicion=listaCaracteres.getPositionForView(view);
if(texto!=null){
c = hanyuDB.rawQuery(
" SELECT * FROM Caracteres WHERE significado LIKE '%"
+ texto + "%'", null);
c.moveToPosition(posicion);
posicion=c.getInt(0)-1;
}
c = hanyuDB.rawQuery(" SELECT audio FROM Caracteres WHERE _id="+(posicion+1), null);
c.moveToFirst();
String audio=c.getString(0);
int resID = getResources().getIdentifier(audio, "raw", "es.hsk.ap");
mediaPlayer = MediaPlayer.create(this, resID);
mediaPlayer.start();
}
アダプター内のxml
膨張したファイル。ImageView (id play)
クリック可能でonClick
アクション が必要です。
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp" >
<TextView
android:id="@+id/caracter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="5dp"
android:paddingRight="15dp"
android:paddingBottom="5dp"
android:textSize="28dp" />
<TextView
android:id="@+id/pinyin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dp" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="right" >
<ImageView
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/icono"
android:paddingRight="7dp"
android:onClick="playSound"
android:src="@drawable/speaker" />
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp" >
<TextView
android:id="@+id/significado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textSize="16dp" />
</TableRow>
Logcat:
04-04 21:36:07.144: E/AndroidRuntime(5569): Uncaught handler: thread main exiting due to uncaught exception
04-04 21:36:07.164: E/AndroidRuntime(5569): java.lang.IllegalStateException: Could not find a method reproducir(View) in the activity
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.View$1.onClick(View.java:2020)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.View.performClick(View.java:2364)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.View.onTouchEvent(View.java:4179)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.View.dispatchTouchEvent(View.java:3709)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
04-04 21:36:07.164: E/AndroidRuntime(5569): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
04-04 21:36:07.164: E/AndroidRuntime(5569): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.os.Looper.loop(Looper.java:123)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-04 21:36:07.164: E/AndroidRuntime(5569): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 21:36:07.164: E/AndroidRuntime(5569): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 21:36:07.164: E/AndroidRuntime(5569): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-04 21:36:07.164: E/AndroidRuntime(5569): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-04 21:36:07.164: E/AndroidRuntime(5569): at dalvik.system.NativeStart.main(Native Method)
04-04 21:36:07.164: E/AndroidRuntime(5569): Caused by: java.lang.NoSuchMethodException: reproducir
04-04 21:36:07.164: E/AndroidRuntime(5569): at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
04-04 21:36:07.164: E/AndroidRuntime(5569): at java.lang.Class.getMethod(Class.java:1014)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.View$1.onClick(View.java:2017)
私のカスタムアダプター:
public class AdapterListaCaracteres extends CursorAdapter{
private Context mContext;
private Cursor datos;
private boolean aRequery;
private LayoutInflater inflater;
/*
* Constructor de la clase
*/
public AdapterListaCaracteres(Context context, Cursor c, boolean autoRequery){
super(context, c, autoRequery);
this.mContext = context;
this.datos = c;
this.aRequery=autoRequery;
}
public void reproducir(View view){
}
/*
* Métodos
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
return datos.getCount();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datos.getString(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View item = convertView;
ViewHolder holder;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
datos.moveToPosition(position);
//Comprobamos si el item existe para reaprovecharlo
if(item==null){
try{
item = inflater.inflate(es.hsk.ap.R.layout.caracter, null);
}
catch(InflateException ex)
{
// lo que querais hacer en este caso,mostrar un toast o lo que sea
}
holder = new ViewHolder();
holder.caracter = (TextView)item.findViewById(es.hsk.ap.R.id.caracter);
holder.pinyin = (TextView)item.findViewById(es.hsk.ap.R.id.pinyin);
holder.significado = (TextView)item.findViewById(es.hsk.ap.R.id.significado);
holder.audio = (ImageView)item.findViewById(es.hsk.ap.R.id.play);
item.setTag(holder);
}
else{
holder = (ViewHolder)item.getTag();
}
holder.caracter.setText(datos.getString(2));
holder.pinyin.setText(datos.getString(3));
holder.significado.setText(datos.getString(4));
return item;
}
/*
* Utilizamos clase ViewHolder para obtener IDs de objetos View inflados anteriormente
* (objetos hijos del objeto convertView) y así reparovecharlos para ahorrar recursos
* y batería al hacer scroll. Se localizan mediante la propiedad Tag
*/
static class ViewHolder{
TextView caracter;
TextView pinyin;
TextView significado;
ImageView audio;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}
}