简体   繁体   中英

JSON Data to ListView Fragment Class

i looked through many topics but couldn't find any answer to my problem..

The problem is that I don't know how to display my parsed data in my Fragment Class that holds my ListView, i tried all sort of different things but just couldn't do it. I need to display it that every team(it's like a ranking) has it's own line. I only could do it that the complete parsed data is shown in a single TextView..

My different classes(i have to do the app using fragments ..)

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??
    }
}

THE MODEL CLASS

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

I hope you can help me with my problem.

You seem to be doing everything all right, but the jsonTask should set the result in the arraylist you've sent to your adapter somehow.

After setting these values you need to call adapter.notifyDataSetChanged() in order to display the content of the arraylist

Pato94 is on the right track.

You execute the Task but at the same time you simply ignore the returned list.

It's easy:

  1. "onPostExecute" will be called after your task finishes.
  2. From there on you should give the list to "TabelleFragment" and put the items of that list into your adapter (which is only available in "TabelleFragment").
  3. Finally you must tell your adapter about these changes by calling "notifyDataSetChanged".

Here are some steps to fix that:

  1. Step:

Implement a Method to accept your parsed data in 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. Step:

Call this Method from onPostExecute():

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

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

Fill your View with data:

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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