Pregunta

I have a navigation drawer with a list of items and icons for those items. The thing is that when i click on an item of that list changes the position ramdonly and i don't know why.

This is the code in the MainActivity:

// Drawer Layout
        NavDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        // Lista
        NavList = (ListView) findViewById(R.id.lista);
        // Declaramos el header el cual sera el layout de header.xml
        View header = getLayoutInflater().inflate(R.layout.header, null);
        // Establecemos header
        NavList.addHeaderView(header);
        // Tomamos listado de imgs desde drawable
        NavIcons = getResources().obtainTypedArray(R.array.navigation_iconos);
        // Tomamos listado de titulos desde el string-array de los recursos
        // @string/nav_options
        titulos = getResources().getStringArray(R.array.nav_options);
        // Listado de titulos de barra de navegacion
        NavItms = new ArrayList<Item_objct>();
        // Agregamos objetos Item_objct al array
        // Perfil
        NavItms.add(new Item_objct(titulos[0], NavIcons.getResourceId(0, -1)));
        // Favoritos
        NavItms.add(new Item_objct(titulos[1], NavIcons.getResourceId(1, -1)));
        // Eventos
        NavItms.add(new Item_objct(titulos[2], NavIcons.getResourceId(2, -1)));
        // Lugares
        NavItms.add(new Item_objct(titulos[3], NavIcons.getResourceId(3, -1)));
        // Etiquetas
        NavItms.add(new Item_objct(titulos[4], NavIcons.getResourceId(4, -1)));
        // Configuracion
        NavItms.add(new Item_objct(titulos[5], NavIcons.getResourceId(5, -1)));
        // Share
        NavItms.add(new Item_objct(titulos[6], NavIcons.getResourceId(6, -1)));

        // Declaramos y seteamos nuestro adaptador al cual le pasamos el array
        // con los titulos
        NavAdapter = new NavigationAdapter(this, NavItms);
        NavList.setAdapter(NavAdapter);
        // Siempre vamos a mostrar el mismo titulo
        // mTitle = mDrawerTitle = getTitle();

        // Declaramos el mDrawerToggle y las imgs a utilizar
        mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
        NavDrawerLayout, /* DrawerLayout object */
        R.drawable.ic_drawer, /* Icono de navegacion */
        R.string.app_name, /* "open drawer" description */
        R.string.hello_world /* "close drawer" description */
        ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                Log.e("Cerrado completo", "!!");
                getSupportActionBar().setTitle(titulos[0]);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                Log.e("Apertura completa", "!!");
                getSupportActionBar().setTitle("Gettford Community");
            }
        };

        // Establecemos que mDrawerToggle declarado anteriormente sea el
        // DrawerListener
        NavDrawerLayout.setDrawerListener(mDrawerToggle);
        // Establecemos que el ActionBar muestre el Boton Home

        // Establecemos la accion al clickear sobre cualquier item del menu.
        // De la misma forma que hariamos en una app comun con un listview.
        NavList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long id) {
                MostrarFragment(position);
                getSupportActionBar().setTitle(titulos[position -1]);
            }
        });

        // Cuando la aplicacion cargue por defecto mostrar la opcion Home
        MostrarFragment(1);

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

This is the code for the adapter of the Drawer:

public class NavigationAdapter extends BaseAdapter {
    private Activity activity;  
    ArrayList<Item_objct> arrayitms; 

   public NavigationAdapter(Activity activity,ArrayList<Item_objct>  listarry) {  
       super();  
       this.activity = activity;  
       this.arrayitms=listarry;
       }     
   //Retorna objeto Item_objct del array list
   @Override
   public Object getItem(int position) {       
       return arrayitms.get(position);
   }   
    public int getCount() {  
      // TODO Auto-generated method stub  
        return arrayitms.size();  
    }    
    @Override
    public long getItemId(int position) {
        return position;
    }   
    //Declaramos clase estatica la cual representa a la fila
    public static class Fila  
    {  
            TextView titulo_itm;
            ImageView icono;
    }  
   public View getView(int position, View convertView, ViewGroup parent) {  
      // TODO Auto-generated method stub  
       Fila view;  
       LayoutInflater inflator = activity.getLayoutInflater();  
      if(convertView==null)  
       {  
           view = new Fila();
           //Creo objeto item y lo obtengo del array
           Item_objct itm=arrayitms.get(position);
           convertView = inflator.inflate(R.layout.drawer_list_item, null);
           //Titulo
           view.titulo_itm = (TextView) convertView.findViewById(R.id.title_item);
           //Seteo en el campo titulo el nombre correspondiente obtenido del objeto
           view.titulo_itm.setText(itm.getTitulo());           
           //Icono
           view.icono = (ImageView) convertView.findViewById(R.id.icon);
           //Seteo el icono
           view.icono.setImageResource(itm.getIcono());           
           convertView.setTag(view);  
        }  
        else  
        {  
           view = (Fila) convertView.getTag();  
        }  
        return convertView;  
    }
}

In Android 4.0+ works great but in Android 2.3 and 2.2 it's gone crazy.

Appreciate any help.

Thanks

¿Fue útil?

Solución

Use this code for Your NavigationAdapter class:-

public class NavigationAdapter extends BaseAdapter {
     private Context context; 
    ArrayList<Item_objct> arrayitms; 

   public NavigationAdapter(Context context,ArrayList<Item_objct>  listarry) {  
       super();  
       this.context = context;  
       this.arrayitms=listarry;
       }     
   //Retorna objeto Item_objct del array list
   @Override
   public Object getItem(int position) {       
       return arrayitms.get(position);
   }   
    public int getCount() {  
      // TODO Auto-generated method stub  
        return arrayitms.size();  
    }    
    @Override
    public long getItemId(int position) {
        return position;
    }   

   public View getView(int position, View convertView, ViewGroup parent) {  
      // TODO Auto-generated method stub  
       if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.drawer_list_item, null);
    }


           Item_objct itm=arrayitms.get(position);          
           //Titulo
           TextView titulo_itm = (TextView) convertView.findViewById(R.id.title_item);
           //Seteo en el campo titulo el nombre correspondiente obtenido del objeto
           titulo_itm.setText(itm.getTitulo());           
           //Icono
           ImageView icono = (ImageView) convertView.findViewById(R.id.icon);
           //Seteo el icono
           icono.setImageResource(itm.getIcono());           
        return convertView;  
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top