2015-12-22 82 views
0

我遇到了讓我發瘋的問題。我以前使用相同代碼製作的每個適配器都可以很好地工作,但是這不適用。調用.notifyDataSetChanged()不起作用,它只調用getView()1次,無論List有多少項。ListView適配器不像預期的那樣運行

總結:當我調用.notifyDataSetChanged()時,適配器不刷新列表視圖,它只調用getView()一次。

適配器代碼:

public class ObstacleListAdapter extends BaseAdapter { 

private Activity activity; 
private LayoutInflater inflater; 
private List<Obstacle> feedItems; 

public ObstacleListAdapter(Activity activity, List<Obstacle> feedItems) { 
    this.activity = activity; 
    this.feedItems = feedItems; 
} 

@Override 
public int getCount() { 
    return feedItems.size(); 
} 

@Override 
public Object getItem(int location) { 
    return feedItems.get(location); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    if (inflater == null) 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if (convertView == null) 
     convertView = inflater.inflate(R.layout.obstacle_list_row, null); 


    TextView text = (TextView) convertView.findViewById(R.id.obstacle_text); 

    Obstacle obstacle = feedItems.get(position); 

    text.setText(obstacle.getName()+" à "+ 
      calculationByDistance(MapsActivity.MY_LOC,new LatLng(obstacle.getLatitude(),obstacle.getLongitude()))+" metros"); 


    return convertView; 
} 

public double calculationByDistance(LatLng StartP, LatLng EndP) { 
    int Radius = 6371;// radius of earth in Km 
    double lat1 = StartP.latitude; 
    double lat2 = EndP.latitude; 
    double lon1 = StartP.longitude; 
    double lon2 = EndP.longitude; 
    double dLat = Math.toRadians(lat2 - lat1); 
    double dLon = Math.toRadians(lon2 - lon1); 
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) 
      + Math.cos(Math.toRadians(lat1)) 
      * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon/2) 
      * Math.sin(dLon/2); 
    double c = 2 * Math.asin(Math.sqrt(a)); 
    double valueResult = Radius * c; 
    double km = valueResult/1; 
    DecimalFormat newFormat = new DecimalFormat("####"); 
    int kmInDec = Integer.valueOf(newFormat.format(km)); 
    double meter = valueResult % 1000; 
    int meterInDec = Integer.valueOf(newFormat.format(meter)); 
    Log.i("Radius Value", "" + valueResult + " KM " + kmInDec 
      + " Meter " + meterInDec); 

    return kmInDec*1000; 
} 

活動代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
    ctx = this; 
    dbOperations = new DatabaseOperations(ctx); 

    listView = (ListView) findViewById(R.id.list_item); 
    adapter = new ObstacleListAdapter(this, obstacleList); 
    listView.setAdapter(adapter); 
} 


@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
    mMap.setMyLocationEnabled(true); 
    mMap.getUiSettings().setZoomControlsEnabled(true); 

    obstacleList = dbOperations.listAllObstacles(); 
    for(int i=0;i<obstacleList.size();i++){ 
     LatLng marker = new LatLng(obstacleList.get(i).getLatitude(), obstacleList.get(i).getLongitude()); 
     mMap.addMarker(new MarkerOptions().position(marker).title(obstacleList.get(i).getName())); 
    } 

    mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 

     @Override 
     public void onMyLocationChange(Location location) { 

      //mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude()))); 
      //CameraUpdate center = 
      //  CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 20); 

      //mMap.moveCamera(center); 
      MY_LOC = new LatLng(location.getLatitude(), location.getLongitude()); 
      obstacleList.clear(); 
      obstacleList = dbOperations.listAllObstacles(); 
      adapter.notifyDataSetChanged(); 

     } 
    }); 

    setMapIfNeeded(); 

} 
+0

當您登錄getICount(),有多少項目是有 –

+0

請上傳您的DatabaseOperations類文件..我想你在邏輯上有一些問題,爲GE?整個數據。 –

+0

通過替換'convertView = inflater.inflate(R.layout.obstacle_list_row,null);'適配器中的這一行用'convertView = inflater.inflate(R.layout.obstacle_list_row,parent,false)來試試你的運氣;' –

回答

1

問題就出在你的活動有:

obstacleList.clear(); 
obstacleList = dbOperations.listAllObstacles(); 

你基本上改變obstacleList到這個新的列表(不會通過傳播到列表中的適配器

相反,你應該做的是:

obstacleList.clear() 
obstacleList.addAll(dbOperations.listAllObstacles()); 
+0

上面的for循環同樣存在問題。 –

+0

感謝人幫助了很多! –

-1

您的擴展類更改ArrayAdapter<Obstacle>

+0

OP實現了BaseAdapter的所有方法。這絕不是一個答案,它是一個建議,可以作爲評論添加。 –

相關問題