2015-12-31 102 views
0

我试图创建我的第一个Android应用程序,但我不能做我想做的事。 我的想法是创建一个简单的分数计数器。我有一个listview的活动。列表视图的每个项目都有一个带名称的TextView,两个用于添加和减去的按钮以及一个用于分数的TextView。Android自定义列表视图适配器和numberpicker dialogfragment

我的想法是当用户点击分数时选择一个数字来增加减少,以显示一个custum numberpicker。自定义的numberpicker有一个接口来使用回调方法来获得选择的号码。

在activity类中,我有一个用于listview的自定义适配器,以及一个用于改进功能的viewholder类。我有几个疑问/问题:

1)我什么时候可以定义按钮/文本浏览器的监听器?目前,我在视图中有侦听器,但我不确定它是否是更好的解决方案,也许最好在适配器中定义它们,或者为listview创建一个onitemclicklistener。

2)我可以实现在适配器或视图持有者类中的numberpicker对话框接口中定义的方法吗?我已经尝试过,但我得到一个演员例外,因为内部类不是活动...

3)如果我在活动(外部)类中实现接口方法,我无法修改调用数字选择器的所需文本视图...

我的两个主要问题是在哪里以及如何为listview行中的每个视图定义侦听器,以及如何获取numberpicker对话框的数量并继续执行......

任何人都可以帮助我吗?

谢谢。

+1

1)您在适配器中定义onClick侦听器。 2)发布您的代码。 3)发布您的代码。 –

回答

0

要在适配器内的视图中使用侦听器,应该在适配器中声明侦听器,并且应该为每个视图创建一个新的侦听器实例。

private static class ViewHolder { 
    private CustomListener listener; 
    private Button incrementBtn; 
} 

现在在getView()方法:

holder.listener = new CustomListener(position); 
holder.incrementBtn.setOnClickListener(holder.listener); 

现在定义继承的类来实现onClick()方法:

private class CustomListener implements View.OnClickListener { 
    private int position; 

    protected CustomListener(int position) { 
     this.position = position; 
    } 

    @Override 
    public void onClick(View v) { 
     //increment score here 
     notifyDataSetChanged(); 
    } 
} 

一旦onClick()方法经由notifyDataSetChanged()完成后,用更新的视图与新的比分。 要使用数字选取器对话框,它与在视图中定义的DialogInterface.OnClickListener的模式相同。

0

当然,对不起......

package com.example.cdp.mispartidas; 

imports... 

public class Tanteo extends ActionBarActivity { 

private String identificador; 
private Partida partida; 
private Backup backup; 
private int indice; 
private static Context context; 

ListView listviewjugadores; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tanteo); 

    // Parametros 
    listviewjugadores = (ListView) findViewById(R.id.jugadorestanteo); 

    Tanteo.context = getApplicationContext(); 

    Log.i("MILOG", "Obtenemos el backup"); 
    backup = Backup.getMiBackup(getApplicationContext()); 

    // Obtenemos el numero de jugadores 
    Bundle bundle = getIntent().getExtras(); 
    identificador = bundle.getString("idpartida"); 

    Log.i("MILOG", "El identificador de la partida es " + identificador); 


    // Buscamos la partida 
    indice = backup.getPartida(identificador); 
    if (indice >= 0) { 
     partida = backup.getBackup().get(indice); 
     // Establecemos el adaptador 
     Log.i("MILOG", "Establecemos el adaptador"); 
     AdaptadorTanteo adaptador = new AdaptadorTanteo(this, getTaskId(), partida.getJugadores()); 
     listviewjugadores.setAdapter(adaptador); 
    } else { 
     Toast.makeText(this, "No se ha encontrado la partida " + identificador, Toast.LENGTH_SHORT).show(); 
    } 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_tanteo, 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(); 

    int numjugadores; 

    switch(id){ 
     // Anadimos un nuevo jugador a la partida 
     case R.id.addjugador: 
      numjugadores = listviewjugadores.getAdapter().getCount(); 
      Jugador player = new Jugador(); 
      // Ponemos un nombre por defecto 
      player.setNombre("Jugador" + String.valueOf(numjugadores + 1)); 
      player.setNumerojugador(numjugadores + 1); 
      // Anadimos la puntuacion 
      player.setPuntuacion(0); 
      // Anadimos el jugador a la lista 
      partida.addJugador(player); 
      // Actualizamos el backup 
      backup.getBackup().set(indice, partida); 
      // Almacenamos 
      Log.i("MILOG", "Guardamos el backup"); 
      backup.guardarBackup(); 
      // Si todo ha ido bien, acutalizamos la lista de jugadores 
      ((AdaptadorTanteo) listviewjugadores.getAdapter()).notifyDataSetChanged(); 
      break; 

     case R.id.partidasguardadas: 
      // Llamamos al intent de nuestras partidas guardadas 
      Intent intenthistorial = new Intent(this, Historial.class); 
      startActivity(intenthistorial); 
      break; 

     case R.id.reiniciarpartida: 
      // Ponemos todos los marcadores a 0 
      // Recorremos nuestra partida 
      numjugadores = partida.getJugadores().size(); 
      // recorremos y reiniciamos 
      for(int i = 0; i < numjugadores; i++){ 
       partida.getJugadores().get(i).setPuntuacion(0); 
      } 
      // Actualizamos el backup 
      backup.getBackup().set(indice, partida); 
      // Almacenamos 
      Log.i("MILOG", "Guardamos el backup"); 
      backup.guardarBackup(); 
      // Si todo ha ido bien, acutalizamos la lista de jugadores 
      ((AdaptadorTanteo) listviewjugadores.getAdapter()).notifyDataSetChanged(); 
      break; 

     case R.id.action_settings: 
      break; 
     default: 
      return true; 
    } 


    return super.onOptionsItemSelected(item); 
} 

// Adaptador para el layout del listview 
public class AdaptadorTanteo extends ArrayAdapter<Jugador> { 

    Activity context; 
    List<Jugador> jugadores; 
    ViewHolder holder; 

    AdaptadorTanteo(Activity context, int textViewResourceId, List<Jugador> listajugadores) { 
     super(context, textViewResourceId, listajugadores); 
     this.context = context; 
     this.jugadores = listajugadores; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     View item = convertView; 

     // Optimizamos el rendimiento de nuestra lista 
     // Si la vista no existe, la creamos 
     if (item == null) { 
      LayoutInflater inflater = context.getLayoutInflater(); 
      item = inflater.inflate(R.layout.tanteo_jugador, null); 

      // Declaramos el holder pasandole nuestras vistas 
      TextView nombre = (TextView) item.findViewById(R.id.nombrejugador); 
      TextView puntuacion = (TextView) item.findViewById(R.id.puntos); 
      ImageButton mas = (ImageButton) item.findViewById(R.id.sumar); 
      ImageButton menos = (ImageButton) item.findViewById(R.id.restar); 

      holder = new ViewHolder(nombre, puntuacion, mas, menos); 

      // Establecemos el tag 
      item.setTag(holder); 
     } 
     // Si la vista existe, la reusamos 
     else { 
      holder = (ViewHolder) item.getTag(); 
     } 

     // Guardamos la posicion en el holder para usarlo en los listener 
     holder.botonmas.setTag(position); 
     holder.botonmenos.setTag(position); 
     holder.puntos.setTag(position); 

     // Establecemos el nombre por defecto 
     holder.nombrejugador.setText(jugadores.get(position).getNombre()); 
     holder.puntos.setText(String.valueOf(jugadores.get(position).getPuntuacion())); 

     return (item); 
    } 
} 


class ViewHolder implements NumeroTanteoDialogFragment.NumberTanteoDialogListener{ 

    TextView nombrejugador; 
    TextView puntos; 
    ImageButton botonmas; 
    ImageButton botonmenos; 
    int position; 

    public ViewHolder(TextView nombre, TextView puntuacion, ImageButton mas, ImageButton menos) { 

     nombrejugador = nombre; 
     puntos = puntuacion; 
     botonmas = mas; 
     botonmenos = menos; 

     // Definimos los listener 
     nombrejugador.setOnClickListener(miListenerLocal); 
     puntos.setOnClickListener(miListenerLocal); 
     botonmas.setOnClickListener(miListenerLocal); 
     botonmenos.setOnClickListener(miListenerLocal); 

    } 

    // Creamos aqui los listener 
    // Asi tenemos acceso al resto de vistas dentro de la fila 
    private View.OnClickListener miListenerLocal = new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      position = (Integer) v.getTag(); 

      // Comprobamos la vista que lo esta invocando 
      switch (v.getId()) { 
       case R.id.nombrejugador: 

        break; 

       case R.id.puntos: 
        try { 
         // Decrementamos el tanteo 
         Log.i("MILOG", "Modificamos el tanteo"); 
         // Lanzamos el dialog 
         NumeroTanteoDialogFragment fragmento = new NumeroTanteoDialogFragment(); 
         Bundle bundles = new Bundle(); 
         bundles.putString("titulo", getString(R.string.sumar_puntos)); 
         fragmento.setArguments(bundles); 
         Log.i("MILOG", "Mostramos el dialog para elegir el numero que queremos modificar"); 
         FragmentManager fragmentManager = ((Activity) context).getFragmentManager(); 
         fragmento.show(fragmentManager, "Dialogo_jugadores"); 

        } catch (Exception ex) { 
         Toast.makeText(Tanteo.context, "Se produjo un error al modificar el tanteo", Toast.LENGTH_SHORT).show(); 
        } 
        break; 

       case R.id.sumar: 
        try { 
         Log.i("MILOG", "Sumamos uno"); 
         // Incrementamos el tanteo 
         int tantos = Integer.parseInt(puntos.getText().toString()) + 1; 
         puntos.setText(String.valueOf(tantos)); 
         // Actualizamos el backup 
         partida.getJugadores().get(position).setPuntuacion(tantos); 
         // Actualizamos el backup 
         backup.getBackup().set(indice, partida); 
         // Almacenamos 
         Log.i("MILOG", "Guardamos el backup"); 
         backup.guardarBackup(); 

        } catch (Exception ex) { 
         Toast.makeText(Tanteo.context, "Se produjo un error al incrementar el tanteo", Toast.LENGTH_SHORT).show(); 
        } 
        break; 

       case R.id.restar: 
        try { 
         // Decrementamos el tanteo 
         Log.i("MILOG", "Restamos uno"); 
         int tantos = Integer.parseInt(puntos.getText().toString()) - 1; 
         puntos.setText(String.valueOf(puntos)); 
         // Actualizamos el backup 
         partida.getJugadores().get(position).setPuntuacion(tantos); 
         // Actualizamos el backup 
         backup.getBackup().set(indice, partida); 
         // Almacenamos 
         Log.i("MILOG", "Guardamos el backup"); 
         backup.guardarBackup(); 
        } catch (Exception ex) { 
         Toast.makeText(Tanteo.context, "Se produjo un error al decrementar el tanteo", Toast.LENGTH_SHORT).show(); 
        } 
        break; 

      } 
     } 
    }; 

    // Sobreescribimos el metodo del dialogo para elegir el numero 
    @Override 
    public void onNumberSelected(int number) { 

     Log.i("MILOG", "Actualizamos los puntos con el dialog"); 
     int puntuacion = Integer.parseInt(puntos.getText().toString()) + number; 
     // Modificamos los puntos 
     puntos.setText(String.valueOf(puntuacion)); 

     // Actualizamos el backup 
     partida.getJugadores().get(position).setPuntuacion(puntuacion); 
     // Actualizamos el backup 
     backup.getBackup().set(indice, partida); 
     // Almacenamos 
     Log.i("MILOG", "Guardamos el backup"); 
     backup.guardarBackup(); 
    } 
} 

}

0

感谢您的帮助,

从来就解决了它使用的适应症。正如你告诉我的,我定义了听众。我的一个问题是我在两个不同的地方更新了viewholder对象。我改变了它,并且还用notifyDataSetChanged()更新了视图。

numberpicker的解决方案是在主要活动中实现回调方法,并将listview的位置作为参数传递以更改正确的项目。

此致敬礼。

相关问题