我是 Java 和 Android 新手。我正在创建需要列表视图的东西。列表视图数据是根据在首选项窗口中设置的 GPS 坐标计算的。我的问题是,在我输入新坐标后,列表视图数据并没有反映这一点。
到目前为止我尝试了什么?我观看了 Google I/O 2010 视频并在 onResume() 期间使用 notifyDataSetChanged()。
但是,当我这样做时,列表视图及其标题会被填充两次。我究竟做错了什么?代码部分如下
public class MainActivity extends Activity{
GPSTracker gps;
Date yetzt = new Date();
String locationName = "";
double latitude = 0;
double longitude = 0;
double elevation = 0;
List<RowItem> rowItems;
ListView listView;
String tmp = "";
ItemListViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItems = new ArrayList<RowItem>();
// check if GPS enabled
if (gps.canGetLocation()) {
showInfo();
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
private void showInfo() {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this);
TimeZone tz = TimeZone.getDefault();
locationName = tz.getDisplayName(false, TimeZone.SHORT);
latitude = Float.parseFloat(sharedPrefs.getString("manual_fetch_lat", Double.toString(gps.getLatitude())));
longitude = Float.parseFloat(sharedPrefs.getString("manual_fetch_long", Double.toString(gps.getLongitude())));
TimeZone timeZone = TimeZone.getTimeZone(tz.getID());
listView = (ListView) findViewById(R.id.list);
//add stuff into rowItems here
adapter = new ItemListViewAdapter(MainActivity.this, R.layout.listview_item_row, rowItems);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
TextView t = new TextView(this);
t = (TextView)header.findViewById(R.id.textView1);
if ( t != null ) {
t.setText("Demo");
}
listView.addHeaderView(header);
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 0, 0, R.string.preference_menu_title);
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
startActivity(new Intent(this, EditSettingsActivity.class));
return true;
}
return false;
}
}
我的 ListViewAdapter 如下
public class ItemListViewAdapter extends ArrayAdapter<RowItem> {
private Context context;
public ItemListViewAdapter(Context context, int resource,
List<RowItem> objects) {
super(context, resource, objects);
this.context = context;
}
/* private view holder class */
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview_item_row, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
ListView 数据如图所示填充
GeoLocation location = new GeoLocation(locationName, latitude, longitude, elevation, timeZone);
ComplexZmanimCalendar czc = new ComplexZmanimCalendar(location);
ComplexZmanimCalendar czcPrevious = new ComplexZmanimCalendar(location);
ComplexZmanimCalendar czcNight = new ComplexZmanimCalendar(location);
Calendar cal = Calendar.getInstance();
Toast.makeText(getApplicationContext(), "Section 1", Toast.LENGTH_LONG).show();
//subtract one day from current date
cal.add(Calendar.DATE, -1);
czcPrevious.setCalendar(cal);
diff = czcPrevious.getSunset().getTime() - czcPrevious.getSunrise().getTime();
interval = diff/12;
SimpleDateFormat parserSDF=new SimpleDateFormat("HH:mm");
List<String> x = new ArrayList<String>();
for(int counter=0; counter < 12; counter++){
tmp = "From "+ parserSDF.format(new Date(czcPrevious.getSunrise().getTime() + (counter*interval) ))+" to "+parserSDF.format(new Date(czcPrevious.getSunrise().getTime() + ((counter+1)*interval) ));
n = getResources().getIdentifier(logo[counter].toLowerCase(),"drawable", getPackageName());
item = new RowItem(n, tmp);
rowItems.add(item);
}
diff = czc.getSunrise().getTime() - czcPrevious.getSunset().getTime();
interval = diff/12;
for(int counter=0; counter < 12; counter++){
tmp = "From "+ parserSDF.format(new Date(czcPrevious.getSunset().getTime() + (counter*interval) ))+" to "+parserSDF.format(new Date(czcPrevious.getSunset().getTime() + ((counter+1)*interval) ));
n = getResources().getIdentifier(logo[counter].toLowerCase(),"drawable", getPackageName());
item = new RowItem(n, tmp);
rowItems.add(item);
}