suja
suja

Reputation: 1278

List not getting updated

I haven't worked with list before so pardon me if this is a simple mistake. Am trying to update the list dynamically when ever the user want to see his/her current location. When the user clicks on a button current location(lat and lon) are fetched and inserted into list. My list is not getting updated. It is not even showing the data which is within the ArrayList am confused.

this is how i declare my list in onCreate()

listView        = (ListView)findViewById(R.id.list);
adapter = new CustomList(StartCalc.this, 0, 0);        
listView.setAdapter(adapter);

adding lat and lon to arraylist

CustomList.lat.add(_CURLAT);
CustomList.lon.add(_CURLON);
adapter.notifyDataSetChanged();

my CustomList class

public class CustomList extends ArrayAdapter<String>{


int _COUNT;
public static ArrayList<Double> lat = new ArrayList<Double>();
public static ArrayList<Double> lon = new ArrayList<Double>();
public static ArrayList<Double> ele = new ArrayList<Double>();
public static ArrayList<Double> dis = new ArrayList<Double>();

private final Activity context;

TextView location_list_textViewNO;
TextView location_list_lat;
TextView location_list_lon;
TextView location_list_ele;
TextView location_list_dis;


public CustomList(Activity context, int resource, int textViewResourceId) {
    super(context, R.layout.location_list, textViewResourceId);
    // TODO Auto-generated constructor stub
    this.context = context;
    this._COUNT  = textViewResourceId;

    lat.add(2.333);
    lon.add(5.03333);
    ele.add(5.77777777);
    dis.add(7.2222);    

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.location_list, null, true);

    location_list_textViewNO = (TextView) rowView.findViewById(R.id.location_list_textViewNO);
    location_list_lat = (TextView) rowView.findViewById(R.id.location_list_lat);
    location_list_lon = (TextView) rowView.findViewById(R.id.location_list_lon);
    location_list_ele = (TextView) rowView.findViewById(R.id.location_list_ele);
    location_list_dis = (TextView) rowView.findViewById(R.id.location_list_dis);

    location_list_textViewNO.setText("A");
    location_list_lat.setText("LAT: " + lat.get(position));
    location_list_lon.setText("LON: " + lon.get(position));
    location_list_ele.setText("ELE: " + ele.get(position));
    location_list_dis.setText("DIS: " + dis.get(position)); 

    return rowView;
}

}

Thanks in advance

Upvotes: 0

Views: 95

Answers (3)

AndyN
AndyN

Reputation: 1754

to update yout listview contents you need to notify dataset changed to your adapter. Try calling

adapter.notifyDataSetChanged();

after

CustomList.lat.add(_CURLAT); CustomList.lon.add(_CURLON);

Hope this helps.

try passing default values for 'lat' and 'lon' in your adapter constructor. like this ,

public CustomList(Activity context, int resource, int textViewResourceId,Double lat_val = 2.333, Double lon_val = 5.03333) {
super(context, R.layout.location_list, textViewResourceId,lat);//<---- Set one of the list as the 4th argument of super constructor 
// TODO Auto-generated constructor stub
this.context = context;
this._COUNT  = textViewResourceId;

lat.add(lat_val);
lon.add(lon_val);
ele.add(5.77777777);
dis.add(7.2222);    

}

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this..

Cut the below lines

public static ArrayList<Double> lat = new ArrayList<Double>();
public static ArrayList<Double> lon = new ArrayList<Double>();
public static ArrayList<Double> ele = new ArrayList<Double>();
public static ArrayList<Double> dis = new ArrayList<Double>();

and paste in your MainActivity

because every time if you refresh that adapter that will initilize again and again..

and also your adding items inside CustomList that's wrong add that items also in the MainActivity.

Upvotes: 0

vipul mittal
vipul mittal

Reputation: 17401

Try calling

listview.notifyDataSetChanged();

after adding lat/long

Also change your adapter as following:

public class CustomList extends ArrayAdapter<Double>{//Set Double as data type


int _COUNT;
public static ArrayList<Double> lat = new ArrayList<Double>();
public static ArrayList<Double> lon = new ArrayList<Double>();
public static ArrayList<Double> ele = new ArrayList<Double>();
public static ArrayList<Double> dis = new ArrayList<Double>();

private final Activity context;

TextView location_list_textViewNO;
TextView location_list_lat;
TextView location_list_lon;
TextView location_list_ele;
TextView location_list_dis;


public CustomList(Activity context, int resource, int textViewResourceId) {
    super(context, R.layout.location_list, textViewResourceId,lat);//<---- Set one of the list as the 4th argument of super constructor 
    // TODO Auto-generated constructor stub
    this.context = context;
    this._COUNT  = textViewResourceId;

    lat.add(2.333);
    lon.add(5.03333);
    ele.add(5.77777777);
    dis.add(7.2222);    

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.location_list, null, true);

    location_list_textViewNO = (TextView) rowView.findViewById(R.id.location_list_textViewNO);
    location_list_lat = (TextView) rowView.findViewById(R.id.location_list_lat);
    location_list_lon = (TextView) rowView.findViewById(R.id.location_list_lon);
    location_list_ele = (TextView) rowView.findViewById(R.id.location_list_ele);
    location_list_dis = (TextView) rowView.findViewById(R.id.location_list_dis);

    location_list_textViewNO.setText("A");
    location_list_lat.setText("LAT: " + lat.get(position));
    location_list_lon.setText("LON: " + lon.get(position));
    location_list_ele.setText("ELE: " + ele.get(position));
    location_list_dis.setText("DIS: " + dis.get(position)); 

    return rowView;
}

}

Upvotes: 1

Related Questions