1

我已经彻底研究了这个主题,并在 StackOverflow 上发现了类似的问题,但对我的问题不够具体。我正在尝试使用 SimpleCursorAdapter 更新我的 ListView。我有一个“获取网络位置”按钮,当我按下它时,每当位置在方法“onLocationChanged”内发生变化时,它都会用新的位置数据(id、lat、lon、acc、时间)动态填充我的 ListView。这是通过将新的位置数据添加到数据库并将光标设置到适配器来完成的。

所以它工作正常,直到按下“返回”按钮或手机改变方向。在 onResume 中,listview 变空了,所以我不得不再次打开数据库并将光标设置为适配器,并将适配器再次设置为 listview。这会在调用“onResume”时使用来自数据库的完整数据填充列表视图。

但是,当在“onLocationChanged”中添加新的位置数据时,新数据不会填充列表视图,直到再次调用“onResume”。在“onResume”和“onLocation”中都调用了 adapter.notifyDataSetChanged,但无济于事。我的猜测是调用“onCreate”后列表视图已更改为不同的视图,但我不知道如何解决。

请了解此问题的任何人告诉我我的代码有什么问题。

这是我的代码:

public class MainActivity extends Activity {

LocationManager locMan;
String provider;
Boolean netWork_enabled = false;
private static long MINTIME;
private static float MINDIS;
Cursor cursor;
NetworkScanDB GeoLocInfoDb;
String row;
double lat;
double lon;
double accur;
double time;
EditText etMinTime;
EditText etMinDis;
ListView lv;
SimpleCursorAdapter sd;
String[] columns;
int[] to;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // initialize lv
    lv = (ListView) findViewById(R.id.listView1);

    // getting min time and distance from edit text
    etMinTime = (EditText) findViewById(R.id.et_minTime);
    etMinDis = (EditText) findViewById(R.id.et_minDis);

    // initiating location
    locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    provider = locMan.NETWORK_PROVIDER;

    try {
        netWork_enabled = locMan.isProviderEnabled(provider);
    } catch (Exception ex) {
    }

    columns = new String[] { NetworkScanDB.Key_RowID,
            NetworkScanDB.Key_Lat, NetworkScanDB.Key_Lon,
            NetworkScanDB.Key_Accur, NetworkScanDB.Key_Time };

    to = new int[] { R.id.t0, R.id.t1, R.id.t2, R.id.t3, R.id.t4 };

    sd = new SimpleCursorAdapter(MainActivity.this, R.layout.nsrow, cursor,
            columns, to, 0); // had to change to api 11., 0=no query

}

LocationListener locationListenerNetwork = new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        try {

            GeoLocInfoDb = new NetworkScanDB(MainActivity.this); 
            GeoLocInfoDb.open();

            // insert row into DB
            GeoLocInfoDb.insertGeoLocInfo(location.getLatitude(),
                    location.getLongitude(), location.getAccuracy(),
                    location.getTime());

            cursor = GeoLocInfoDb.getGeoLocInfoCursor();

            sd = new SimpleCursorAdapter(MainActivity.this, R.layout.nsrow,
                    cursor, columns, to, 0); // had to change to api 11.,
                                                // 0=no query

            Toast.makeText(getApplicationContext(),
                    "added new location onLocationChanged",
                    Toast.LENGTH_LONG).show();

            // lv = (ListView) findViewById(R.id.listView1);

            sd.notifyDataSetChanged();

            lv.setAdapter(sd);

            GeoLocInfoDb.close();

        } catch (Exception e) {
            Log.w("nwscan", e.toString());
        }

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
};

public void getNetworkLocation(View v) {

    MINTIME = Long.parseLong(etMinTime.getText().toString());
    MINDIS = Float.parseFloat(etMinDis.getText().toString());

    if (netWork_enabled) {

        locMan.requestLocationUpdates(provider, MINTIME, MINDIS,
                locationListenerNetwork);

    } else {
        Toast.makeText(getApplicationContext(), "network not enable",
                Toast.LENGTH_LONG).show();
    }

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    Toast.makeText(getApplicationContext(), "onResume ", Toast.LENGTH_LONG)
            .show();

    GeoLocInfoDb = new NetworkScanDB(MainActivity.this); 
    GeoLocInfoDb.open();
    cursor = GeoLocInfoDb.getGeoLocInfoCursor();

    sd = new SimpleCursorAdapter(MainActivity.this, R.layout.nsrow, cursor,
            columns, to, 0); // had to change to api 11., 0=no query

    sd.notifyDataSetChanged();

    lv.setAdapter(sd);

}

... }

4

0 回答 0