2017-04-19 54 views
0

我試圖做一個「動態搜索」 ..不兼容的類型:<匿名TextWatcher>不能轉換到上下文

這個片段是我實現的請求......在這裏,我得到一個錯誤(錯誤:(95,40)error:incompatible types:can not be converted to Context)and MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);這就是我得到了錯誤的行

片段:

public class CercaFragment extends Fragment { 

TextView textView; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 

    View v = inflater.inflate(R.layout.cerca, parent, false); 


    textView = (TextView) v.findViewById(R.id.textView); 
    setHasOptionsMenu(true); 

    EditText search = (EditText)v.findViewById(R.id.editText); 

    search.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {    // TODO Auto-generated method stub   } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) {     
      textView.setText(s); 

      String url = "http://api.geonames.org/searchJSON?q=londoz&maxRows=10&fuzzy=0.8&username=demo"; 

      JsonObjectRequest jsObjRequest = new JsonObjectRequest 
        (Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 

         @Override 
         public void onResponse(JSONObject response) { 


          GeonamesClass example = new Gson().fromJson(response.toString(),GeonamesClass.class); 
                  textView.setText("Name"+example.getUsers().get(0).getName()); 
         } 
        }, new Response.ErrorListener() { 

         @Override 
         public void onErrorResponse(VolleyError error) { 
          // TODO Auto-generated method stub 

         } 
        }); 
      // Access the RequestQueue through your singleton class. 

      MySingleton.getInstance(this).addToRequestQueue(jsObjRequest); 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 

    }); 

    return v; 

} 
} 

MySingleton:

public class MySingleton { 
    private static MySingleton mInstance; 
    private RequestQueue mRequestQueue; 
    private ImageLoader mImageLoader; 
    private static Context mCtx; 

private MySingleton(Context context) { 
    mCtx = context; 
    mRequestQueue = getRequestQueue(); 

    mImageLoader = new ImageLoader(mRequestQueue, 
      new ImageLoader.ImageCache() { 
       private final LruCache<String, Bitmap> 
         cache = new LruCache<String, Bitmap>(20); 

       @Override 
       public Bitmap getBitmap(String url) { 
        return cache.get(url); 
       } 

       @Override 
       public void putBitmap(String url, Bitmap bitmap) { 
        cache.put(url, bitmap); 
       } 
      }); 
} 

public static synchronized MySingleton getInstance(Context context) { 
    if (mInstance == null) { 
     mInstance = new MySingleton(context); 
    } 
    return mInstance; 
} 

public RequestQueue getRequestQueue() { 
    if (mRequestQueue == null) { 
     // getApplicationContext() is key, it keeps you from leaking the 
     // Activity or BroadcastReceiver if someone passes one in. 
     mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); 
    } 
    return mRequestQueue; 
} 

public <T> void addToRequestQueue(Request<T> req) { 
    getRequestQueue().add(req); 
} 

public ImageLoader getImageLoader() { 
    return mImageLoader; 
} 

} 

回答

2

當您在事件中做到,你是不是指的是應用程序的上下文,但是指向產生事件的對象,以獲取上下文在你的單身你必須做MySingleton.Getinstance(this)...以外的事件,或者如果它在那裏你想打電話它做一些像MySingleton.Getinstance。(getActivity())... ....

getActivity()是被稱爲片段中它的調用上下文引用

的方法我希望幫助 問候

+0

啊奧科克的感謝! ! –

相關問題