Android Studio显示此警告:
未选中调用List作为原始类型'ConnectionSuccess的成员
如果我检查类型或什么,我怎么能跳过这个警告?
public class RequestTask {
private Context mContext;
ConnectionSuccess connectionSuccess;
RequestHandler requestHandler;
ProgressDialog progressDialog;
public RequestTask(Context context, ConnectionSuccess connectionSuccess) {
this.mContext = context;
this.connectionSuccess = connectionSuccess;
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage(mContext.getResources().getString(R.string.loading));
}
// Request a string response
public void MakeRequest() {
String URL = mContext.getResources().getString(R.string.menu_url);
if (ConnectionTracker.isNetworkAvailable(mContext)) {
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).create();
Response response1 = gson.fromJson(response, Response.class);
response1.setItemsList(response1.getItemsList());
//the warning shows here
connectionSuccess.onResponse(response1.getItemsList());
} catch (Exception e) {
e.printStackTrace();
}
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Error handling
progressDialog.dismiss();
Toast.makeText(mContext, mContext.getResources().getString(R.string.error_message), Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
// Add a request to RequestQueue.
MySingleton.getInstance(mContext).addToRequestQueue(stringRequest);
} else if (!ConnectionTracker.isNetworkAvailable(mContext)) {
Toast.makeText(mContext, mContext.getResources().getString(R.string.no_internet), Toast.LENGTH_LONG).show();
}
}
}
public class More extends Fragment implements ConnectionSuccess<Data> {
@Override
public void onResponse(List<Data> result) {
}
}
public interface ConnectionSuccess<T> {
void onResponse(List<T> result);
}
public class Data {
@SerializedName("arabic_title")
String arabic_title;
@SerializedName("english_title")
String english_title;
@SerializedName("url")
String url;
public String getArabic_title() {
return arabic_title;
}
public void setArabic_title(String arabic_title) {
this.arabic_title = arabic_title;
}
public String getEnglish_title() {
return english_title;
}
public void setEnglish_title(String english_title) {
this.english_title = english_title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
@SerializedName("data")
List<Data> itemsList;
public List<Data> getItemsList() {
return itemsList;
}
public void setItemsList(List<Data> itemsList) {
this.itemsList = itemsList;
}
public interface ConnectionSuccess<T> {
void onResponse(List<T> result);
}
ConnectionSuccess
有一个类型参数T
,但在你的构造函数arg和字段声明中,你没有传递一个类型参数。你可能意味着两个都是ConnectionSuccess<Data>
这将允许编译器在你调用connectionSuccess.onResponse(response1.getItemsList())
时发现它应该期望类型为List<Data>
的参数