How to use a custom Adapter on android Listfragment

Asked

Viewed 552 times

4

Hello folks I’m having problems with using a Adapter customized and display this Adapter in a Listfragment.

My Custom Adapter

public class UsuariosAdapter extends BaseAdapter {
    Context ctx;
    List < Usuario > usuarios;

    public UsuariosAdapter(Context ctx, List < Usuario > usuarios) {
        this.ctx = ctx;
        this.usuarios = usuarios;
    }


    @Override
    public int getCount() {
        return 0;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Primeiro passo
        Usuario usuario = usuarios.get(position);

        //Segundo passo
        View linha = LayoutInflater.from(ctx).inflate(R.layout.item_usuario, null);

        //Terceiro passo
        ImageView imgUser = (ImageView) linha.findViewById(R.id.imgUser);
        TextView txtNome = (TextView) linha.findViewById(R.id.txtNome);
        TextView txtEmail = (TextView) linha.findViewById(R.id.txtEmail);
        TextView txtEstrelas = (TextView) linha.findViewById(R.id.txtEstrelas);

        //0=VW; 1=GM; 2=Fiat 3=Ford;
        Resources res = ctx.getResources();

        //TypedArray imgUser = res.obtainTypedArray(R.array.usuarios);
        imgUser.setImageResource(R.mipmap.ic_launcher);

        txtNome.setText(usuario.nome);
        txtEmail.setText(usuario.email);

        txtEstrelas.setText(String.valueOf(usuario.estrelas));

        return linha;
    }
}

My Listfragment

public class UsuarioListFragment extends ListFragment {
    List < Usuario > mUsuarios;
    UsuariosAdapter mAdapter;
    ListView mListView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mListView = new ListView(this);
        setContentView(mListView);

        mUsuarios = new ArrayList < Usuario > ();

        mUsuarios.add(new Usuario("Thiago Soares", "Quadra 516 Conjunto 4", "[email protected]", 4.5f));

        mAdapter = UsuariosAdapter(this, mUsuarios);
        mListView.setAdapter(mAdapter);

    }

}

Place where it is giving error:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mListView = new ListView(this);
    setContentView(mListView);

    mUsuarios = new ArrayList < Usuario > ();

    mUsuarios.add(new Usuario("Thiago Soares", "Quadra 516 Conjunto 4", "[email protected]", 4.5f));

    mAdapter = UsuariosAdapter(this, mUsuarios);
    mListView.setAdapter(mAdapter);
}
  • What’s the mistake ?

  • mListView = new Listview(this); setContentView(mListView);

  • Note that convertView.getContext() returns the context, so no need to pass it in the Adapter constructor.

1 answer

3


If the function of your Listfragment is merely presenting a Listview doesn’t need to create it since he has one by default.

Moreover, this is not a Context for that use getActivity()

Change the method onActivityCreated() thus:

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    mUsuarios = new ArrayList<Usuario>();

    mUsuarios.add(new Usuario("Thiago Soares", "Quadra 516 Conjunto 4", "[email protected]", 4.5f));

    setListAdapter(new UsuariosAdapter(getActivity(), mUsuarios));
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.