listView没有更新

时间:2017-01-09 04:47:34

标签: android listview

此对话框的列表视图未更新。在关于此问题的大多数其他stackoverflow帖子中,问题是适配器的关联数组更改引用,但我确保在此处不这样做。是否有无法更新的另一种解释?

  /**
     * This class represents a dialog fragment that appears on clicking the connect button of
     * the main activity. The fragment displays a list of all discovered bluetooth devices.
     */
public class DiscoveredDialogFragment extends DialogFragment {


    public static DiscoveredDialogFragment newInstance() {
        return new DiscoveredDialogFragment();
    }

    public interface DiscoveredDialogListener {
        public void onDeviceSelectedForConnection(String addressMac);

        public void onScanClicked();
    }

    @BindView(R.id.listview)
    ListView mListView;
    private SimpleAdapter mAdapter;
    private ArrayList<HashMap<String, String>> mListDevice;
    private DiscoveredDialogListener mDiscoveredDialogListener;

    /**
     * Display the view on on create.
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_fragment_devices_discovered, null);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
        getDialog().setCanceledOnTouchOutside(false);
        ButterKnife.bind(this, view);
        return view;
    }

    /**
     * On activity being created, connect mListDevice arraylist with mListView. This will display
     * the Name and MacAddress on each line of the listview.
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mListDevice = new ArrayList<HashMap<String, String>>();
        mAdapter = new SimpleAdapter(this.getActivity(), mListDevice,
                android.R.layout.two_line_list_item,
                new String[]{"Name", "AddressMac"},
                new int[]{android.R.id.text1, android.R.id.text2});
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                //create a new bluetooth client with the associated macaddress, and give this client
                //its own thread
                mDiscoveredDialogListener.onDeviceSelectedForConnection(mListDevice.get(position).get("AddressMac"));
                dismiss();
            }
        });
    }

    public void setListener(DiscoveredDialogListener discoveredDialogListener) {
        mDiscoveredDialogListener = discoveredDialogListener;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        Log.e("EventBus", "registered");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * On clicking scan, discover all discoverable bluetooth devices
     */
    @OnClick(R.id.scan)
    public void scan() {
        mDiscoveredDialogListener.onScanClicked();
    }

    /**
     * Update mListDevice with new bluetooth devices and their associated information.
     * @param device
     */
    @Subscribe
    public void onEventMainThread(BluetoothDevice device) {
        Log.e("BluetoothAdapter", "found a device");
        HashMap<String, String> item = new HashMap<String, String>();
        item.put("Name", device.getName());
        item.put("AddressMac", device.getAddress());
        if(!mListDevice.contains(item)){
            mListDevice.add(item);
        }

        mAdapter.notifyDataSetChanged();
    }

修改

事实证明一切正常,但listview在白色背景上用白色文字写。

2 个答案:

答案 0 :(得分:0)

SimpleAdapater不适用于更改的数据;它只处理静态数据。因此,如果您想更新listview,那么每次都应该像这样设置Adapter

@Subscribe
public void onEventMainThread(BluetoothDevice device) {

    HashMap<String, String> item = new HashMap<String, String>();
    item.put("Name", device.getName());
    item.put("AddressMac", device.getAddress());
    if(!mListDevice.contains(item)){
        mListDevice.add(item);
    }
    mAdapter = new SimpleAdapter(this.getActivity(), mListDevice,
            android.R.layout.two_line_list_item,
            new String[]{"Name", "AddressMac"},
            new int[]{android.R.id.text1, android.R.id.text2});
    mListView.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();
}

注意由于您的数据是动态的,请使用CustomAdapter来提高您的列表浏览效果

答案 1 :(得分:0)

适配器

SimpleAdapter
        {

        mListDevice = new ArrayList<HashMap<String, String>>();
        ...


    // adapter method to call
        public void updateData(ArrayList<HashMap<String,String>> mListDevice )
        {
        this.mListDevice =mListDevice ;
        notifyDataSetChanged();
        }

        }

从Fragment / activity

调用方法
 HashMap<String, String> item = new HashMap<String, String>();
    item.put("Name", device.getName());
    item.put("AddressMac", device.getAddress());
mListDevice.add(item);
mAdapter.updateData(mListDevice);