如何使用tablayout和viewpager在片段中使用json填充listview

时间:2017-07-29 04:21:12

标签: android json listview android-fragments

如何在片段中使用JSON填充listview。应用程序运行时没有错误,但列表视图为空。我的片段中没有任何数据。但是当我把它作为一个活动而不是一个片段时,列表视图会填充JSON数据。

这是我的listview和json解析的片段代码

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

解决了,我忘了在manifest lol中添加权限

这是我的logcat Here is my logcat

1 个答案:

答案 0 :(得分:0)

/ *设置以下变量* /

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*/
    }

实现其他ListView回调,如下所示:

@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) {

            }
        }
    }