从除活动之外的任何其他地方更新ListView

时间:2014-07-05 17:30:51

标签: android listview

如何从不相关的课程更新列表视图的项目?我在MainActivity中有这个方法,我得到的是非静态方法,不能从静态上下文中引用'。好像我只能从MainActivity修改listview。

基本上我需要在每次startBluetooth()和定期搜索时更新列表。

MainActivity

public class MainActivity extends Activity {
Button butAlwaysDiscoverable, butStartBluetooth, butScanDevices, butFriends;
ListView list;
BluetoothService bluetoothService;
List<String> mArrayDevices = new ArrayList<String>();
ArrayAdapter<String> arrayAdapter;
ArrayList<Friend> friendList = new ArrayList<Friend>();
private BluetoothAdapter mBluetoothAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //========BUTTONS=======
    butStartBluetooth = (Button) findViewById(R.id.butStartBluetooth);
    butAlwaysDiscoverable = (Button) findViewById(R.id.butMakeDiscoverable);
    butScanDevices = (Button) findViewById(R.id.butScanDevices);
    butFriends = (Button) findViewById(R.id.butFriends);

    //=======DB HANDLER======
    final FriendDBHandler dbHandler = new FriendDBHandler(this, null);

    //=========LIST==========
    list = (ListView) findViewById(R.id.myListView);

    //=======BLUETOOTH=======
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    bluetoothService = new BluetoothService(getWindow().getContext());

    mArrayDevices.clear();
    friendList.clear();
    populateList();

    bluetoothService.startPeriodicSearch();

    butFriends.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (Friend friend : dbHandler.getFriends()) {
                Toast.makeText(MainActivity.this, friend.deviceName, Toast.LENGTH_SHORT).show();
            }
        }
    });

    butStartBluetooth.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            bluetoothService.startBluetooth();
        }
    });

    butAlwaysDiscoverable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
            startActivity(discoverableIntent);
        }
    });

    butScanDevices.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            populateList();
            mBluetoothAdapter.startDiscovery();
            // Create a BroadcastReceiver for ACTION_FOUND
            final BroadcastReceiver mReceiver = new BroadcastReceiver() {
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    // When discovery finds a device
                    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                        // Get the BluetoothDevice object from the Intent
                        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        // Add the name and address to an array adapter to show in a ListView
                        mArrayDevices.add(device.getName() + "\n" + device.getAddress());
                        updateList();
                    }
                }
            };
            // Register the BroadcastReceiver
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
        }
    });

    list.setClickable(true);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            Object o = list.getItemAtPosition(position);
            Toast.makeText(MainActivity.this, o.toString(), Toast.LENGTH_SHORT).show();
            Friend friend = new Friend();
            friend.set(o.toString());
            friendList.add(friend);
            dbHandler.addFriend(friend);
        }
    });
}

private void populateList() {
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    // If there are paired devices
    if (pairedDevices.size() > 0) {
        // Loop through paired devices
        for (BluetoothDevice device : pairedDevices) {
            // Add the name and address to an array adapter to show in a ListView
            mArrayDevices.add(device.getName() + "\n" + device.getAddress());
            updateList();
        }
    }
}

static void updateList() {
    arrayAdapter = new ArrayAdapter<String>(
            MainActivity.this,
            android.R.layout.simple_list_item_1,
            mArrayDevices);

    list.setAdapter(arrayAdapter);
}

}

BluetoothService

package com.av.timetogether;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.os.Handler;

/**
 * Created by andrei on 7/5/2014.
 */
public class BluetoothService {
    private static BluetoothAdapter mBluetoothAdapter;
    private static Handler handler;
    private Context con;

    public BluetoothService(Context context) {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        handler = new Handler();
        con = context;
    }

    public static void startBluetooth() {
        MainActivity.updateList();
        if (!mBluetoothAdapter.isEnabled()) {
            mBluetoothAdapter.enable();
        }
    }

    public static void startPeriodicSearch() {
        handler.postDelayed(runnable, 100);
    }

    private static Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if(!mBluetoothAdapter.isDiscovering())
                mBluetoothAdapter.startDiscovery();

            handler.postDelayed(this, 5000);
        }
    };
}

0 个答案:

没有答案