我正在尝试创建一个解析来自JSON
解析器的数据的应用程序,并使用recyclerview
中的数据提取来填充JSON
项目,但每当我运行它时它只会冻结在加载并显示没有错误,除了适配器没有附加跳过布局。我陷入了死胡同,因为我无法继续下去。我该如何解决这个代码的问题。
MainActivity
public class MainActivity extends AppCompatActivity {
CollapsingToolbarLayout collapsingToolbarLayout;
ImageView image;
TextView personName;
TextView personProf;
RecyclerView rv;
private List<Person> personList;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
ImageView images;
RecyclerViewHeader header;
ImageView iv;
public String strurl;
URL url;
URI uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
personName= (TextView)findViewById(R.id.textView);
personProf=(TextView)findViewById(R.id.textView2);
images= (ImageView)findViewById(R.id.imageView);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitle("Capsule");
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));
//setPalette();
strurl = "http://www.humanfox.com/capsule/assets/img/jawed_headshot.jpg";
Typeface face2 = Typeface.createFromAsset(getAssets(),"fonts/OpenSans-Regular.ttf");
Typeface face4= Typeface.createFromAsset(getAssets(),"fonts/OpenSans-Bold.ttf");
image = (ImageView) findViewById(R.id.image);
Picasso.with(this)
.load("http://d152j5tfobgaot.cloudfront.net/wp-content/uploads/2013/06/Javed-Habib_11.jpg")
.into(image);
//header = (RecyclerViewHeader) findViewById(R.id.header);
//header.attachTo(rv,true);
rv =(RecyclerView)findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
personList= new ArrayList<>();
getData();
}
private void getData(){
//Showing a progress dialog
final ProgressDialog loading = ProgressDialog.show(this,"Loading Data", "Please wait...",false,false);
//Creating a json array request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(COnfig.DATA_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Dismissing progress dialog
loading.dismiss();
//calling method to parse json array
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
Person per = new Person();
JSONObject json = null;
try {
json = array.getJSONObject(i);
per.setImageUrl(json.getString(COnfig.TAG_IMAGE_URL));
per.setName(json.getString(COnfig.TAG_NAME));
per.setPassion(json.getString(COnfig.TAG_PROFESSION));
} catch (JSONException e) {
e.printStackTrace();
}
personList.add(per);
}
adapter = new RVAdapter(personList, this);
rv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
}
Recycler view Adapter
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
private ImageLoader imageLoader;
Context context;
private List<Person> persons;
RVAdapter(List<Person> persons,Context context){
this.persons = persons;
this.context = context;
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(persons.get(i).getImageUrl(), ImageLoader.getImageListener(personViewHolder.personPhoto, R.drawable.ic_launcher, android.R.drawable.ic_dialog_alert));
personViewHolder.personName.setText(persons.get(i).getName());
personViewHolder.personProf.setText(persons.get(i).getPassion());
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return persons.size();
}
public static class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
CardView cv;
TextView personName;
TextView personProf;
ImageView personPhoto;
Context context;
PersonViewHolder(final View itemView) {
super(itemView);
context = itemView.getContext();
itemView.setClickable(true);
itemView.setOnClickListener(this);
cv = (CardView) itemView.findViewById(R.id.cv);
personName = (TextView) itemView.findViewById(R.id.textView);
personProf = (TextView) itemView.findViewById(R.id.textView2);
personPhoto=(ImageView) itemView.findViewById(R.id.imageView);
}
@Override
public void onClick(View view) {
final Intent intent;
int position =getAdapterPosition();
switch(position){
case 0:
context.startActivity(new Intent(context,caps1.class));
}
}
}
}
自定义排球请求
public class CustomVolleyRequest {
private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private CustomVolleyRequest(Context context) {
this.context = context;
this.requestQueue = getRequestQueue();
imageLoader = new ImageLoader(requestQueue,
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 CustomVolleyRequest getInstance(Context context) {
if (customVolleyRequest == null) {
customVolleyRequest = new CustomVolleyRequest(context);
}
return customVolleyRequest;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
}
return requestQueue;
}
public ImageLoader getImageLoader() {
return imageLoader;
}
}
答案 0 :(得分:1)
确保已在后台线程上实现了发送请求和解析json,而不是在主线程中。否则,它会冻结,因为你的主线程可以做一个任务,并且应该做UI更新而不是所有任务。