Skip to main content
tags editadas
Link
ramaral
  • 44,7mil
  • 14
  • 60
  • 110
public class UsuariosAdapter extends BaseAdapter {
    Context ctx;
List<Usuario>    List < Usuario > usuarios;

    public UsuariosAdapter(Context ctx, List<Usuario>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;
    }
}
public class UsuarioListFragment extends ListFragment {
List<Usuario>    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>ArrayList < Usuario > ();

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

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

    }

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

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

    mUsuarios = new ArrayList<Usuario>ArrayList < Usuario > ();

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

    mAdapter = UsuariosAdapter(this, mUsuarios);
    mListView.setAdapter(mAdapter);
 
}
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;
  }
}
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);

   }

}
 @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);
 
}
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;
    }
}
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);

    }

}
@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);
}
Fonte Link

Como usar um Adapter personalizado no ListFragment do android

Olá pessoal estou tendo problemas com a utilização de um Adapter customizado e exibir este Adapter em uma ListFragment.

###Meu Adapter Customizado

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;
  }
}

###Minha 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);

   }

}

###Local onde está dando erro:

 @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);

}