我是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);
}
答案 0 :(得分:1)
我没有看到任何用任何值修改或填充数组“rowItems”的地方。
您可以发布EditSettingsActivity,以便我们可以看到您在那里做了什么吗?
到目前为止,您可以更改以下代码行:
TextView t = new TextView(this);
t = (TextView)header.findViewById(R.id.textView1);
......对于这一个......
TextView t = (TextView)header.findViewById(R.id.textView1);
我刚看到你的适配器并且几乎是完美的,我可以看到你关注谷歌I / O,XD的谷歌视频....只需制作你的适配器......
static class ViewHolder {
EditSettingsActivity有助于了解应用的生命周期...但是现在......
如果您的MainActivity取决于您在EditSettingsActivity上设置的某些值,则必须将值返回到MainActivity以修改在MainActivity上声明的数组rowItem,并在修改此数组后,您可以调用notifyDataSetChanged()。
您可以使用startActivityForResult执行此操作,您可以在此处找到它的工作原理:
aryo.info/blog/android-returning-value-to-the-calling.html
我不知道您在EditSettingsActivity上修改了哪种类型的值,但是当您回来时,您也可以将它们存储在SharePreferences上并在MainActivity上检索它们。 这样,下次用户访问应用时,他会保留上次设置的相同首选项,并且只有在卸载应用时才会删除它们。
如何使用共享偏好设置:
http://developer.android.com/guide/topics/data/data-storage.html#pref
我刚刚在这个链接中添加了一个示例项目,它正是您想要的,它正在运行: