-3

i need to get object list view in broadcast receiver class... but if i write listView = (ListView) findViewById(R.id.listView1); i get error.

Any ideas?

THANKS in advance everybody!

I hope that you can help me!

public class AlarmReceiver extends BroadcastReceiver {


    private Intent intent;
    String value1 = null;
    String value2 = null;
    ListView lv;
    ArrayList<String> al;
    ArrayAdapter<String> aa;
    ArrayList<Sms> dataSms;
    public static CustomAdapter adapter;
    ListView listView;

 /*   @Override
    protected void onCreate(final Bundle savedInstanceState) {

        lv = (ListView) findViewById(R.id.listView1);

    }
*/
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

      //  TextView textView = (TextView) ((Activity) context).findViewById(R.id.textView1);
         listView = (ListView)findViewById(R.id.listView1);
}
Lo_Zio
  • 15
  • 7
  • post your code and error log – an_droid_dev Jan 20 '17 at 13:44
  • "Error:(39, 31) error: cannot find symbol method findViewById(int)" – Lo_Zio Jan 20 '17 at 13:45
  • Please post the code which causes the problem. Be sure to include the class and method declarations as well as all relevant variable declarations. – Code-Apprentice Jan 20 '17 at 13:47
  • Don't update listView data in broadcast receiver class. Create one public static method in MainActivity class and call that method in broadcast receiver class. In that method, execute your code on UI thread. Refer this link http://stackoverflow.com/questions/22869928/android-broadcastreceiver-onreceive-update-textview-in-mainactivity – Mayur Gangurde Jan 20 '17 at 13:49
  • It depends on BroadcastReceiver ... you need to make inner non static class of Activity/Fragment itself and don't forget to unregiser it in right lifecycle event – Selvin Jan 20 '17 at 14:15
  • so i must use broadcast receiver as inner class of my main activity? – Lo_Zio Jan 20 '17 at 14:20
  • So you show some code ... now it's clear that's FUBAR .... you should not have any UI elements inside BroadcastReceiver ... also if you register BroadcastReceiver inside manifes it lives only for the time of onReceive so having fields inside doesn't make sens at all – Selvin Jan 20 '17 at 14:21
  • *so i must use broadcast receiver as inner class of my main activity?* if you wana change UI from it yes ... but if you need it to be registered in manifest (like on boot/alarm) then you need 2 BroadcastReceivers ... the one pointed in manifest(which would work as proxy) and second one inside the Activity ... the second one may be called view localbroadcastmanager ... you may also use some other subscriber observer pattern/event bus – Selvin Jan 20 '17 at 14:25
  • @mdg5435 now list view delete first item but not send sms.... – Lo_Zio Jan 20 '17 at 14:38
  • Delete/Update listView after smsManager.sendTextMessage(name, null, sms, null, null); code. – Mayur Gangurde Jan 20 '17 at 18:38

2 Answers2

0

make a class variable and initialize it in the onCreate method of your activity. once initialized it can be used from anywhere inside the class...

so:

private ListView lv;
@Override
protected void onCreate(final Bundle savedInstanceState) {
       ...
       lv = (ListView) findViewById(R.id.listView1);
       ...
}

Hope that helps...

Cheers

Fabian Schneider
  • 799
  • 1
  • 13
  • 40
  • i have problem to instance a list view into onReceive() in broadcast receiver class...not in activity – Lo_Zio Jan 20 '17 at 14:15
  • you don't instance a listView in onReceive(). initialize the listview as above and then use it in the onReceive() onReceive(..){ lv.xxxxx;} – Fabian Schneider Jan 20 '17 at 14:20
  • your code misses the following anyway: `super.onCreate(savedInstanceState); setContentView(R.layout.activityName);` right after onCreate and before anything else – Fabian Schneider Jan 20 '17 at 14:49
0

Try:

LayoutInflater mInf = LayoutInflater.from(context);
View view = mInf.inflate(R.layout.activity_favorite_list, null);
ListView listView = view.findViewById(R.id.listView1);