Search code examples
javaandroidarraylistandroid-listfragment

Polulate ListFragment using an ArrayList


I am a complete beginner to Android Dev so I am following guides that I can find. I want to populate a ListFragment from an ArrayList. So far, I have used http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/ to get a sliding menu which shows different Fragments when the items in the menu are clicked. Then I used

package de.vogella.android.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.ListFragment;

public class MyListFragment extends ListFragment {

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
  }

  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
    // do something with the data
  }
        } 

to populate the ListFragment.

I want to use an ArrayList instead. I want to use this code:

package com.example.triage;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private List<Patient> patients = new ArrayList<Patient>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        populatePatientList();
        populateListView();
        registerClickCallback();
    }

    private void populatePatientList() {
        patients.add(new Patient("Vijay Vazirani", 0));
        patients.add(new Patient("Jie Wu", 2));
        patients.add(new Patient("Nadia Magnenat Thalmann", 3));
        patients.add(new Patient("Andrew Ng", 1));
        patients.add(new Patient("Steve Omohundro", 0));
        patients.add(new Patient("Joseph Sifakis", 4));
        patients.add(new Patient("Eva Tardos", 2));
        patients.add(new Patient("Konrad Zuse", 1));
    }

    private void populateListView() {
        ArrayAdapter<Patient> adapter = new PatientListAdapter();
        ListView patientList = (ListView) findViewById(R.id.listPatientsInER);
        patientList.setAdapter(adapter);

    }

    private class PatientListAdapter extends ArrayAdapter<Patient> {
        public PatientListAdapter(){
            super(MainActivity.this, R.layout.view_list_item, patients);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // get a view to work with
            View itemView = convertView;
            if(itemView == null){
                itemView = getLayoutInflater().inflate(R.layout.view_list_item, parent, false);
            }

            // find the patient to work with
            Patient currentPatient = patients.get(position);

            //patient name
            TextView nameText = (TextView)itemView.findViewById(R.id.listItem_textPatientName);
            nameText.setText(currentPatient.getName());

            //Urgency
            TextView urgencyText = (TextView)itemView.findViewById(R.id.listItem_textUrgencyValue);
            urgencyText.setText(currentPatient.getUrgency());

            return itemView;
        }

    }

    private void registerClickCallback() {
        ListView list = (ListView) findViewById(R.id.listPatientsInER);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Patient clickedPatient = patients.get(position);
                Toast.makeText(MainActivity.this, clickedPatient.getName(), Toast.LENGTH_SHORT).show();             
            }
        });
    }
}

Could someone guide me on how to do that? Every time I try, I get stuck at the method populateListView() because there is no ListView in ListFragment (I think) and hence findViewById() doesnt work.

Thanks! Makrand


Solution

  • Bingo, the problem lies in your populateListView :

    private void populateListView() {
            ArrayAdapter<Patient> adapter = new PatientListAdapter();
            //add the list patients to your adapter
            for(Patient p: patients)
            {
               adapter.add(p);
            }
            ListView patientList = (ListView) findViewById(R.id.listPatientsInER);
            patientList.setAdapter(adapter);
    
        }
    

    You have set the adapter to the listview, but you forgot to insert the list patients to the adapter, hence your adapter has no data and your listview display nothing.