我得到了这个列表,它发现所有找到的蓝牙设备。和一个适配器应该去并将它们添加到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>
你确认日志是否实际添加数据? –
日志说明列表和适配器都获得项目,但ListView没有得到任何子项。 – Blapple
你可以发布你的ViewPager及其适配器PLZ的代码吗? –