2015-04-01 50 views
1

我正在使用適用於Android的Estimote SDK。我正在嘗試更改官方網站上提供的代碼。我想在ListFragment中顯示檢測到的信標列表(每個信標有一些信息)。反覆更新ListFragment中的列表

在我的主要活動我開始所有必要的(服務,聽衆..),特別是我有這個的onCreate(...)方法:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    beaconManager.setRangingListener(new BeaconManager.RangingListener() { 
     @Override 
     public void onBeaconsDiscovered(Region region, List<Beacon> beacons) { 
      // update the list of the beacons in the ListFragment in some way 
     } 
    }); 
} 

如果我理解正確的話,這種方法是在每個固定時間連續調用。

這是ListFragment的代碼:

public class DiscoveredBeacons extends ListFragment { 

    private List<Beacon> beacons = new ArrayList<Beacon>(); 
    private Beacon beacon; 
    private View mContentView = null; 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     this.setListAdapter(new MyListAdapter(getActivity(), R.layout.beacon_row, beacons)); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     mContentView = inflater.inflate(R.layout.beacons_list, null); 
     return mContentView; 
    } 

    // this does not work if i call it from the main Activity 
    public void updateList(List<Beacon> beacons) { 
     beacons.clear(); 
     beacons.addAll(beacons); 
     ((MyListAdapter) getListAdapter()).notifyDataSetChanged(); 
    } 

    private class MyListAdapter extends ArrayAdapter<Beacon> { 

     private List<Beacon> items; 

     public MyListAdapter(Context context, int textViewResourceId, 
           List<Beacon> items) { 
      super(context, textViewResourceId, items); 
      this.items = items; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      if (v == null) { 
       LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.beacon_row, null); 
      } 
      Beacon beacon = items.get(position); 
      if (beacon != null) { 
       TextView number = (TextView) v.findViewById(R.id.number); 
       TextView rssi = (TextView) v.findViewById(R.id.rssi); 
       TextView power = (TextView) v.findViewById(R.id.power); 
       TextView distance = (TextView) v.findViewById(R.id.distance); 
       if(number != null) { 
        number.setText("Beacon number " + (position + 1)); 
       } 
       if (rssi != null) { 
        rssi.setText("rssi: " + beacon.getRssi()); // int 
       } 
       if (power != null) { 
        power.setText("power: " + beacon.getMeasuredPower()); // int 
       } 
       if(distance != null) { 
        distance.setText("distance: " + Utils.computeAccuracy(beacon)); 
       } 
      } 
      return v; 
     } 
    } 
} 

正如你所看到的,我試圖把一個公共方法在ListFragment,並調用它的裏面的onCreate聽衆,但這不起作用。特別是我得到一個異常:

04-01 20:14:04.029 18052-18052/it.loris.beaconsdetector E/AndroidRuntime:致命異常:主 工藝:it.loris.beaconsdetector,PID:18052 的java .lang.UnsupportedOperationException at java.util.Collections $ UnmodifiableCollection.clear(Collections.java:936) at it.loris.beaconsdetector.DiscoveredBeacons.updateList(DiscoveredBeacons.java:40) at it.loris.beaconsdetector.BeaconsDetector $ 1 .onBeaconsDiscovered(BeaconsDetector.java:41) 在com.estimote.sdk.BeaconManager $ IncomingHandler.handleMessage(BeaconManager.java:479) 在android.os.Handler.dispatchMessage(Handler.java:102)在java.lang.reflect.Method.invoke(Native Method) at android.app.ActivityThread.main(ActivityThread.java:5274) .lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:909) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:704)

謝謝你的幫助。

回答

1

這是一個容易犯的錯誤。您正嘗試清除信標列表,但不清除您從列表中獲取的列表實例。使用「this」在列表片段中訪問您的信標。並添加從方法中收到的信標。這應該解決它。

// this does not work if i call it from the main Activity 
public void updateList(List<Beacon> beacons) { 
    // Call this to refer your variable in List fragment 
    this.beacons.clear(); 
    this.beacons.addAll(beacons); 
    ((MyListAdapter) getListAdapter()).notifyDataSetChanged(); 
} 
+0

OMG,OMG請殺了我。 – user3075478 2015-04-01 18:34:28

+0

好吧,這是令人難以置信的 - 。 - 現在沒關係。就一個問題。這是做到這一點的正確方法嗎? – user3075478 2015-04-01 18:36:50

+0

@ user3075478正確的做法是什麼?更新列表片段中的項目列表? – osayilgan 2015-04-01 18:59:35

0

嘗試增加這getView()

if (convertView == null) { 
      LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
       Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.beacon_row, null); 
     } else { v = convertView; }