从API填充片段中的自定义列表视图

时间:2015-03-10 20:33:46

标签: android listview android-fragments android-custom-view

我想在TED API中使用Ted Talks填充自定义列表视图,但没有显示任何内容。我做了所有的努力,但没有做到。 这是我的代码 片段类

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   愚蠢的错误。我没有打电话给getMethod。

1 个答案:

答案 0 :(得分:0)

根据您使用的代码

obj = new HandleJSON(TalkUrl + Offset);
            obj.fetchJSON("Talks");

应该是

obj = new HandleJSON(TalkUrl + Offset);
            obj.fetchJSON("talks");//Note the case of KEY

JSON区分大小写。

**Edit:**

确保您没有填充

TalkItemsArray

在您的代码中。