在progressBar中显示BroadcastReceiver数据

时间:2014-05-14 14:34:31

标签: android listview broadcastreceiver android-progressbar

我试图显示wifi数据。好吧,我从broadcastReceiver获取数据,然后我尝试在listView中显示它,并在progressBar中显示wifi信号Strength。每次在onReceive方法中都有新数据时,必须更新列表和progressBare。

但是progressBar只是在第一个项目中工作,就像在这张图片中一样。

enter image description here

这是我使用的活动:

    public class WiFiScanResult extends ActionBarActivity {
    ListView list1 ;

    Receiver receiver = new Receiver();

    List<ScanResult> results ;
     private ProgressBar progressBar;
     int channelNumber;
     private Handler mHandler = new Handler();
        WifiManager wifi;
        Button      enab;
        String resultsString ; 
        String[] myStringArray;
        int aa = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wi_fi_scan_result);
        ConnectivityManager cxMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        list1 = (ListView) findViewById(R.id.listWifi);


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


    }

    public class Receiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("onReceive","onReceive");


        if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){
             results = wifi.getScanResults();

         }
        MonAdaptateurDeListe adaptateur = new MonAdaptateurDeListe();

         list1.setAdapter(adaptateur); 
    }
    }

    private class MonAdaptateurDeListe extends ArrayAdapter<ScanResult> {
        int pos ;
        TextView text;
        public MonAdaptateurDeListe(){
            super(WiFiScanResult.this,R.layout.item_layout,results);
        }


        @SuppressLint("DefaultLocale")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;

            if (itemView == null){
                itemView = getLayoutInflater().inflate(R.layout.item_layout, parent, false);
            }


            progressBar = (ProgressBar) findViewById(R.id.progressBar1);
            ImageView imageView = (ImageView)itemView.findViewById(R.id.imageView1);

            TextView textView4 = (TextView) itemView.findViewById(R.id.level);
            imageView.setImageResource(R.drawable.network);

                    if (results != null)
                    { 
                        pos = position;
                        text = textView4 ;
                        text.setText(results.get(pos).level+"dBm");

                        Handler progressBarHandler = new Handler();



                        progressBarHandler .post(new Runnable() {

                              public void run() {
                                  progressBar.setProgress(10);
                              }
                        });

                        TextView textView1 = (TextView) itemView.findViewById(R.id.SSIDBssid);
            textView1.setText( results.get(position).SSID +"(" + results.get(position).BSSID +")");



            }

            return itemView;
        }

    }

    //We unregister the broadcastreceiver quand on termine
            @Override
            protected void onDestroy (){//à la sortie de l'app on annule l'enregistrement
                super.onDestroy();
                if (receiver != null) {
                      Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT).show();
                      WiFiScanResult.this.unregisterReceiver(receiver);

                }

            }

}

PS:当我把这个代码放在onReceive方法上时,我得到的结果如图所示,

MonAdaptateurDeListe adaptateur = new MonAdaptateurDeListe();

         list1.setAdapter(adaptateur); 

但是当我把它放在onCreate方法中时,我得到一个例外:

 05-14 15:58:26.232: E/AndroidRuntime(21044): FATAL EXCEPTION: main
05-14 15:58:26.232: E/AndroidRuntime(21044): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.debitdistance/com.example.debitdistance.WiFiScanResult}: java.lang.NullPointerException
05-14 15:58:26.232: E/AndroidRuntime(21044):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at android.os.Handler.dispatchMessage(Handler.java:107)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at android.os.Looper.loop(Looper.java:194)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at android.app.ActivityThread.main(ActivityThread.java:5371)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at java.lang.reflect.Method.invokeNative(Native Method)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at java.lang.reflect.Method.invoke(Method.java:525)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at dalvik.system.NativeStart.main(Native Method)
05-14 15:58:26.232: E/AndroidRuntime(21044): Caused by: java.lang.NullPointerException
05-14 15:58:26.232: E/AndroidRuntime(21044):    at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at android.widget.ListView.setAdapter(ListView.java:466)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at com.example.debitdistance.WiFiScanResult.onCreate(WiFiScanResult.java:67)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at android.app.Activity.performCreate(Activity.java:5122)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-14 15:58:26.232: E/AndroidRuntime(21044):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
05-14 15:58:26.232: E/AndroidRuntime(21044):    ... 11 more

关于哪一点:list1.setAdapter(adaptateur);

1 个答案:

答案 0 :(得分:0)

看起来你的结果在&#34; MonAdaptateurDeListe&#34;中是空的。如果你把它放在创作中。

在创建时将列表(新的Arraylist)传递给适配器,然后在接收时设置列表。

还有更多关于设计说明。你的&#34; MonAdaptateurDeListe&#34;我非常了解它非常紧密的活动。尝试制作你的&#34; MonAdaptateurDeListe&#34;独立于使用它的活动。

感谢