如果这是一个简单的错误,我还没有在列表中使用过,所以请原谅我。我试图在用户想要查看他/她当前位置时动态更新列表。当用户点击按钮时,将获取当前位置(lat和lon)并将其插入列表中。我的清单没有得到更新。它甚至没有显示ArrayList
之内的数据。
这是我在onCreate()
listView = (ListView)findViewById(R.id.list);
adapter = new CustomList(StartCalc.this, 0, 0);
listView.setAdapter(adapter);
将lat和lon添加到arraylist
CustomList.lat.add(_CURLAT);
CustomList.lon.add(_CURLON);
adapter.notifyDataSetChanged();
我的CustomList类
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;
}
}
提前致谢
答案 0 :(得分:1)
尝试拨打
listview.notifyDataSetChanged();
还要更改您的适配器,如下所示:
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;
}
}
答案 1 :(得分:0)
更新yout listview内容,您需要通知更改为适配器的数据集。 尝试拨打
adapter.notifyDataSetChanged();
后
CustomList.lat.add(_CURLAT); CustomList.lon.add(_CURLON);
希望这有帮助。
尝试在适配器构造函数中传递'lat'和'lon'的默认值。像这样,
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);
}
答案 2 :(得分:0)
试试这个..
剪切以下行
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>();
并粘贴到 MainActivity
因为每次刷新适合启动 再次 的适配器
以及您在CustomList
内添加错误的项目也会在MainActivity中添加这些项目。