开发者

Design question: how to do "UserControls" in Android dev? Trying to avoid one large .java file with all code in it

Coming from a Visual Studio/WinForms background, I face the problem of how to avoid placing all code in one large .java-file when developing for Android.

In WinForms its very easy to create a new UserControl that contains both layout and code to handle a certain task. This way the code doesnt have to be places in one large file, but can be "modularized" of sorts.

How to achieve the same thing in Android dev? As an example, I have my MainActivity.java that all code is contained in. Now, I want to scan for Bluetooth devices. A popup should appear during scan and whenever a device is found, I want a list (in the popup) to be populated with the info of the BT device.

So, from the click of a Button I can do .startDiscovery(), but I really want all of that code to be elsewhere, not in my main .java file. It should be separate, including all event listeners. None of that (hooking events etc) sh开发者_运维百科ould be in MainActivity, it should all be placed in the "userControl" (and as you probably understand, there are many good reasons for that)

What is the best approach for this? Am i going abouit this the correct way? ** EDIT 2011-09-05 **

So I added a class, BluetoothTest.java, to handle everything as described in this post. However, its not working very well. The popup doesnt show. Am I doing this correctly?

package com.sdkexample;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

public class BluetoothTest {

    private Context ct;
    private View parentView;
    public BluetoothTest(Context ct, View parentView)
    {
        this.ct = ct;
        this.parentView = parentView;
    }

    public void start()
    {
        LayoutInflater mInflater = (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View btView = mInflater.inflate(R.layout.popup_bluetoothsearch, null);
        final ListView lv = (ListView)btView.findViewById(R.id.listViewBtDevices);
        lv.setAdapter(new ArrayAdapter<String>(ct, R.id.twoLineListItem1));
        PopupWindow pw = new PopupWindow(btView);
        pw.showAtLocation(parentView, Gravity.CENTER, 100, 100);

        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            // Device does not support Bluetooth
        }
        else
        {
            if (!mBluetoothAdapter.isEnabled())
                mBluetoothAdapter.enable();

            final BroadcastReceiver mReceiver = new BroadcastReceiver() {
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();

                    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                        // 
                        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                        ArrayAdapter aa = (ArrayAdapter)lv.getAdapter();
                        aa.add(device.toString());

                        if (device.getName()== "DPP-350")
                        {
                            // ***
                            // HERE I WANT TO ADD THE DEVICE TO MY LIST IN MY POPUP!
                            // ***
                        }
                    }
                }
            };
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            ct.registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

            mBluetoothAdapter.startDiscovery();
        }
    }

}

--- EDIT ---

This page describes some stuff. My problem with that post is that, like I described above, the code is all contained in the "parent"; it is not contained in a form of UserControl.

The "parent" has all the references for the elements in his popup (it saves for example the TextView as mResultText), another unwanted thing. The point is that the popup should be "independent", so that it can be used from any Activity. I should have to implement separate code for each and every popup I want to use

---

Here is the code I want to use for discovery:

private void SearchPrinter()
{
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        // Device does not support Bluetooth
    }
    else
    {
        final BroadcastReceiver mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();

                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    // 
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                    if (device.getName()== "DPP-350")
                    {
                        // ***
                        // HERE I WANT TO ADD THE DEVICE TO MY LIST IN MY POPUP!
                        // ***
                    }
                }
            }
        };
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

        mBluetoothAdapter.startDiscovery();
    }

}


in your activity create an instance of a class.The class constructor will do all the needed operations SearchPrinter() startDiscovery etc. I can't think of another way seperating your code.

UPDATE

From this other class you can have access to all the GUI elements. You can do that by passing a Context reference (getApplicationContext()) or the widget itself.


This shouldn't be a problem at all. Android is based on Java which is an OOP language by design. You need to create separate class to represent the BluetoothListPopUp. In that class you could add all the relevant methods related to the pop up such as addToList() that you will call from MainActivity.java

The steps are:

  1. Create a class BluetoothListPopUp as your PopUp (probably extension of PopUpWindow)
  2. Also create the layout for this popup in separate xml file
  3. Add all the methods specific to BluetoothListPopUp including one to add the list
  4. Instantiate this class in your MainActivity.java
  5. Upon onReceive(), you call the method in BluetoothListPopup to add the device to the list.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜