简体   繁体   中英

How do i populate a listview with json inside a fragment with tablayout and viewpager

how do i populate my listview with JSON inside a fragment. The app runs with no error, but the listview is empty. I am not getting any data inside my fragment. But when i make it as an activity and not as a fragment the listview is populated with JSON data.

Here is my fragment code with listview and json parsing

public class News extends Fragment {
    private ListView lv;
    private String JSON_STRING;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(news, container, false);
        lv = (ListView) rootView.findViewById(R.id.listView3);

        getJSON();

        return rootView;

    }




    private void showResult(){
        JSONObject jsonObject;
        ArrayList<HashMap<String,String>> list = new ArrayList<>();
        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY1);

            for(int i = 0; i<result.length(); i++){
                JSONObject jo = result.getJSONObject(i);
                String title = jo.getString(Config.TAG_title);
                HashMap<String,String> match = new HashMap<>();               
                match.put(Config.TAG_title,title);
                list.add(match);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(
                getActivity(), list, R.layout.newsadapterlayout,
                new String[]{Config.TAG_title,Config.TAG_content, Config.TAG_n_date, Config.TAG_NID},
                new int[]{ R.id.title});
        lv.setAdapter(adapter);
    }
    private void getJSON(){
        class GetJSON extends AsyncTask<Void,Void,String> {


            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                JSON_STRING = s;
                showResult();
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequest(Config.URL_NEWS);
                return s;
            }
        }
        GetJSON gj = new GetJSON();
        gj.execute();
    }

Solved it, I forgot to add permissions in manifest lol

Here is my logcat 这是我的日志

/* setup these below variables */

private int currentScrollState, currentFirstVisibleItem, currentVisibleItemCount, currentTotalItemCount;       

     if (lv.size() > 0) {
           ListAdapter adapter = new SimpleAdapter(
            getActivity(), list, R.layout.newsadapterlayout,
            new String[]{Config.TAG_title,Config.TAG_content, Config.TAG_n_date, Config.TAG_NID},
            new int[]{ R.id.title});

            adapter.notifyDataSetChanged();
            list.setAdapter(adapter);
            lv.setSelectionFromTop(currentFirstVisibleItem, 0); /* you missed here*/
    }

Implement other ListView callbacks something like below:

@Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        this.currentScrollState = scrollState;
        this.isScrollCompleted();
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                         int totalItemCount) {
        this.currentFirstVisibleItem = firstVisibleItem;
        this.currentVisibleItemCount = visibleItemCount;
        this.currentTotalItemCount = totalItemCount;
    }

    private void isScrollCompleted() {

        if (currentFirstVisibleItem + currentVisibleItemCount >= currentTotalItemCount) {
            if (this.currentVisibleItemCount > 0 && this.currentScrollState == OnScrollListener.SCROLL_STATE_IDLE) {

            }
        }
    }

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