我有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;
}
}