从 API 填充片段中的自定义列表视图
Populate Custom List View in Fragment from API
我想用来自 TED API 的 Ted 演讲填充自定义列表视图,但没有显示任何内容。我已经做了所有的努力,但什么也没做。
这是我的代码
片段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;
}
}
}
其中 custom_list_talk 是自定义列表的布局,talk_list_frag 是具有 id 为“@android:id/list”的列表视图的片段的布局
API 工作正常,数据来自 API。
问题是它在自定义列表视图中什么都不显示(或者它可能没有膨胀等)
EDIT
Foolish mistake. I didn't call getMethod.
根据您使用的代码
obj = new HandleJSON(TalkUrl + Offset);
obj.fetchJSON("Talks");
应该是
obj = new HandleJSON(TalkUrl + Offset);
obj.fetchJSON("talks");//Note the case of KEY
JSON 区分大小写。
**Edit:**
确保您没有填充
TalkItemsArray
在你的代码中。
我想用来自 TED API 的 Ted 演讲填充自定义列表视图,但没有显示任何内容。我已经做了所有的努力,但什么也没做。 这是我的代码 片段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;
}
}
} 其中 custom_list_talk 是自定义列表的布局,talk_list_frag 是具有 id 为“@android:id/list”的列表视图的片段的布局 API 工作正常,数据来自 API。 问题是它在自定义列表视图中什么都不显示(或者它可能没有膨胀等)
EDIT Foolish mistake. I didn't call getMethod.
根据您使用的代码
obj = new HandleJSON(TalkUrl + Offset);
obj.fetchJSON("Talks");
应该是
obj = new HandleJSON(TalkUrl + Offset);
obj.fetchJSON("talks");//Note the case of KEY
JSON 区分大小写。
**Edit:**
确保您没有填充
TalkItemsArray
在你的代码中。