我正在尝试编写一个简单的Andriod应用 已实现一个功能: 读取Web API的json数据显示在textview中,并且工作正常。 当我想在视图页面中显示它时,布局已正确加载,但读取json数据的类未加载。
此代码用于查看页面
public class dboElc_5min extends AppCompatActivity {
private ViewPager pager = null;
private MyPagerAdapter adapter = null;
private View[] views = new View[2];
private int[] viewId = {R.layout.activity_dbo_elc_5min_1, R.layout.activity_dbo_elc_5min_2};
private List<View> viewList = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dbo_elc_5min);
initViews();
adapter = new MyPagerAdapter(viewList);
pager.setAdapter(adapter);
}
private void initViews() {
pager = (ViewPager) findViewById(R.id.id_viewpager);
viewList = new ArrayList<>();
for (int i = 0; i < viewId.length; i++) {
views[i] = View.inflate(this, viewId[i], null);
viewList.add(views[i]);
}
}
}
此代码是页面
public class dboElc_5min_1 extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout mSwipeRefreshLayout;
TextView mTextViewResult;
RequestQueue mQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dbo_elc_5min_1);
mQueue = Volley.newRequestQueue(this);
jsonParse();
mTextViewResult = findViewById(R.id.text_view_result);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_green_light);
}
public void jsonParse()
{
String url ="http://***/test/api/ELC_5min";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray array= response.getJSONArray("Elc_5min");
for(int i=0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
String XElc_A_Vrs = object.getString("XElc_A_Vrs");
String XElc_A_Vst = object.getString("XElc_A_Vst");
String XElc_A_Vtr = object.getString("XElc_A_Vtr");
String XElc_A_Ir = object.getString("XElc_A_Ir");
String XElc_A_Is = object.getString("XElc_A_Is");
String XElc_A_It = object.getString("XElc_A_It");
String Elc_A_KWH = object.getString("Elc_A_KWH");
mTextViewResult.setText(" "+"RS : "+XElc_A_Vrs+"V"+"\n"+
" "+"ST : "+XElc_A_Vst+"V"+"\n"+
" "+"TR : "+XElc_A_Vtr+"V"+"\n\n\n"+
" "+"R : " +XElc_A_Ir+"A"+"\n"+
" "+"S : " +XElc_A_Is+"A"+"\n"+
" "+"T : " +XElc_A_It+"A"+"\n"+
" " +Elc_A_KWH+"KWH"+"\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
@Override
public void onRefresh() {
jsonParse();
mSwipeRefreshLayout.setRefreshing(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
},500);
}
}
这是我的PageAdapter
public class MyPagerAdapter extends PagerAdapter{
private List<View> viewList ;
public MyPagerAdapter(List<View> viewList) {
this.viewList = viewList;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
}
@Override
public int getCount() {
return viewList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
实施的结果是,仅捕获了布局,而我的json数据无法正确显示。