I want to populate custom list view with Ted Talks from TED API, but nothing is shown. I have done all the efforts but nothing does it. Here's my code Fragment Class
public class TalksFragment extends ListFragment {
private ListView talkList;
private String TalkUrl = "https://api.ted.com/v1/talks.json?api-key=nm4nq9uyqg558m7z8axbu3be&order=created_at:desc&fields=media_profile_uris,photo_urls,speakers&offset=";
private HandleJSON obj;
private ProgressDialog m_ProgressDialog = null;
private ArrayList<TalksItems> TalkItemsArray = null;
private TalkListAdapter talkListAdapter;
private Runnable viewOrders;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View talks = inflater.inflate(R.layout.talk_list_frag, container, false);
talkList = (ListView) talks.findViewById(android.R.id.list);
return talks;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TalkItemsArray = new ArrayList<>();
this.talkListAdapter = new TalkListAdapter(getActivity(), android.R.layout.simple_list_item_1, TalkItemsArray);
talkList.setAdapter(this.talkListAdapter);
viewOrders = new Runnable(){
@Override
public void run() {
int Offset=0;
obj = new HandleJSON(TalkUrl + Offset);
obj.fetchJSON("Talks");
while (obj.parsingComplete);
TalkItemsArray = obj.getTalkItems();
getActivity().runOnUiThread(returnRes);
}
};
Thread thread = new Thread(null, viewOrders, "MagentoBackground");
thread.start();
m_ProgressDialog = ProgressDialog.show(getActivity(),
"Please wait...", "Retrieving data ...", true);
}
private Runnable returnRes = new Runnable() {
@Override
public void run() {
int size = TalkItemsArray.size();
if(TalkItemsArray != null && TalkItemsArray.size() > 0){
talkListAdapter.notifyDataSetChanged();
for(int i=0; i<size; i++)
talkListAdapter.add(TalkItemsArray.get(i));
}
m_ProgressDialog.dismiss();
talkListAdapter.notifyDataSetChanged();
}
};
private class TalkListAdapter extends ArrayAdapter<TalksItems>{
LayoutInflater inflater;
private ArrayList<TalksItems> items;
public TalkListAdapter(Context context, int textViewResourceId, ArrayList<TalksItems> items) {
super(context, textViewResourceId, items);
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null)
view = inflater.inflate(R.layout.custom_list_talk, null);
else
view = convertView;
TalksItems TI = items.get(position);
if(TI != null){
TextView talkName = (TextView)view.findViewById(R.id.txtTalkName);
TextView speakerName = (TextView)view.findViewById(R.id.txtSpeakerName);
TextView talkDescription = (TextView)view.findViewById(R.id.txtTalkDescription);
ImageView talkImage = (ImageView)view.findViewById(R.id.talkImage);
talkName.setText(TI.getTalkName());
speakerName.setText(TI.getSpeakerName());
talkDescription.setText(TI.getTalkDescription());
talkImage.setImageURI(TI.getImageURL());
}
return view;
}
}
} Where custom_list_talk is layout for custom list and talk_list_frag is layout for fragment having a listview with id "@android:id/list" API is working correctly, data is coming correctly from API. Problem is that it shows nothing in custom listview(or may be it is not inflating or etc)
EDIT Foolish mistake. I didn't call getMethod.
As per your code you are using
obj = new HandleJSON(TalkUrl + Offset);
obj.fetchJSON("Talks");
It should be
obj = new HandleJSON(TalkUrl + Offset);
obj.fetchJSON("talks");//Note the case of KEY
JSON is Case Sensitive.
**Edit:**
Make sure you are not populating the
TalkItemsArray
In your code.