我浏览了很多主题,但找不到任何问题的答案..
问题是我不知道如何在包含我的ListView的Fragment类中显示我的解析数据,我尝试了各种不同的东西,但却无法做到。 我需要证明每个团队(就像排名一样)拥有它自己的路线。 我只能这样做,完整的解析数据显示在一个TextView ..
我的不同课程(我必须使用使用片段 ...)
TABLE CLASS
public class TabelleFragment extends Fragment {;
JSONTask jsonTask;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tabelle_fragment, container, false);
ArrayList<FootballModel> arrayList = new ArrayList<>();
FootballAdapter adapter = new FootballAdapter(getActivity().getApplicationContext(), arrayList);
ListView listView = (ListView) view.findViewById(R.id.listRanking);
listView.setAdapter(adapter);
Button buttonDL = (Button) view.findViewById(R.id.buttonDL);
buttonDL.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view2) {
String result = "https://www.dein-weg-in-die-cloud.de/tomcat7/RestSoccer/fussball/tabelle";
startURLFetch(result);
}
}
);
FootballModel fm = new FootballModel();
arrayList.add(fm);
return view;
}
protected void startURLFetch(String result) {
jsonTask = new JSONTask(this);
jsonTask.execute(result);
}
ADAPTER CLASS
public class FootballAdapter extends ArrayAdapter<FootballModel> {
public FootballAdapter(Context context, ArrayList<FootballModel> arrayList) {
super(context, R.layout.football_adapter_item, arrayList);
}
@Override
public View getView(int position, View myView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = vi.inflate(R.layout.football_adapter_item, null);
FootballModel fm = new FootballModel();
fm = getItem(position);
//TODO TextViews
return myView;
}
}
PARSE CLASS
public class JSONTask extends AsyncTask<String, String, List<FootballModel>> {
TabelleFragment tabelleFragment;
public JSONTask(TabelleFragment f) {
this.tabelleFragment = f;
}
@Override
protected List<FootballModel> doInBackground(String... params) {
HttpsURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpsURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("tabelle");
List<FootballModel> footballModelList = new ArrayList<>();
for(int i=0; i<parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
FootballModel input = new FootballModel();
input.setId(finalObject.getString("id"));
input.setName(finalObject.getString("name"));
input.setTore(finalObject.getString("tore"));
input.setPunkte(finalObject.getString("punkte"));
input.setSpiele(finalObject.getString("spiele"));
//hinzufügen des fertigen Objektes
footballModelList.add(input);
}
return footballModelList;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if(connection != null) {
connection.disconnect();
}
try {
if(reader !=null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(final List<FootballModel> result) {
super.onPostExecute(result);
//TODO: need to set data to the list??
}
}
模特课程
public class FootballModel {
String id;
int image;
String name;
String tore;
String punkte;
String spiele;
public FootballModel(String id, int image, String name, String tore, String punkte, String spiele) {
this.id = id;
this.image = image;
this.name = name;
this.tore = tore;
this.punkte = punkte;
this.spiele = spiele;
}
public FootballModel() {
}
public void setId(String id) {
this.id = id;
}
public void setImage(int image) {
this.image = image;
}
public void setName(String name) {
this.name = name;
}
public void setTore(String tore) {
this.tore = tore;
}
public void setPunkte(String punkte) {
this.punkte = punkte;
}
public void setSpiele(String spiele) {
this.spiele = spiele;
}
public String getId() {
return id;
}
public int getImage() {
return image;
}
public String getName() {
return name;
}
public String getTore() {
return tore;
}
public String getPunkte() {
return punkte;
}
public String getSpiele() {
return spiele;
}
@Override
public String toString() {
return "FootballModel{" +
"id='" + id + '\'' +
", image='" + image + '\'' +
", name='" + name + '\'' +
", tore='" + tore + '\'' +
", punkte='" + punkte + '\'' +
", spiele='" + spiele + '\'' +
'}';
}
我希望你能解决我的问题。
答案 0 :(得分:0)
你似乎正在做所有事情,但是jsonTask应该在你以某种方式发送给你的适配器的arraylist中设置结果。
设置这些值后,您需要调用adapter.notifyDataSetChanged()以显示arraylist的内容
答案 1 :(得分:0)
Pato94走在正确的轨道上。
您执行任务但同时只是忽略返回的列表。
很简单:
以下是修复该问题的一些步骤:
实现一个方法来接受TabelleFragment中的解析数据:
public void onParseFinished(final List<FootballModel> result) {
//todo: fill data from result into your adapter
//todo: inform your adapter with adapter.notifyDataSetChanged()
//info: the adapter will inform the ui to update the view
//example code below
adapter.addAll(result);
//not sure if this is really needed. Try with / without this
adapter.notifyDataSetChanged();
}
从onPostExecute()调用此方法:
@Override
protected void onPostExecute(final List<FootballModel> result) {
super.onPostExecute(result);
tabelleFragment.onParseFinished(result);
}
使用数据填充您的视图:
public View getView(int position, View myView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = vi.inflate(R.layout.football_adapter_item, null);
FootballModel fm = new FootballModel();
fm = getItem(position);
//TODO TextViews
//------------------------------------------//
TextView text = myView.findViewByID(...);
text.setText(...);
//------------------------------------------//
return myView;
}