Java 为什么我的自定义listview没有显示任何内容?

Java 为什么我的自定义listview没有显示任何内容?,java,android,listview,android-fragments,Java,Android,Listview,Android Fragments,我正在制作一个列表视图,以显示codeforces中所有即将举行的比赛。因此,我创建了一个CodeforcesContest对象和一个包含ListView的片段。我希望有关竞赛的所有信息都在ListView的一行中。竞赛信息是竞赛名称、竞赛时间和保存站点名称的另一个字符串,该字符串始终为“Codeforces”。这是我到目前为止所拥有的。我对Java和android非常陌生。我已经搜索并找到了使用自定义类实现自定义listview的方法。但是,我的listview没有显示任何内容。我正在使用an

我正在制作一个列表视图,以显示codeforces中所有即将举行的比赛。因此,我创建了一个
CodeforcesContest
对象和一个包含
ListView
的片段。我希望有关竞赛的所有信息都在ListView的一行中。竞赛信息是竞赛名称、竞赛时间和保存站点名称的另一个字符串,该字符串始终为“Codeforces”。这是我到目前为止所拥有的。我对Java和android非常陌生。我已经搜索并找到了使用自定义类实现自定义listview的方法。但是,我的listview没有显示任何内容。我正在使用android studio 3.0

CodeforcesContest.java

package com.example.redwanul.cptracker;

/**
 * Created by redwanul on 12/1/17.
 */

public class CodeforcesContest {
    private String name;
    private String time;

    public CodeforcesContest(String name, String time) {
        this.name = name;
        this.time = time;
    }

    public String getName() {
        return name;
    }
    public String getTime() {
        return time;
    }
    public void setName(String name) { this.name = name; }
    public void setTime(String time) { this.time = time; }
}
package com.example.redwanul.cptracker;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link FragmentUpcomingContest.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link FragmentUpcomingContest#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FragmentUpcomingContest extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public FragmentUpcomingContest() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FragmentUpcomingContest.
     */
    // TODO: Rename and change types and number of parameters
    public static FragmentUpcomingContest newInstance(String param1, String param2) {
        FragmentUpcomingContest fragment = new FragmentUpcomingContest();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ArrayList <CodeforcesContest> arrayList = new ArrayList<>();
        CodeforcesContest[] contests;
        View myLayout = inflater.inflate(R.layout.fragment_fragment_upcoming_contest,container,false);

        ListView listView = myLayout.findViewById(R.id.listView);
        try {
            JSONArray jsonArray = new GetCodeforcesContestList().execute().get();
            for(int i = 0; i < jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String _name = jsonObject.getString("name");
                String _phase = jsonObject.getString("phase");
                String _time = jsonObject.getString("startTimeSeconds");
                Log.d("Debug: name",_name);
                Log.d("Debug: phase",_phase);
                Log.d("Debug: Time",_time);
                if(_phase.equals(new String("BEFORE"))){
                    arrayList.add(new CodeforcesContest(_name,_time));
                }
            }

        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        contests = new CodeforcesContest[arrayList.size()];

        for(int i = 0; i < arrayList.size(); i++){
            contests[i] = arrayList.get(i);
        }
        Log.d("Array Length: ", new Integer(arrayList.size()).toString());
        ContestAdapter mAdapter = new ContestAdapter(getContext(),R.layout.row,contests);
        listView.setAdapter(mAdapter);

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_upcoming_contest, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
package com.example.redwanul.cptracker;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by redwanul on 12/1/17.
 */

public class ContestAdapter extends ArrayAdapter<CodeforcesContest> {
    Context context;
    int layourResourceId;
    private static LayoutInflater inflater = null;
    CodeforcesContest[] data = null;

    public ContestAdapter(Context context, int layoutResourceId, CodeforcesContest[] data){
        super(context,layoutResourceId,data);
        this.layourResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        int ll = data.length;
        Log.d("Constructor",new Integer(data.length).toString());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if(vi == null)
            vi = inflater.inflate(R.layout.row,null);
        CodeforcesContest contest = data[position];
        Log.d("In Adapter",contest.getName());
        TextView contestName = (TextView)vi.findViewById(R.id.contest_name);
        TextView contestSite = (TextView)vi.findViewById(R.id.contest_site);
        TextView timeDate = (TextView)vi.findViewById(R.id.date_time);

        Date date = new Date(Integer.parseInt(contest.getTime()) * 1000);
        SimpleDateFormat formatter = new SimpleDateFormat("M/D/YYYY");
        String dateString = formatter.format(date);

        contestName.setText(contest.getName());
        contestSite.setText("Codeforces");
        timeDate.setText(dateString);
        return vi;
    }
}
单行的自定义布局

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="9">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/contest_name"
        android:layout_weight="3"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/contest_site"
        android:layout_weight="3"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/date_time"
        android:layout_weight="3"/>

</LinearLayout>

用于显示ListView的片段类

FragmentUpcomingContest.java

package com.example.redwanul.cptracker;

/**
 * Created by redwanul on 12/1/17.
 */

public class CodeforcesContest {
    private String name;
    private String time;

    public CodeforcesContest(String name, String time) {
        this.name = name;
        this.time = time;
    }

    public String getName() {
        return name;
    }
    public String getTime() {
        return time;
    }
    public void setName(String name) { this.name = name; }
    public void setTime(String time) { this.time = time; }
}
package com.example.redwanul.cptracker;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link FragmentUpcomingContest.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link FragmentUpcomingContest#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FragmentUpcomingContest extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public FragmentUpcomingContest() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FragmentUpcomingContest.
     */
    // TODO: Rename and change types and number of parameters
    public static FragmentUpcomingContest newInstance(String param1, String param2) {
        FragmentUpcomingContest fragment = new FragmentUpcomingContest();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ArrayList <CodeforcesContest> arrayList = new ArrayList<>();
        CodeforcesContest[] contests;
        View myLayout = inflater.inflate(R.layout.fragment_fragment_upcoming_contest,container,false);

        ListView listView = myLayout.findViewById(R.id.listView);
        try {
            JSONArray jsonArray = new GetCodeforcesContestList().execute().get();
            for(int i = 0; i < jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String _name = jsonObject.getString("name");
                String _phase = jsonObject.getString("phase");
                String _time = jsonObject.getString("startTimeSeconds");
                Log.d("Debug: name",_name);
                Log.d("Debug: phase",_phase);
                Log.d("Debug: Time",_time);
                if(_phase.equals(new String("BEFORE"))){
                    arrayList.add(new CodeforcesContest(_name,_time));
                }
            }

        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        contests = new CodeforcesContest[arrayList.size()];

        for(int i = 0; i < arrayList.size(); i++){
            contests[i] = arrayList.get(i);
        }
        Log.d("Array Length: ", new Integer(arrayList.size()).toString());
        ContestAdapter mAdapter = new ContestAdapter(getContext(),R.layout.row,contests);
        listView.setAdapter(mAdapter);

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_upcoming_contest, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
package com.example.redwanul.cptracker;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by redwanul on 12/1/17.
 */

public class ContestAdapter extends ArrayAdapter<CodeforcesContest> {
    Context context;
    int layourResourceId;
    private static LayoutInflater inflater = null;
    CodeforcesContest[] data = null;

    public ContestAdapter(Context context, int layoutResourceId, CodeforcesContest[] data){
        super(context,layoutResourceId,data);
        this.layourResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        int ll = data.length;
        Log.d("Constructor",new Integer(data.length).toString());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if(vi == null)
            vi = inflater.inflate(R.layout.row,null);
        CodeforcesContest contest = data[position];
        Log.d("In Adapter",contest.getName());
        TextView contestName = (TextView)vi.findViewById(R.id.contest_name);
        TextView contestSite = (TextView)vi.findViewById(R.id.contest_site);
        TextView timeDate = (TextView)vi.findViewById(R.id.date_time);

        Date date = new Date(Integer.parseInt(contest.getTime()) * 1000);
        SimpleDateFormat formatter = new SimpleDateFormat("M/D/YYYY");
        String dateString = formatter.format(date);

        contestName.setText(contest.getName());
        contestSite.setText("Codeforces");
        timeDate.setText(dateString);
        return vi;
    }
}
package com.example.redwanul.cptracker;
导入android.content.Context;
导入android.net.Uri;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ListView;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入java.util.ArrayList;
/**
*一个简单的{@link Fragment}子类。
*包含此片段的活动必须实现
*{@link FragmentUpcomingContest.OnFragmentInteractionListener}接口
*处理交互事件。
*使用{@link FragmentUpcomingContest#newInstance}工厂方法
*创建此片段的实例。
*/
公共类FragmentUpcomingContest扩展了Fragment{
//TODO:重命名参数参数,选择匹配的名称
//片段初始化参数,例如ARG_ITEM_NUMBER
私有静态最终字符串ARG_PARAM1=“PARAM1”;
私有静态最终字符串ARG_PARAM2=“PARAM2”;
//TODO:重命名和更改参数类型
私有字符串mParam1;
私有字符串mParam2;
私有OnFragmentInteractionListener mListener;
公共碎片收集竞赛(){
//必需的空公共构造函数
}
/**
*使用此工厂方法创建的新实例
*使用提供的参数创建此片段。
*
*@param param1参数1。
*@param param2参数2。
*@return FragmentUpcomingContest的新实例。
*/
//TODO:重命名和更改参数的类型和数量
公共静态FragmentUpcomingContest newInstance(字符串param1,字符串param2){
FragmentUpcomingContest fragment=新的FragmentUpcomingContest();
Bundle args=新Bundle();
args.putString(ARG_PARAM1,PARAM1);
args.putString(ARG_PARAM2,PARAM2);
fragment.setArguments(args);
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!=null){
mParam1=getArguments().getString(ARG_PARAM1);
mParam2=getArguments().getString(ARG_PARAM2);
}
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
ArrayList ArrayList=新的ArrayList();
CodeforcesContest[]竞赛;
查看myLayout=充气机。充气(R.layout.fragment\u fragment\u即将到来的比赛,容器,错误);
ListView ListView=myLayout.findViewById(R.id.ListView);
试一试{
JSONArray JSONArray=新GetCodeforcesContestList().execute().get();
for(int i=0;i