CustomAdapter ListView未更新

时间:2016-03-04 09:11:07

标签: android listview android-fragments bluetooth android-viewpager

我得到了这个列表,它可以在发现蓝牙设备时抓取它们。还有一个适配器应该将它们添加到位于ListView内的Fragment内的ViewPager。列表本身和适配器会更新,但ListView不会更新 为了测试我使用的代码,我创建了一个新项目,它完美地运行。 所以我的问题是,任何人都可以指出为什么ViewPager中的ListView不会更新,但其外部的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>

4 个答案:

答案 0 :(得分:0)

在设置适配器后添加此行代码: adapter.notifyDataSetChanged();

答案 1 :(得分:0)

关于你的列表视图没有更新的问题我遇到了同样的问题但是使用了不同的适配器...我的解决方法是调用更新或执行onResume()方法的列表视图更新的方法因为oncreate执行一次因此你的列表视图只能工作一次,所以尝试在你的活动的onResume()方法中使用你的代码。希望它有所帮助。

答案 2 :(得分:0)

好的我明白了。要使ViewPager中的片段重绘,请使用FragmentStatePagerAdapter而不是FragmentPagerAdapter。希望它有所帮助:)

答案 3 :(得分:0)

我自己想通了。 我的ListView未更新的原因是因为我在主Activity中设置了适配器,我应该在我的Fragment中调用它。
当我把代码放在那里时,ListView就像它应该更新了。