summaryrefslogtreecommitdiff
path: root/Defogger/src/no/mork/android/defogger/ScanListAdapter.java
blob: cc1c4e51a5241c71ca2061322cbba511eae7f6d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package no.mork.android.defogger;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;

// originally from https://developer.android.com/guide/topics/ui/layout/recyclerview
// but converted to simpler ArrayAdapter using https://developer.android.com/guide/topics/ui/declaring-layout.html#FillingTheLayout


public class ScanListAdapter extends BaseAdapter {
    private ArrayList<BluetoothDevice> mObjects;
    private Context mCtx;
    private LayoutInflater mInflater;
    private int mRes;
    private int mTxtId;
    
    public ScanListAdapter(Context context, int resource, int textViewResourceId)  {
	mObjects = new ArrayList<BluetoothDevice>();
	mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	mRes = resource;
	mTxtId = textViewResourceId;
    }

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

    @Override
    public BluetoothDevice getItem(int position) {
	return mObjects.get(position);
    }

    @Override
    public long getItemId(int position) {
	return getItem(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
	BluetoothDevice device = getItem(position);

	if (convertView == null) {
	    convertView = mInflater.inflate(mRes, container, false);
	}
	
	((TextView) convertView.findViewById(mTxtId)).setText(device.getAddress() + " - " + device.getName());
	return convertView;
    }

    public void add(BluetoothDevice device) {
	if (device.getName() != null && mObjects.indexOf(device) < 0) { // avoid duplicates and ignore nameless devices
	    mObjects.add(device);
	    notifyDataSetChanged();
	}
    }
}