How to use a custom Adapter in the android ListFragment

4

Hello everyone, I'm having trouble using a custom Adapter and displaying 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);

    }

}

Location where you are 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);
}
    
asked by anonymous 29.07.2015 / 16:40

1 answer

3

If the function of your ListFragment is just to display a ListView you do not need to create it since it has one by default.

In addition, this is not a Context for this to use getActivity()

Change method onActivityCreated() like this:

@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));
}
    
29.07.2015 / 17:09