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'的适配器 –