使用Volley的流行阵列列表并移动到片段

时间:2015-09-21 18:10:50

标签: android android-fragments android-volley

如何获取由截击请求填充的数据列表,因为该片段正在获取一个已在主活动中实例化的空列表。

主要活动

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private ViewPager pager;
private ViewPageAdapter adapter;
private SlidingTabLayout tabs;
private int numTabs = 2;
private ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

private ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sendJsonRequest();

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

    adapter =  new ViewPageAdapter(getSupportFragmentManager(),titles,numTabs);

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true);

    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            //return getResources().getColor(R.color.tabsScrollColor);
            return ContextCompat.getColor(getApplicationContext(), R.color.cgred);
        }
    });

    tabs.setViewPager(pager);
    ////////////////////////////////////////



   // Toast.makeText(getApplicationContext(),getList().toString(),Toast.LENGTH_LONG).show();

}

此方法发出对Web服务数据的请求,并使用方法parseJson

填充数组
public void sendJsonRequest() {

    RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, BCWATCH, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            parseJson(response); // This method populates the list instantiated at the beginning of the main activity
            progressDialog.dismiss();

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(),"Error!",Toast.LENGTH_LONG).show();
            progressDialog.dismiss();

        }
    });

    queue.add(jsonObjectRequest); 

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Getting data...");
    progressDialog.show();

}

片段

我的片段收到一个空列表,我相信这是因为请求的异步执行,但是如何获取片段中的完整列表?

public class Tab1 extends android.support.v4.app.Fragment {

HashMap<String, String> obj;
ArrayList<HashMap<String, String>> list;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    list = ((MainActivity) getActivity() ).getList();

    if(list != null){
        //Toast.makeText(getContext(),"Not null",Toast.LENGTH_LONG).show();

        for (HashMap<String, String> entry : list) {
            Set<String> keys = entry.keySet();
            for (String key : keys) {
                String value = entry.get(key);
                Toast.makeText(getContext(),"key:"+key+" value:"+value,Toast.LENGTH_LONG).show();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你可以使用fragment的构造函数填充数组列表,并在那里设置适配器for example否则你应该自己在片段中发出JSON请求。