【问题标题】:How do i populate a listview with json inside a fragment with tablayout and viewpager如何在带有 tablayout 和 viewpager 的片段中填充带有 json 的列表视图
【发布时间】:2023-03-15 10:41:01
【问题描述】:

如何在片段中使用 JSON 填充我的列表视图。该应用程序运行没有错误,但列表视图为空。我的片段中没有任何数据。但是当我将它作为一个活动而不是作为一个片段时,列表视图将填充 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();
    }

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

这是我的日志

【问题讨论】:

  • 检查你的json,可能是空的。
  • 先生,我已经检查过了。在浏览器中有一些数据

标签: android json listview android-fragments


【解决方案1】:

/* 设置以下变量 */

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

            }
        }
    }

【讨论】:

    猜你喜欢
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多