0

我得到了這個列表,它發現所有找到的藍牙設備。和一個適配器應該去並將它們添加到ListView這是在Fragment裏面ViewPager。列表本身和適配器會更新,但ListView不會更新。
爲了測試我使用的代碼,我做了一個新的項目,它完美地工作。 所以我的問題是,任何人都可以指出爲什麼我的ListView在ViewPager裏面沒有更新,但是它外面的ListView有效嗎?CustomAdapter ListView沒有更新

這裏是我的代碼:
變量賦值

setContentView(R.layout.fragment_settings); 
mList = (ListView) findViewById(R.id.device_list); 
... 
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
adapter = new BluetoothArrayAdapter(this, R.layout.device_layout, mDevices); 
mList.setEmptyView(mEmpty); 
mList.setAdapter(adapter); 

方法被調用,以填補適配器

@Override 
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { 
     .... 
     mDevices.add(device); 
     mList.invalidate(); 
     adapter.notifyDataSetChanged(); 
     .... 
    } 
} 

BluetoothArrayAdapter:

public class BluetoothArrayAdapter extends ArrayAdapter<BluetoothDevice> { 

    public BluetoothArrayAdapter(Context context, int resource, ArrayList<BluetoothDevice> values) { 
     super(context, resource, values); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     super.getView(position, convertView, parent); 
     Log.i("Adapter", "Inside getview"); 
     View v = convertView; 

     if (v == null) { 
      LayoutInflater vi; 
      vi = LayoutInflater.from(getContext()); 
      v = vi.inflate(R.layout.device_layout, null); 
     } 

     BluetoothDevice p = getItem(position); 

     if (p != null) { 
      TextView deviceName = (TextView) v.findViewById(R.id.device_name); 
      TextView deviceAddress = (TextView) v.findViewById(R.id.device_address); 

      if(deviceName != null) { 
       deviceName.setText(p.getName()); 
      } 
      if(deviceAddress != null) 
       deviceAddress.setText(p.getAddress()); 

     } 
     return v; 
    } 
} 

ViewPager:

public class SectionsViewPager extends ViewPager {  
    public SectionsViewPager(Context context) { 
     super(context); 
    } 
    public SectionsViewPager(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) { 
     if(v != this && v instanceof ViewPager) { return true; } 
     return super.canScroll(v, checkV, dx, x, y); 
    } 
} 

ViewPagerAdapter:

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a MainFragment (defined as a static inner class below). 
     return MainFragment.newInstance(position + 1); 
    } 

    @Override 
    public int getCount() { 
     // Show amount of pages. 
     return 4; 
    } 

    @Override 
    public int getItemPosition(Object object) { 
     return POSITION_NONE; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return "SECTION " + position; 
    } 
} 

而且我的佈局:
device_layout:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
android:id="@+id/device_empty" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="?android:attr/listPreferredItemHeight" 
android:padding="6dip"> 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginStart="6dip" 
    android:layout_marginEnd="6dip" 
    android:contentDescription="Bluetooth device" 
    android:src="@drawable/remote_icon_transparent"/> 

<TextView 
    android:id="@+id/device_address" 
    android:layout_width="fill_parent" 
    android:layout_height="26dip" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_toRightOf="@id/icon" 
    android:ellipsize="marquee" 
    android:singleLine="true" 
    android:text="MAC address" 
    android:textSize="12sp" /> 

<TextView 
    android:id="@+id/device_name" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/device_address" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignWithParentIfMissing="true" 
    android:layout_toRightOf="@id/icon" 
    android:gravity="center_vertical" 
    android:text="Name" 
    android:textSize="16sp" /> 

<View style="@style/Divider" 
    android:layout_below="@+id/icon" 
    android:layout_alignParentStart="true" /> 

</RelativeLayout> 

fragment_setting:

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <ListView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/device_list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
    <TextView android:id="@id/android:empty" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:background="@color/colorPrimaryDark" 
     android:textColor="@color/colorWhite" 
     android:text="No Devices"/> 
</LinearLayout> 
+0

你確認日誌是否實際添加數據? –

+0

日誌說明列表和適配器都獲得項目,但ListView沒有得到任何子項。 – Blapple

+0

你可以發佈你的ViewPager及其適配器PLZ的代碼嗎? –

回答

0

我想出了自己。 我的ListView未更新的原因是因爲我在我的主Activity中設置了它的適配器,我應該在我的Fragment中調用它。
當我把代碼放在那裏時,我的ListView就像它應該更新一樣。

0

您的設置適配器後,加入這行太代碼: adapter.notifyDataSetChanged();

+0

我已經調用過。我在調用的方法中調用它以填充適配器 – Blapple

0

關於你對列表視圖沒有更新我曾經遇到過同樣的問題,但有不同的適配器的問題......解決我是要調用的更新方法或在的onResume()方法執行列表視圖更新其你可以使用ovveride,因爲oncreate被執行一次,因此你的列表視圖只能工作一次,因此請嘗試在你的activity的onResume()方法中使用你的代碼。希望能幫助到你。

+0

我嘗試添加在onResume中設置適配器的代碼,但同樣的事情仍然發生。我應該把其他代碼放在那裏嗎? – Blapple

+0

是嘗試在更改後在您的活動中調用onResume() –

0

好吧,我明白了。要使ViewPager中的片段重繪,請使用FragmentStatePagerAdapter而不是FragmentPagerAdapter。希望它有幫助:)

+0

您是否在此適配器上調用了'notifyDataSetChange'?你必須調用'ListView'的適配器和'ViewPager'的適配器 –