使用android中的信标更新listview,将信标放入arraylist

时间:2016-04-10 18:48:24

标签: android listview altbeacon beacon

请帮助,我可以找到一个信标的距离,但我不能用该地区的其他信标填充列表视图。我通过使用字符串数组测试了listview,填充正常。其他一切都有效,但我最后一部分完全失去了,接近这边缘......

这是我的主要活动的代码.import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.widget.*;
import org.altbeacon.beacon.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class MainActivity extends Activity implements BeaconConsumer{



    private BeaconManager beaconManager;
    int distance;
    Identifier minorID;
    double distanceLong;
    private Handler mHandler;
    private int i;
    public TextView distanceText;
    public TextView distanceName1;
    public ImageView distanceImage;
    public String name1;
    public boolean exitRegion = false;
    public String minorString;
    List<String> beaconList;

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

        Intent intent = getIntent();

         name1 = intent.getStringExtra("beacon1");


        beaconManager = BeaconManager.getInstanceForApplication(this);

        beaconManager.getBeaconParsers().add(new BeaconParser()
                .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));

        beaconManager.bind(this);


        distanceText = (TextView) findViewById(R.id.distanceText);
        distanceText.setText("No beacons found yet, please wait");






            distanceName1 = (TextView) findViewById(R.id.name1);
            distanceName1.getText();
            distanceName1.setText("How far away is "+name1+"?");



        distanceText = (TextView) findViewById(R.id.distanceText);
        distanceText.getText();
        distanceText.setText("Distance: " + distance+ " metres");
        distanceImage = (ImageView) findViewById(R.id.distanceImage);
        //alertDialogue();

        i = 0;
        mHandler = new Handler() {
            @Override
            public void close() {

            }

            @Override
            public void flush() {

            }

            @Override
            public void publish(LogRecord record) {

            }
        };




        beaconList = new ArrayList<String>();
        ListAdapter beaconAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, beaconList);
        ListView beaconListView = (ListView) findViewById(R.id.beaconListView);
        beaconListView.setAdapter(beaconAdapter);


        distanceText.post(mUpdate);
        distanceImage.post(mUpdate);


    }





    private Runnable mUpdate = new Runnable() {
        public void run() {



            distanceText.setText("Distance: " + distance+ " ms");
            imageDistance(distance);
            alertDialogue(distance);
            i++;
            distanceText.postDelayed(this, 10000);


        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        beaconManager.unbind(this);
    }

    @Override
    public void onBeaconServiceConnect() {
        final Region region = new Region("myBeacons", Identifier.parse("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), null, null);

        beaconManager.setMonitorNotifier(new MonitorNotifier() {
            @Override
            public void didEnterRegion(Region region) {
                try {
                    Log.d(TAG, "didEnterRegion");
                    exitRegion=false;
                    beaconManager.startRangingBeaconsInRegion(region);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void didExitRegion(Region region) {
                try {
                    Log.d(TAG, "didExitRegion");
                    exitRegion = true;
                    beaconManager.stopRangingBeaconsInRegion(region);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void didDetermineStateForRegion(int i, Region region) {

            }
        });

        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                for(Beacon oneBeacon : beacons) {


                    distanceLong = oneBeacon.getDistance();
                    distance = (int) distanceLong;
                    minorID = oneBeacon.getId3();
                    minorString = minorID.toString();
                    String distanceString = String.valueOf("Distance of " + minorID +" beacon: "+ (int) distanceLong+ " metres");
                    beaconList.add(distanceString);

                    Beacon.setHardwareEqualityEnforced(true);

                }


            }
        });

        try {
            beaconManager.startMonitoringBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

`

我的布局代码就在这里,正如我所说的UI已经大部分已经可以使用但是我可能已经错过了listview ...

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"

            android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
              android:orientation="vertical"

    tools:context="${relativePackage}.${activityClass}">



    <TextView
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:textColor="@color/material_blue_grey_800"
                android:textSize="25sp"
                android:id="@+id/name1"
                android:layout_gravity="center_horizontal"
                android:layout_margin="10dp"

    />


        <TextView android:layout_width="wrap_content"
                  android:layout_height="60dp"
                  android:id="@+id/distanceText"
                  android:textColor="@color/material_blue_grey_800"
                  android:textSize="25sp"
                  android:layout_margin="10dp"
                  android:layout_gravity="center_horizontal"
                  android:layout_below="@+id/name1"

    />
        <ImageView android:layout_width="150dp"
                       android:layout_height="150dp"
                       android:id="@+id/distanceImage"
                       android:layout_margin="10dp"
                       android:layout_below="@+id/distanceText"
                       android:layout_gravity="center_horizontal"
        />

        <ListView android:layout_width="match_parent"
                  android:layout_height="match_parent"
                    android:id="@+id/beaconListView"
                  android:textColor="@color/material_blue_grey_800"
                  android:textSize="25sp"
        ></ListView>


</LinearLayout></ScrollView>

1 个答案:

答案 0 :(得分:0)

一些提示:

  • 您不需要单独的beaconList,只需要beaconAdapter。但是你确实需要将beaconAdapter的声明移动为类级别变量,如下所示:

    ...
    public String minorString;
    private ListAdapter beaconAdapter;
    ...
    
  • 然后你可以在现在的位置初始化它,只需删除声明部分。您还可以删除对beaconList的引用,这是不需要的:

    beaconAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    
  • didRangeBeaconsInRegion回调中,您可以更新beaconAdapter中的条目。像这样:

    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        beaconAdapter.clear();
        for(Beacon oneBeacon : beacons) {
            distanceLong = oneBeacon.getDistance();
            distance = (int) distanceLong;
            minorID = oneBeacon.getId3();
            minorString = minorID.toString();
            String distanceString = String.valueOf("Distance of " + minorID +" beacon: "+ (int) distanceLong+ " metres");
            beaconAdapter.add(distanceString);
            Beacon.setHardwareEqualityEnforced(true);
        }
        beaconAdapter.notifyDataSetChanged()            
    }
    

请注意,在上面的代码中,notifyDataSetChanged()被称为@ niels-masdorp正确地说他的回答。