首页 文章

ContentAdapter在Android中使用JSON数据

提问于
浏览
1

我正在使用的Android应用程序使用片段进行选项卡导航

我的片段活动/视图中有一个ContentAdapter,它使用res / values / arrays.xml中的数据(如下所示)来呈现标签内容

res/values/arrays.xml

<resources>
  <string-array name="names">
    <item>an item A</item>
    <item>an item B</item>
  </string-array>
  <string-array name="descs">
    <item>this is desc for item A</item>
    <item>this is desc for item B</item>
  </string-array>
</resources>

java/my.app.namespace/MyFragmentList.java Adapter

public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
    // Set numbers of List in RecyclerView.
    private static final int LENGTH = 18;

    private final String[] mDesc;
    private final String[] mName;

    public ContentAdapter(Context context) {

        Resources resources = context.getResources();
        mDesc = resources.getStringArray(R.array.descs);
        mName = resources.getStringArray(R.array.names);

        a.recycle();
    }

我可以看到在公共ContentAdapter()内部调用资源文件,并设置值以供以后访问public void onBindViewHolder(ViewHolder holder,int position),它将呈现xml中的每个项目 .

我想访问公共ContentAdapter(Context上下文)中的外部JSON URL并从那里设置数组值 .

public ContentAdapter(Context context) {

        //Resources resources = context.getResources();
        //mDesc = resources.getStringArray(R.array.descs);
        //mName = resources.getStringArray(R.array.names);

        //get JSON URL Data (ASYNC?)
        mName = set name from json;
        //...

        a.recycle();
    }

不确定在应用程序中此时获取JSON的最佳方法是什么 . 在加载MainActivity之前是否下载JSON或者此时是否进行JSON提取?

1 回答

  • 0

    正如我在评论中提到的那样:

    您不应该在适配器中加载数据,首先应该在相应的activity / fragment中加载/获取并传递对适配器的引用 . 因此,首先在活动中获取您的json数据,在那里解析它,然后将结果列表传递给适配器 .

    你应该遵循以下方法:

    在您的活动中:

    private class GetContentTask extends AsyncTask<Void, Void, JSONObject> {
    
        public GetContentTask() {
        }
    
        @Override
        protected JSONObject doInBackground(Void... voids) {
            // fetch JSON from api
           JSONObject jsonObject = new JSONObject();
            //you can pass string or json object whatever is preferred
            return jsonObject;
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //show progress
        }
    
        @Override
        protected void onPostExecute(final JSONObject result) {
            super.onPostExecute(result);
            //hide progress
            ArrayList<String> mDesc; // parse array from result
            ArrayList<String> mName; // parse array from result
    
            //set your adapter
            ContentAdapter adapter = new ContentAdapter(context,mDesc,mName);
    
            //or call your method and pass data in it
            setupViewPager(viewPager,mDesc,mName);
        }
    }
    

    你的适配器类:

    public class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
    
        private final ArrayList<String> mDesc;
        private final ArrayList<String> mName;
    
        public ContentAdapter(Context context,ArrayList<String> mDesc,ArrayList<String> mName) {
            this.mDesc = mDesc;
            this.mName = mName;
        }
    }
    

    希望这会有所帮助 .

相关问题