这是我通过这个类的主要类我从api通过url获取数据并使One Adapter类维护它但发生意外错误。
这是MainActivity.java:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
public static SvgLoader svgLoader;
private final String Url="https://restcountries.eu/rest/v2/all";
List<ListItem> listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(RecyclerView)findViewById(R.id.recycleview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItems = new ArrayList<>();
loadRecyclerViewData();
}
private void loadRecyclerViewData() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data.....");
progressDialog.show();
StringRequest stringRequest= new StringRequest(Request.Method.GET,
Url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONArray json = new JSONArray(response);
for (int i=0; i<json.length();i++)
{
JSONObject o = json.getJSONObject(i);
// String NewString = o.getString("callingCode").toString();
// System.out.println("hjashsdgx = "+NewString);
// String NewString2 = NewString.replaceAll("[\"/[ /]]", "");
// JSONArray jsoncallingCode = new JSONArray("callingCodes");
// JSONObject callingcode =jsoncallingCode.getJSONObject(1);
ListItem item = new ListItem(o.getString("name")
,"+"+o.getString(
"callingCodes").replace("]","")
.replace("[","").replace("\"","")
,o.getString("flag"));
listItems.add(item);
}
adapter = new MyAdapter(listItems,getApplicationContext());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
protected void onDestroy() {
super.onDestroy();
SvgLoader.pluck().close();
}
}
这是我的MyAdapter.java。此活动在第29行的SvgLoader点发生错误。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
{
private List<ListItem> listItems;
private Context context;
public MyAdapter()
{}
public MyAdapter(List<ListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(parent.getContext()).inflate(R.layout.cartview,null);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListItem listItem = listItems.get(position);
holder.heading.setText(listItem.getHead());
holder.desc.setText(listItem.getDesc());
SvgLoader.pluck()
.with(new MainActivity()) // error occur here...
.setPlaceHolder(R.mipmap.ic_launcher, R.mipmap.ic_launcher)
.load(listItem.getImageurl().toString(), holder.imageView);
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView heading ;
public TextView desc;
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
heading=(TextView) itemView.findViewById(R.id.heading);
desc =(TextView) itemView.findViewById(R.id.desc);
imageView=(ImageView)itemView.findViewById(R.id.imageload);
}
}
}
我不知道为什么会这样,有什么想法?
答案 0 :(得分:1)
替换以下代码
<强> MainActivity.java 强>
adapter = new CountryAdapter(listItems, MainActivity.this); //Replace getApplicationContext() to MainActivity.this
<强> MyAdapter.java 强>
public MyAdapter(List<CountryListItem> listItems, Activity activity) {
this.listItems = listItems;
this.activity = activity;
}// pass activity replace of context
SvgLoader.pluck()
.with(activity)//Replace new MainActivity with activity
.setPlaceHolder(R.mipmap.ic_launcher, R.mipmap.ic_launcher)
.load(listItem.getImageUrl().toString(), holder.imageView);
答案 1 :(得分:0)
试试这段代码:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
{
private List<ListItem> listItems;
private Context context;
public MyAdapter()
{}
public MyAdapter(List<ListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(parent.getContext()).inflate(R.layout.cartview,null);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListItem listItem = listItems.get(position);
holder.heading.setText(listItem.getHead());
holder.desc.setText(listItem.getDesc());
SvgLoader.pluck()
.with(context)
.setPlaceHolder(R.mipmap.ic_launcher, R.mipmap.ic_launcher)
.load(listItem.getImageurl().toString(), holder.imageView);
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView heading ;
public TextView desc;
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
heading=(TextView) itemView.findViewById(R.id.heading);
desc =(TextView) itemView.findViewById(R.id.desc);
imageView=(ImageView)itemView.findViewById(R.id.imageload);
}
}
}