0

我是 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);             

    }
4

1 回答 1

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 上声明的数组 rowItems,并且在修改此数组之后,您可以调用 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

我刚刚在此链接中添加了示例项目,该项目完全符合您的要求并且正在运行:

https://dl.dropboxusercontent.com/u/33565803/StackOverFlowExamples/ListViewHeaderAddItemsNotifyDataExample.zip

于 2013-06-21T00:46:43.097 回答