Android ListView不会更新动态位置数据

时间:2013-11-04 23:30:54

标签: android-listview simplecursoradapter onresume

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

因此,在按下“后退”按钮或手机改变方向之前,它可以正常工作。在onResume中,listview变为空,所以我不得不再次打开数据库并将光标再次设置为适配器和适配器到listview。这将在调用“onResume”时使用数据库中的完整数据填充listview。

但是,当在“onLocationChanged”中添加新位置数据时,新数据不会填充列表视图,直到再次调用“onResume”。在“onResume”中调用adapter.notifyDataSetChanged并且“onLocation”已更改但无效。我的猜测是在调用“onCreate”之后listview已经改为另一个但我不知道如何解决这个问题。

请知道此问题的任何人让我知道我的代码有什么问题。

这是我的代码:

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

}

... }

0 个答案:

没有答案