Я использую функцию notifychanged () после вызова add.и нет ошибки в моем logcat.loaddata () для загрузки данных без URL JSON.и получить data () для загрузки данных из json url.Помогите мне, пожалуйста.мой манифест Android был установлен для разрешения Android.Я сбит с толку.Я следую весь путь, чтобы показать свое мнение о переработчике, но нет возможности показать данные.Я использую URL от героку.и шоу JSON.Можете ли вы показать мне, как решить мою проблему?большое спасибо раньше.:)
Это мой домашний фрагмент
package com.android.herry.combile;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.textclassifier.TextLinks;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class FragmentHome extends Fragment {
private RecyclerView listevent;
private LinearLayoutManager linearLayoutManager;
private EventAdapter eventListAdapter;
private Context context;
private RecyclerView mList;
private final static String url= "https://guarded-scrubland-29104.herokuapp.com/index.php?action=show_data";
public static FragmentHome newInstance(){
return new FragmentHome();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
listevent = (RecyclerView) rootView.findViewById(R.id.listEvent);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
linearLayoutManager = new LinearLayoutManager(context);
eventListAdapter = new EventAdapter();
listevent.setLayoutManager(linearLayoutManager);
listevent.setAdapter(eventListAdapter);
getData();
}
private void loadData(){
List<Event> memberList = new ArrayList<>();
Event member;
int thumb[] = {R.drawable.ic_account, R.drawable.ic_account, R.drawable.ic_account,
R.drawable.ic_account, R.drawable.ic_account, R.drawable.ic_account,
R.drawable.ic_account, R.drawable.ic_account, R.drawable.ic_account,
R.drawable.ic_account, R.drawable.ic_account, R.drawable.ic_account,
R.drawable.ic_account, R.drawable.ic_account, R.drawable.ic_account,
R.drawable.ic_account, R.drawable.ic_account, R.drawable.ic_account,
R.drawable.ic_account, R.drawable.ic_account};
String name[] = {"Ayana Shahab", "Beby Chaesara Anadila", "Delima Rizky", "Dena Siti Rohyati",
"Elaine Hartanto", "Frieska Anastasia Laksani", "Gabriella M. W.", "Ghaida Farisya",
"Jennifer Rachel Natasya", "Jessica Vania Widjaja", "Jessica Veranda", "Melody Nurramdhani Laksani",
"Nabilah Ratna Ayu Azalia", "Rezky Wiranti Dhike", "Sendy Ariani", "Shania Junianatha",
"Shinta Naomi", "Sofia Meifaliani", "Sonia Natalia", "Thalia Ivanka Elizabeth"};
String team = "Team J";
for(int i=0; i < thumb.length; i++){
member = new Event();
member.setId(i+1);
member.setTitle(name[i]);
member.setDetail(team);
member.setPic(thumb[i]);
memberList.add(member);
}
eventListAdapter.addAll(memberList);
}
private void getData(){
final List<Event> listitem = new ArrayList<>();
final Event item=new Event();
final String[] ocha = new String[1];
StringRequest jsonArrayRequest = new StringRequest(Request.Method.GET,url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("Records");
for (int i = 0; i < array.length(); i++) {
jsonObject = array.getJSONObject(i);
item.setTitle(jsonObject.getString("event_id"));
item.setDetail(jsonObject.getString("detail"));
item.setId(jsonObject.getInt("event_id"));
ocha[0] =jsonObject.getString("event_id")+ocha;
// Log.e("ochantik", String.valueOf(ocha));
listitem.add(item);
eventListAdapter.notifyDataSetChanged();
}
listevent.setAdapter(eventListAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
requestQueue.add(jsonArrayRequest);
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
}
Это Event.java
package com.android.herry.combile;
import android.os.Parcel;
import android.os.Parcelable;
public class Event implements Parcelable {
private int id;
private String title;
private String detail;
private int pic;
protected Event(Parcel in) {
id = in.readInt();
title = in.readString();
detail = in.readString();
pic= in.readInt();
}
public static final Parcelable.Creator<Event> CREATOR = new Parcelable.Creator<Event>() {
@Override
public Event createFromParcel(Parcel in) {
return new Event(in);
}
@Override
public Event[] newArray(int size) {
return new Event[size];
}
};
public Event() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDetail() { return detail; }
public void setDetail(String detail) { this.detail = detail; }
public int getPic() { return pic; }
public void setPic(int pic) {
this.pic = pic;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(title);
dest.writeString(detail);
dest.writeInt(pic);
}
}
, а это мой адаптер
package com.android.herry.combile;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class EventAdapter extends RecyclerView.Adapter<EventAdapter.EventViewHolder> {
private List<Event> eventList;
public EventAdapter() {
eventList = new ArrayList<>();
}
private void add(Event item) {
eventList.add(item);
notifyItemInserted(eventList.size() - 1);
}
public void addAll(List<Event> eventList) {
for (Event event : eventList) {
add(event);
}
}
public void remove(Event item) {
int position = eventList.indexOf(item);
if (position > -1) {
eventList.remove(position);
notifyItemRemoved(position);
}
}
public void clear() {
while (getItemCount() > 0) {
remove(getItem(0));
}
}
public Event getItem(int position) {
return eventList.get(position);
}
@Override
public EventViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_list, parent, false);
EventViewHolder memberViewHolder = new EventViewHolder(view);
return memberViewHolder;
}
@Override
public void onBindViewHolder(EventViewHolder holder, int position) {
final Event event = eventList.get(position);
holder.EventPic.setImageResource(event.getPic());
holder.EventTitle.setText(event.getTitle());
holder.EventDetail.setText(event.getDetail());
}
@Override
public int getItemCount() {
return eventList.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
}
static class EventViewHolder extends RecyclerView.ViewHolder {
ImageView EventPic;
TextView EventTitle;
TextView EventDetail;
public EventViewHolder(View itemView) {
super(itemView);
EventPic = (ImageView) itemView.findViewById(R.id.Event_pic);
EventTitle = (TextView) itemView.findViewById(R.id.Event_Title);
EventDetail = (TextView) itemView.findViewById(R.id.Event_Detail);
}
}
}