有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android Altbeacon。使用自定义适配器和Listview打印找到的所有信标信息

我想在安卓中使用listview显示UUID和找到的信标的次要代码

我想我对自定义适配器有问题

这是我的实际代码:

找到信标。爪哇

public class FoundBeacon extends Activity implements BeaconConsumer {

private BeaconManager beaconManager;
private ListView listview;
private ArrayList<Beacon> beaconList;
private AdapterBeacon bAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_found_beacon);
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.bind(this);

    beaconList = new ArrayList<Beacon>();
    this.bAdapter = new AdapterBeacon(this, beaconList);

    listview = (ListView) findViewById(R.id.listview);
    listview.setAdapter(bAdapter);
}


@Override
public void onBeaconServiceConnect() {

    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {

            if (beacons.size() > 0) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        bAdapter.copyBeacons(beacons);
                        listview.setAdapter(bAdapter);
                    }
                });
            }
        }

    });
    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {
    }
}
}

适配器Beacon。爪哇

public class AdapterBeacon extends ArrayAdapter<Beacon> {

private Context context;
private ArrayList<Beacon> allBeacons;

private LayoutInflater mInflater;
private boolean mNotifyOnChange = true;

public AdapterBeacon(Context context, ArrayList<Beacon> mBeacons) {
    super(context, R.layout.row_beacon);
    this.context = context;
    this.allBeacons = new ArrayList<Beacon>(mBeacons);
    this.mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return allBeacons .size();
}

@Override
public Beacon getItem(int position) {
    return allBeacons .get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public int getPosition(Beacon item) {
    return allBeacons .indexOf(item);
}

@Override
public int getViewTypeCount() {
    return 1; //Number of types + 1 !!!!!!!!
}

@Override
public int getItemViewType(int position) {
    return 1;
}


public void copyBeacons(Collection<Beacon> beacons)
{
    allBeacons.clear();
    for (Beacon b : beacons)
    {
        allBeacons.add(b);
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    int type = getItemViewType(position);
    if (convertView == null) {
        holder = new ViewHolder();
        switch (type) {
            case 1:
                convertView = mInflater.inflate(R.layout.row_beacon,parent, false);
                holder.name = (TextView) convertView.findViewById(R.id.t1);
                holder.description = (TextView) convertView.findViewById(R.id.t2);
                break;
        }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.name.setText(allBeacons.get(position).getId1().toString());
    holder.description.setText(allBeacons.get(position).getId2().toString());
    holder.pos = position;
    return convertView;
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
    mNotifyOnChange = true;
}

public void setNotifyOnChange(boolean notifyOnChange) {
    mNotifyOnChange = notifyOnChange;
}


//---------------static views for each row-----------//
static class ViewHolder {

    TextView name;
    TextView description;
    int pos; //to store the position of the item within the list
}
}

活动找到了灯塔。xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/activity_found_beacon"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.pio.pd_inz_ibeacon.FoundBeacon">

<ListView
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:layout_alignParentTop="true"
    安卓:layout_alignParentStart="true"
    安卓:id="@+id/listview" />

路标。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" >

<TextView
    安卓:id="@+id/t1"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="text"/>

<TextView
    安卓:id="@+id/t2"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="text"/>

<TextView
    安卓:id="@+id/t3"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="text"/>


共 (0) 个答案