Listview не показывает никаких данных при использовании Fragment + Tablayout - PullRequest
0 голосов
/ 02 ноября 2019

У меня есть 7 семь вкладок, где я хочу показать данные из JSON. Используя один фрагмент, я генерирую 7 семи вкладок. Теперь я хочу показать данные в виде списка для загрузки данных JSON, я использовал пользовательский класс, который расширяет AsyncTask, и пользовательский адаптер расширяет BaseAdapter. Все выглядит нормально, без ошибок, но в Listview не отображаются данные.

Класс фрагмента

public class WeekFragment extends Fragment {
    public static final String ARG_PAGE = "ARG_PAGE";
    public static ListView listViewSchedule;


    private int mPage;

    public static WeekFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        WeekFragment fragment = new WeekFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_week, container, false);
        TextView textView = (TextView) view.findViewById(R.id.fragmentTextView);
        listViewSchedule = (ListView) view.findViewById(R.id.listViewSchedule);
        textView.setText("Fragment #" + mPage);

        new ScheduleDataLoad().execute();

        return view;
    }


}

Класс ScheduleDataLoad

public class ScheduleDataLoad extends AsyncTask<Void,Void,Void> {
    public String data ="";
    WeekFragment weekFragment = new WeekFragment();
    ScheduleAdapter scheduleAdapter = new ScheduleAdapter(weekFragment.getActivity());


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        try {
            JSONObject jsonObject = new JSONObject(data);

            JSONArray standingsArray = jsonObject.getJSONArray("schedule");
            for (int i = 0; i < standingsArray.length(); i++) {
                JSONObject standingsDetail = standingsArray.getJSONObject(i);
                scheduleAdapter.scheduleList.add(new ScheduleEntry(
                        standingsDetail.getString("week"),
                        standingsDetail.getString("teamX"),
                        standingsDetail.getString("teamY"),
                        standingsDetail.getString("teamXScore"),
                        standingsDetail.getString("teamYScore"),
                        standingsDetail.getString("teamXRecord"),
                        standingsDetail.getString("teamYRecord"),
                        standingsDetail.getString("date"),
                        standingsDetail.getString("status"),
                        standingsDetail.getString("location")
                ));
            }


            weekFragment.listViewSchedule.setAdapter(scheduleAdapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected Void doInBackground(Void... voids) {

        try {
            URL url = new URL("URL Here(Not showing my API)");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            String line = "";


            while (line != null) {
                line = bufferedReader.readLine();
                data = data + line;
                Log.d(TAG, "doInBackground: test 2" + data);
            }


        } catch (MalformedURLException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

Класс ScheduleAdapter

public class ScheduleAdapter extends BaseAdapter {

    ArrayList<ScheduleEntry> scheduleList;
    Context con;
    private static View row;

    public ScheduleAdapter(Context con) {
        this.con = con;
        scheduleList = new ArrayList<ScheduleEntry>();
    }

    @Override
    public int getCount() {
        return scheduleList.size();
    }

    @Override
    public Object getItem(int i) {
        return scheduleList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater layoutInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.schedule_layout, viewGroup, false);
        TextView teamX = (TextView)row.findViewById(R.id.teamX);

        ScheduleEntry scheduleEntry = scheduleList.get(i);

        teamX.setText(scheduleEntry.teamX);

        return row;
    }
}
...