Android arrayadapter without specifying object

nmxprime

i want to use listview with ArrayAdapter

After scanning for WiFi, the code for broadcast receiver is as below

public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        scanResultDevices.notifyDataSetInvalidated();
        wifiResults = wm.getScanResults();
        scanResultDevices = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);
        scanResultAdapter = new ArrayAdapter<ScanResult>(getApplicationContext(),android.R.layout.simple_list_item_1);
        Log.e("inside","BD REceiver");
        if(wifiResults != null){
            for(ScanResult s : wifiResults){
                scanResultAdapter.add(s);
                scanResultDevices.add(s.SSID);
            }
        scanResult.setAdapter(scanResultDevices);
        scanResultDevices.notifyDataSetChanged();
        }
        unregisterReceiver(scanComp);
    }

But this shows null pointer exception at scanResult.setAdapter(scanResultDevices)

I know how to use new ArrayAdapter<T>(context, textViewResourceId, objects). But in this specific case i want to try new ArrayAdapter<T>(context, textViewResourceId). If what i am trying is not possible, then how can i do it correctly?

EDIT:

Updated code

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    View v = findViewById(R.id.dialogtitle);
    scanResult = (ListView) findViewById(R.id.scanResult);

    //scanResult.setAdapter(scanResultDevices);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Scan For Device").setCustomTitle(v).setPositiveButton("SCan",this).create();
    builder.show();
}

The code for onClick:

@Override
public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    wm = (WifiManager) getSystemService(WIFI_SERVICE);
    if(!wm.isWifiEnabled()){
        wm.setWifiEnabled(true);
        Toast.makeText(getApplicationContext(), "Turning on Wifi", Toast.LENGTH_SHORT);
        Log.e("Wifi","Not-on!Enabling");
    }
    MyDialogFragment alert = new MyDialogFragment();
    alert.show(getFragmentManager(), "SCAN_DIALOG");
    wm.startScan();

    registerReceiver(scanComp, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));


//  if(wifiResults.isEmpty())

}
Adam Radomski

It is possible simply:

ArrayAdapter<YOUR_CLASS> name =   new ArrayAdapter<YOUR_CLASS>(context, textViewResourceId);

And then in api above level 11

name.addAll(YOUR_OBJECTS_COLLECTION);

or below

 for(Class item_name: collection){
 name.add(item_name);
 }

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

You should init scanResult in onReceive method

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Declare object of derived class without specifying it

assigning value from object without specifying the index

Android - Forming an URL object specifying a port number

Jackson databind - how to deserialize an object without specifying the target class?

Update JSONB object in array without specifying index in PostgreSQL

How to declare an object of generic abstract class without specifying template type

How to update an entire object using jdbcTemplate without specifying column names?

Entity Framework, cannot add new object without specifying primary key

Subseting multiple object in R without specifying all of them

Android using custom object with ArrayAdapter, and communicating back views

Adding an object stored in a variable to another object without specifying a key is using the variable name as the key. Is this expected behavior?

When you make an ArrayList without specifying the object type, does it create it automatically when you add the first object?

mysqldump without specifying location

Specifying a range within a workbook without specifying the sheet

Specifying class of object in clojure

ArrayAdapter in Fragment - Android

android - There is no default constructor for ArrayAdapter

Extending ArrayAdapter in android

Android ListView and ArrayAdapter/Arraylist

Spinner and ArrayAdapter NullPointerException in Android

ArrayAdapter use Kotlin android

ArrayAdapter initialization error (Android)

CheckBox and TextView in ArrayAdapter in Android

Android ArrayAdapter NullPointerException getID

Android textview swipe with ArrayAdapter

android UnsupportedOperationException in ArrayAdapter<String>

Custom ArrayAdapter for a ListView (Android)

Why can we access window.document property without specifying window object?

How to insert an object into PostGreSql using Dapper Extensions without specifying primary key explicitly?