Skip to main content
se añadieron 372 caracteres en el cuerpo
Origen Enlace
Jorgesys
  • 111.1k
  • 14
  • 55
  • 134

public class UsersAdapter extends BaseAdapter {

public class UsersAdapter extends BaseAdapter {


    private static LayoutInflater inflater = null;
    public List<User> allUsers;

    private Context  context;

    /*************
     * Constructor
     *****************/
    public UsersAdapter(Context  ctx, ArrayList<User> data) {


        context = ctx;
        allUsers = data;
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

    }


    @Override
    public int getCount() {
        return allUsers.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View rowView = convertView;

        UserViewHolder holder = new UserViewHolder();

        final User user = allUsers.get(position);


        if(convertView==null)
        {
            rowView = inflater.inflate(R.layout.boxer_item, null);
            holder.name = (TextView) rowView.findViewById(R.id.txt_name);
            holder.icon = (ImageView) rowView.findViewById(R.id.img_user);

            rowView.setTag(holder);
        }
        else
        {
            holder = (UserViewHolder) rowView.getTag();
        }


        holder.name.setText(user.getName());

        Glide.with(context).load(user.getImage()).into(holder.icon);


       rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
              //  Toast.makeText(context, "You Clicked " + user.getName(), Toast.LENGTH_SHORT).show();

              if (user.getName().equals("Ramon \nDekkers")) {

                   Fragment fragment = new Ramon_dekkers();
                   FragmentManager fragmentManager = context.getFragmentManager(); -> Eso falla
                   FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                   fragmentTransaction.replace(R.id.frame,fragment); -> Y aqui
                   fragmentTransaction.addToBackStack(null); //If you want to add to back stack
                   fragmentTransaction.commit();

               }

            }
        });
        return rowView;
    }


    private static class UserViewHolder {
        public ImageView icon;
        public TextView name;

    }
}

}

public class UsersAdapter extends BaseAdapter {

private static LayoutInflater inflater = null;
public List<User> allUsers;

private Context  context;

/*************
 * Constructor
 *****************/
public UsersAdapter(Context  ctx, ArrayList<User> data) {


    context = ctx;
    allUsers = data;
    inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

}


@Override
public int getCount() {
    return allUsers.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = convertView;

    UserViewHolder holder = new UserViewHolder();

    final User user = allUsers.get(position);


    if(convertView==null)
    {
        rowView = inflater.inflate(R.layout.boxer_item, null);
        holder.name = (TextView) rowView.findViewById(R.id.txt_name);
        holder.icon = (ImageView) rowView.findViewById(R.id.img_user);

        rowView.setTag(holder);
    }
    else
    {
        holder = (UserViewHolder) rowView.getTag();
    }


    holder.name.setText(user.getName());

    Glide.with(context).load(user.getImage()).into(holder.icon);


   rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
          //  Toast.makeText(context, "You Clicked " + user.getName(), Toast.LENGTH_SHORT).show();

          if (user.getName().equals("Ramon \nDekkers")) {

               Fragment fragment = new Ramon_dekkers();
               FragmentManager fragmentManager = context.getFragmentManager(); -> Eso falla
               FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
               fragmentTransaction.replace(R.id.frame,fragment); -> Y aqui
               fragmentTransaction.addToBackStack(null); //If you want to add to back stack
               fragmentTransaction.commit();

           }

        }
    });
    return rowView;
}


private static class UserViewHolder {
    public ImageView icon;
    public TextView name;

}

}

public class UsersAdapter extends BaseAdapter {


    private static LayoutInflater inflater = null;
    public List<User> allUsers;

    private Context  context;

    /*************
     * Constructor
     *****************/
    public UsersAdapter(Context  ctx, ArrayList<User> data) {


        context = ctx;
        allUsers = data;
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

    }


    @Override
    public int getCount() {
        return allUsers.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View rowView = convertView;

        UserViewHolder holder = new UserViewHolder();

        final User user = allUsers.get(position);


        if(convertView==null)
        {
            rowView = inflater.inflate(R.layout.boxer_item, null);
            holder.name = (TextView) rowView.findViewById(R.id.txt_name);
            holder.icon = (ImageView) rowView.findViewById(R.id.img_user);

            rowView.setTag(holder);
        }
        else
        {
            holder = (UserViewHolder) rowView.getTag();
        }


        holder.name.setText(user.getName());

        Glide.with(context).load(user.getImage()).into(holder.icon);


       rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
              //  Toast.makeText(context, "You Clicked " + user.getName(), Toast.LENGTH_SHORT).show();

              if (user.getName().equals("Ramon \nDekkers")) {

                   Fragment fragment = new Ramon_dekkers();
                   FragmentManager fragmentManager = context.getFragmentManager(); -> Eso falla
                   FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                   fragmentTransaction.replace(R.id.frame,fragment); -> Y aqui
                   fragmentTransaction.addToBackStack(null); //If you want to add to back stack
                   fragmentTransaction.commit();

               }

            }
        });
        return rowView;
    }


    private static class UserViewHolder {
        public ImageView icon;
        public TextView name;

    }
}
se añadieron 448 caracteres en el cuerpo
Origen Enlace
Rf Mvs
  • 1.5k
  • 3
  • 25
  • 44
private static LayoutInflater inflater = null;
public List<User> allUsers;

private Context  context;

/*************
 * Constructor
 *****************/
public UsersAdapter(Context  ctx, ArrayList<User> data) {


    context = ctx;
    allUsers = data;
    inflater = (LayoutInflater) context.getSystemService(Contextcontext.LAYOUT_INFLATER_SERVICE);

}


@Override
public int getCount() {
    return allUsers.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = convertView;

    UserViewHolder holder = new UserViewHolder();

    final User user = allUsers.get(position);


    if(convertView==null)
    {
        rowView = inflater.inflate(R.layout.boxer_item, null);
        holder.name = (TextView) rowView.findViewById(R.id.txt_name);
        holder.icon = (ImageView) rowView.findViewById(R.id.img_user);

        rowView.setTag(holder);
    }
    else
    {
        holder = (UserViewHolder) rowView.getTag();
    }


    holder.name.setText(user.getName());

    Glide.with(context).load(user.getImage()).into(holder.icon);


   rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
          //  Toast.makeText(context, "You Clicked " + user.getName(), Toast.LENGTH_SHORT).show();

           if (user.getName().equals("Ramon \nDekkers")) { 

               Fragment fragment = new Ramon_dekkers();
               FragmentManager fragmentManager = context.getFragmentManager(); -> Eso falla
               FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
               fragmentTransaction.replace(R.id.frame,fragment); -> Y aqui
               fragmentTransaction.addToBackStack(null); //If AQUÍyou ESwant DONDEto NECESITOadd PONERto DEback IRstack
 A OTRO FRAGMENT, ¿CÓMO?           fragmentTransaction.commit();

           }

        }
    });
    return rowView;
}


private static class UserViewHolder {
    public ImageView icon;
    public TextView name;

}
private static LayoutInflater inflater = null;
public List<User> allUsers;

private Context context;

/*************
 * Constructor
 *****************/
public UsersAdapter(Context ctx, ArrayList<User> data) {


    context = ctx;
    allUsers = data;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


@Override
public int getCount() {
    return allUsers.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = convertView;

    UserViewHolder holder = new UserViewHolder();

    final User user = allUsers.get(position);


    if(convertView==null)
    {
        rowView = inflater.inflate(R.layout.boxer_item, null);
        holder.name = (TextView) rowView.findViewById(R.id.txt_name);
        holder.icon = (ImageView) rowView.findViewById(R.id.img_user);

        rowView.setTag(holder);
    }
    else
    {
        holder = (UserViewHolder) rowView.getTag();
    }


    holder.name.setText(user.getName());

    Glide.with(context).load(user.getImage()).into(holder.icon);


   rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
          //  Toast.makeText(context, "You Clicked " + user.getName(), Toast.LENGTH_SHORT).show();

           if (user.getName().equals("Ramon \nDekkers")) {

               // AQUÍ ES DONDE NECESITO PONER DE IR A OTRO FRAGMENT, ¿CÓMO?

           }

        }
    });
    return rowView;
}


private static class UserViewHolder {
    public ImageView icon;
    public TextView name;

}
private static LayoutInflater inflater = null;
public List<User> allUsers;

private Context  context;

/*************
 * Constructor
 *****************/
public UsersAdapter(Context  ctx, ArrayList<User> data) {


    context = ctx;
    allUsers = data;
    inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

}


@Override
public int getCount() {
    return allUsers.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = convertView;

    UserViewHolder holder = new UserViewHolder();

    final User user = allUsers.get(position);


    if(convertView==null)
    {
        rowView = inflater.inflate(R.layout.boxer_item, null);
        holder.name = (TextView) rowView.findViewById(R.id.txt_name);
        holder.icon = (ImageView) rowView.findViewById(R.id.img_user);

        rowView.setTag(holder);
    }
    else
    {
        holder = (UserViewHolder) rowView.getTag();
    }


    holder.name.setText(user.getName());

    Glide.with(context).load(user.getImage()).into(holder.icon);


   rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
          //  Toast.makeText(context, "You Clicked " + user.getName(), Toast.LENGTH_SHORT).show();

          if (user.getName().equals("Ramon \nDekkers")) { 

               Fragment fragment = new Ramon_dekkers();
               FragmentManager fragmentManager = context.getFragmentManager(); -> Eso falla
               FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
               fragmentTransaction.replace(R.id.frame,fragment); -> Y aqui
               fragmentTransaction.addToBackStack(null); //If you want to add to back stack
               fragmentTransaction.commit();

           }

        }
    });
    return rowView;
}


private static class UserViewHolder {
    public ImageView icon;
    public TextView name;

}
Origen Enlace
Rf Mvs
  • 1.5k
  • 3
  • 25
  • 44

Problema con método onClick en BaseAdapter

El problema es que tengo ListView y necesito que al hacer click en un item me vaya a otro Fragment siendo este BaseAdapter.

public class UsersAdapter extends BaseAdapter {

private static LayoutInflater inflater = null;
public List<User> allUsers;

private Context context;

/*************
 * Constructor
 *****************/
public UsersAdapter(Context ctx, ArrayList<User> data) {


    context = ctx;
    allUsers = data;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


@Override
public int getCount() {
    return allUsers.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = convertView;

    UserViewHolder holder = new UserViewHolder();

    final User user = allUsers.get(position);


    if(convertView==null)
    {
        rowView = inflater.inflate(R.layout.boxer_item, null);
        holder.name = (TextView) rowView.findViewById(R.id.txt_name);
        holder.icon = (ImageView) rowView.findViewById(R.id.img_user);

        rowView.setTag(holder);
    }
    else
    {
        holder = (UserViewHolder) rowView.getTag();
    }


    holder.name.setText(user.getName());

    Glide.with(context).load(user.getImage()).into(holder.icon);


   rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
          //  Toast.makeText(context, "You Clicked " + user.getName(), Toast.LENGTH_SHORT).show();

           if (user.getName().equals("Ramon \nDekkers")) {

               // AQUÍ ES DONDE NECESITO PONER DE IR A OTRO FRAGMENT, ¿CÓMO?

           }

        }
    });
    return rowView;
}


private static class UserViewHolder {
    public ImageView icon;
    public TextView name;

}

}