2014-03-27 3 views
1

내 Wi-Fi를 따르기 위해 브로드 캐스트 수신기를 만들었습니다. LogCat에서 normaly 정보를 얻었지만 ListView에 데이터를 넣을 수 없습니다. 는 내가 onReceive 방법을 테스트하는리스트 뷰 예제를 넣어하지만 난 작동하지 않았다, 나는이 오류가 있어요 :Android : ListView의 BroadcastReceiver 표시

public class Wifi extends Activity implements OnClickListener{ 
/** Called when the activity is first created. */ 
WifiManager wifi; 
Button  enab; 
String resultsString ; 
String[] myStringArray; 

public class Receiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 

    } 
ListView listView ; 
      // Get ListView object from xml 
      listView = (ListView) findViewById(R.id.listView1); 

      // Defined Array values to show in ListView 
     String[] values = new String[] { "Android List View", 
             "Adapter implementation", 
             "Simple List View In Android", 
             "Create List View Android", 
             "Android Example", 
             "List View Source Code", 
             "List View Array Adapter", 

             }; 
//getting this error : The constructor ArrayAdapter<String>(Wifi.Receiver, int, int, //String[]) is undefined   
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values); 


     listView.setAdapter(adapter); 

} 
} 

PS :이 내 코드입니다

The constructor ArrayAdapter(Wifi.Receiver, int, int, String[]) is undefined

을 때 나는 그것이 정상적으로 작동 onCreate 메서드에 ListView의 코드를 넣어,하지만 난 onReceive 메서드에서 그것을 원한다.

답변

3

귀하의 경우 this은 활동 컨텍스트를 나타내지 않습니다. 생성자

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects) 

Added in API level 1 Constructor 

Parameters 

context    The current context. 
resourc    The resource ID for a layout file containing a layout to use when instantiating views. 
textViewResourceId The id of the TextView within the layout resource to be populated 
objects    The objects to represent in the ListView. 

에서

사용

ArrayAdapter<String> adapter = new ArrayAdapter<String>(Wifi.this,android.R.layout.simple_list_item_1, android.R.id.text1, values); 

봐 그래서 처음 PARAM는 유효한 상황

java.lang.Object 
    ↳ android.content.Context // see context 
     ↳ android.content.ContextWrapper 
      ↳ android.view.ContextThemeWrapper 
       ↳ android.app.Activity // see activity 

입니다 그리고 당신의 클래스는 활동을

public class Wifi extends Activity 
를 확장

유효한 문맥으로 Wifi.this을 사용하십시오.

+0

감사합니다! 당신은 내 생명을 구했어. :) 효과가있어 : D – Amina

+0

@Amina 나는 7 가지 질문을 받아들이지 않았다. 대답 옆에있는 체크를 클릭하여 답을 수락하십시오. http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Raghunandan

+0

설명을 주셔서 감사합니다. – Amina

관련 문제