Insert more items into an Android Listview

Asked

Viewed 1,326 times

0

I am a beginner in Java/Android programming, and would like a help with adding items to a Listview.

The app receives the first 5 records from a Json (Url), and adds in Listview, so far, all right.

The app then displays a button, which when clicking, queries the next 5 records.

Here is my doubt: the app replaces the first 5 records for the next 5, and I need this new content query to be added to the end of Listview and not replace what was already.

Go below, snippets of the code

Main Activity

// Populando Campeoes ListView
private void popularCampeoes() {
    ArrayList<Campeoes> newCampeoes = Campeoes.fromJson(JsonCampeoes);
    CampeoesAdapter adapterCampeoes = new CampeoesAdapter(this, newCampeoes);

    ListView listClubeCampeoes = (ListView) findViewById(R.id.listClubeCampeoes);
    listClubeCampeoes.setAdapter(adapterCampeoes);
}

With Json downloaded, the app invokes the popularCampeoes method();

Campeoesadapter

class CampeoesAdapter extends ArrayAdapter<Campeoes> {

    CampeoesAdapter(Context context, ArrayList<Campeoes> campeoes) {
        super(context, 0, campeoes);
    }

    private Context context;

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

        final Campeoes campeoes = getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.campeoes_linha, parent, false);
        }

        TextView tvTitulo = (TextView) convertView.findViewById(R.id.clubeCampTitulo);
        TextView tvConteudo = (TextView) convertView.findViewById(R.id.clubeCampConteudo);

        assert campeoes != null;

        tvTitulo.setText(campeoes.CampTitulo);
        tvConteudo.setText(campeoes.CampConteudo);

        return convertView;
    }

}

I thank you immediately, any help you have.

1 answer

3


Each time you receive new records you should add them to Arraylist and not create a new one. The same applies to Adapter.

Declare the variables listCampeoes and adapterCampeoes as attributes of Activity:

private ArrayList<Campeoes> listCampeoes;
private CampeoesAdapter adapterCampeoes;

In the method onCreate() create the Arraylist and the Adapt:

@Override
protected void onCreate(Bundle savedInstanceState) {

    ...
    ...
    ArrayList<Campeoes> listCampeoes = new ArrayList<>();
    CampeoesAdapter adapterCampeoes = new CampeoesAdapter(this, listCampeoes);

    ListView listClubeCampeoes = (ListView) findViewById(R.id.listClubeCampeoes);
    listClubeCampeoes.setAdapter(adapterCampeoes);

    ...
}

In the method popularCampeoes() add the new records to Arraylist and notify the Adapter:

// Populando Campeoes ListView
private void popularCampeoes() {
    ArrayList<Campeoes> newCampeoes = Campeoes.fromJson(JsonCampeoes);
    listCampeoes.addAll(newCampeoes);
    adapterCampeoes.notifyDataSetChanged();
}
  • Perfect ramaral, with its simple and objective explanation, I was able to understand the difference between the form that was written and the form that Oce left as an example. Thank you very much !!!!

Browser other questions tagged

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