我试图将包含联系人图片和文字的json数据解析为recyclerView,我收到了错误
java.lang.NumberFormatException: Invalid int: "http://lorempixel.com/100/100/"
当我尝试使用onBindViewHolder()
picasso
中设置图像时
这是我的适配器
package com.example.task;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder>{
private List<Contact> contactList;
Context context ;
private LayoutInflater mInflater;
public Adapter( Context context ,List<Contact> contactList) {
this.context = context;
this.contactList = contactList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_card, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Contact item = contactList.get(position);
holder.title.setText(item.getName());
Picasso.with(context).load(contactList.get(position).getProfilePic())
.into((holder.profile_pic));
//holder.profile_pic.setImageResource(Integer.parseInt(item.getProfilePic()));
//holder.year.setText(movie.getYear());
}
@Override
public int getItemCount() {
return contactList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
ImageView profile_pic;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.name_left);
profile_pic = (ImageView) itemView.findViewById(R.id.profile_image_left);
}
}
}
这是我的MainActivity
package com.example.task;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
/**
* Views
*/
private RecyclerView recyclerView;
private ArrayList<Contact> contactList;
private Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* Array List for Binding Data from JSON to this List
*/
contactList = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
/**
* Getting List and Setting List Adapter
*/
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
/*recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Snackbar.make(parentView, contactList.get(position).getName() + " => " + contactList.get(position).getPhone().getHome(), Snackbar.LENGTH_LONG).show();
}
});*/
/**
* Just to know onClick and Printing Hello Toast in Center.
*/
/* Toast toast = Toast.makeText(getApplicationContext(), R.string.string_click_to_load, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();*/
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
assert fab != null;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(@NonNull final View view) {*/
/**
* Checking Internet Connection
*/
if (InternetConnection.checkConnection(getApplicationContext())) {
final ProgressDialog dialog;
/**
* Progress Dialog for User Interaction
*/
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("getting json");
dialog.setMessage("Downloading json data for you");
dialog.show();
//Creating an object of our api interface
ApiService api = RetroClient.getApiService();
/**
* Calling JSON
*/
Call<ContactList> call = api.getMyJSON();
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<ContactList>() {
@Override
public void onResponse(Call<ContactList> call, Response<ContactList> response) {
//Dismiss Dialog
dialog.dismiss();
if(response.isSuccessful()) {
/**
* Got Successfully
*/
contactList = response.body().getContacts();
/**
* Binding that RecyclerView to Adapter
*/
adapter = new Adapter(MainActivity.this, contactList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
} else {
Snackbar.make(recyclerView,"Something went wrong", Snackbar.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<ContactList> call, Throwable t) {
dialog.dismiss();
}
});
} else {
Snackbar.make(recyclerView, "Internet not available", Snackbar.LENGTH_LONG).show();
}
}
}
这是我访问
的json api的链接http://pratikbutani.x10.mx/json_data.json
我做错了什么?
答案 0 :(得分:1)
将“profile_pic”变量的返回类型设置为字符串,并在Contact.java类中将“profile_pic”的数据类型更改为String。