Brauche Hilfe Set<String>

  • Antworten:2
Eugen T
  • Forum-Beiträge: 27

01.11.2013, 17:37:55 via Website

Hallo Leute.

Ich möchte einen Set verwenden und in eine ListView einfügen.
Die Frage ist wie setze ich in das ListView.Ich möchte es nicht in ein Adapter einfügen also so wie hier in dem code sonst kommen duplicates vor.
Den Set möchte direkt ins ListView einfügen.
Bitte um einen Lösungsvorschlag.
Danke Vorraus.

1for(String one : foundDevicesSet){
2 deviceNameAdapter.add(one);
3 }

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

01.11.2013, 19:26:39 via Website

Was willst du so machen?
1. poste mal den kompletten code
2. So macht man keine For schleife
Mach dann lieber
1foreach(String one in foundDevicesSet)
2{
3deviceNameAdapter.add(one);
4}

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Eugen T
  • Forum-Beiträge: 27

01.11.2013, 20:00:14 via Website

1public class MainActivity extends Activity {
2
3 ToggleButton tBtn;
4 BluetoothAdapter theBluetoothAdapter;
5 Set<BluetoothDevice> pairedDevices;
6 ArrayAdapter<String> deviceNameAdapter ;
7 ListView DeviceList;
8 TextView bluetoohOff;
9 TextView availableDivce;
10 Button searchButton;
11 Set<String> foundDevicesSet;
12 BroadcastReceiver receiver;
13 private static final String TAG =" myLOG";
14
15
16 @Override
17 protected void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.main);
20
21 Init();
22 setAdapter();
23 textVisibilitys();
24 BluetoothEnabled();
25 discoverDevices();
26 devicesList();
27
28
29
30 }
31
32 public void Init(){
33
34 foundDevicesSet = new HashSet<String>();
35 tBtn = (ToggleButton)findViewById(R.id.toggleButton1);
36 availableDivce = (TextView)findViewById(R.id.textView2);
37 bluetoohOff =(TextView)findViewById(R.id.textView1);
38 DeviceList = (ListView)findViewById(R.id.listView1);
39 searchButton =(Button)findViewById(R.id.button1);
40 deviceNameAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);
41 DeviceList.setAdapter(deviceNameAdapter);
42 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
43 registerReceiver(receiver, filter);
44 receiver = new BroadcastReceiver() {
45
46 @Override
47 public void onReceive(Context context, Intent intent) {
48 // TODO Auto-generated method stub
49
50 String action = intent.getAction();
51
52 if (BluetoothDevice.ACTION_FOUND.equals(action)) {
53 BluetoothDevice newDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
54 if (newDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
55 foundDevicesSet.add(newDevice.getName() + "|" + newDevice.getAddress());
56
57 for(String one : foundDevicesSet){
58 deviceNameAdapter.add(one);
59 }
60
61
62 }
63 }
64 }
65 };
66
67
68
69 }

Antworten