繁体   English   中英

JSON数据到ListView Fragment类

[英]JSON Data to ListView Fragment Class

我浏览了许多主题,但找不到解决我问题的答案。

问题是我不知道如何在保存ListView的Fragment类中显示解析后的数据,我尝试了各种不同的方法,但是做不到。 我需要证明每个团队(就像一个排名)都有自己的路线。 我只能将完整的解析数据显示在单个TextView中。

我不同的班级(我必须使用片段来做应用程序。)

表类

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);
}

适配器类

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;

}

}

上课

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 + '\'' +
            '}';
}

我希望你能帮助我解决我的问题。

您似乎一切正常,但是jsonTask应该在以某种方式发送到适配器的arraylist中设置结果。

设置这些值之后,您需要调用adapter.notifyDataSetChanged()来显示arraylist的内容

Pato94在正确的轨道上。

您执行了任务,但是同时您只是忽略了返回的列表。

这很容易:

  1. 任务完成后,将调用“ onPostExecute”。
  2. 从那里开始,您应该将列表提供给“ TabelleFragment”,并将该列表中的项目放入适配器(仅在“ TabelleFragment”中可用)。
  3. 最后,您必须通过调用“ notifyDataSetChanged”将这些更改告知适配器。

以下是一些修复步骤:

  1. 步:

在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();
}
  1. 步:

从onPostExecute()调用此方法:

@Override
protected void onPostExecute(final List<FootballModel> result) {
    super.onPostExecute(result);

    tabelleFragment.onParseFinished(result);
}
  1. 步:

用数据填充视图:

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;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM