Insert more items into an Android ListView

0

I'm a beginner in Java / Android programming, and would like some help with adding items to a ListView.

The app receives the first 5 records of a Json (Url), and adds everything in the ListView so far.

The app then displays a button, which when clicked, searches the next 5 records.

Here's my question : The app replaces the first 5 records for the next 5, and I need this new content query added to the end of the ListView and not replace what was already.

Below, code snippets

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 popular methodCampeoes ();

ChampionsAdapter

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 for any help you may have.

    
asked by anonymous 19.02.2017 / 07:35

1 answer

3

Each time you receive new records, you must add them to the ArrayList and not create a new one. The same applies to the Adapter.

Declare listCampeoes and adapterCampeoes as attributes of Activity:

private ArrayList<Campeoes> listCampeoes;
private CampeoesAdapter adapterCampeoes;

In the onCreate() method, 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 popularCampeoes() method, add the new records to the ArrayList and notify the Adapter :

// Populando Campeoes ListView
private void popularCampeoes() {
    ArrayList<Campeoes> newCampeoes = Campeoes.fromJson(JsonCampeoes);
    listCampeoes.addAll(newCampeoes);
    adapterCampeoes.notifyDataSetChanged();
}
    
19.02.2017 / 12:24