JSON数据到ListView片段类



我看了很多主题,但没有找到任何答案。

问题是,我不知道如何在我的Fragment类中显示我的解析数据,它持有我的ListView,我尝试了各种不同的东西,但就是做不到。我需要显示它,每个团队(它就像一个排名)有它自己的线。我只能这样做,完整的解析数据显示在一个单一的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;
}

}

<

解析类/strong>

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

我希望你能帮我解决这个问题。

Pato94在正确的轨道上。

执行Task,但同时忽略返回的列表。

很容易:

  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();
}
  • 步骤:
  • 从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;
    }
    

    你似乎做的一切都很好,但jsonTask应该设置结果在数组列表你已经发送到你的适配器。

    设置这些值后,您需要调用adapter.notifyDataSetChanged()来显示数组列表的内容

    最新更新