我有一个场景,其中有三个标签: -
我已经解析了json,但我无法理解为什么数据没有显示出来。
可滑动标签代码如下 -
public class Post extends NavDrawerActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Products", "Jobs", "Services" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().getDecorView()
.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); // load
// titles
// navMenuIcons = getResources()
// .obtainTypedArray(R.array.nav_drawer_icons);// load icons from
set(navMenuTitles, navMenuIcons);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
工作代码片段如下: -
public class FragmentJobs extends BaseFragment {
// Log tag
private static final String TAG = FragmentProducts.class.getSimpleName();
private ProgressDialog pDialog;
private List<Jobs> jobsList = new ArrayList<Jobs>();
private ListView listView;
private CustomJobListAdapter jobAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_jobs, container,
false);
Popups.showToast("Jobs onCreateView called", getActivity());
listView = (ListView) rootView.findViewById(R.id.list);
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
getActivity().getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonObjectRequest postReq = new JsonObjectRequest(Request.Method.GET,
AppConfig.URL_GETPOSTS, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
hidePDialog();
try {
JSONArray jArr = response.getJSONArray("jobs");
for (int ia = 0; ia < jArr.length(); ia++) {
JSONObject obj = jArr.getJSONObject(ia);
Jobs jobs = new Jobs();
jobs.setAdded_date(obj.getString("added_date"));
jobs.setJob_name(obj.getString("job_name"));
jobs.setJob_category(obj
.getString("job_category"));
jobs.setThumbnailUrl(obj.getString("job_image"));
jobsList.add(jobs);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG,
" onErrorResponse FragmentJobs"
+ error.getMessage());
error.printStackTrace();
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(getActivity(), postReq);
jobAdapter = new CustomJobListAdapter(this, getActivity(), jobsList);
listView.setAdapter(jobAdapter);
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
ProductFragments代码如下: -
public class FragmentProducts extends BaseFragment {
// Log tag
private static final String TAG = FragmentProducts.class.getSimpleName();
private ProgressDialog pDialog;
private List<Products> productList = new ArrayList<Products>();
private ListView listView;
private CustomProductListAdapter productAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_products, container,
false);
Popups.showToast("Products onCreateView called", getActivity());
listView = (ListView) rootView.findViewById(R.id.list);
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
getActivity().getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonObjectRequest postReq = new JsonObjectRequest(Request.Method.GET,
AppConfig.URL_GETPOSTS, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
hidePDialog();
try {
JSONArray jArr = response.getJSONArray("products");
for (int ia = 0; ia < jArr.length(); ia++) {
JSONObject obj = jArr.getJSONObject(ia);
Products posts = new Products();
posts.setProduct_name(obj
.getString("product_name"));
posts.setThumbnailUrl(obj
.getString("product_image"));
posts.setProduct_category(obj
.getString("product_category"));
posts.setProduct_price(obj
.getString("product_price"));
posts.setDate(obj.getString("added_date"));
// adding posts to posts array
productList.add(posts);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG,
" onErrorResponse FragmentJobs"
+ error.getMessage());
error.printStackTrace();
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(getActivity(), postReq);
productAdapter = new CustomProductListAdapter(this, getActivity(),
productList);
listView.setAdapter(productAdapter);
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
ServicesFragment的代码如下: -
public class FragmentServices extends BaseFragment {
// Log tag
private static final String TAG = FragmentServices.class.getSimpleName();
private ProgressDialog pDialog;
private List<Jobs> jobsList = new ArrayList<Jobs>();
private List<Services> servicesList = new ArrayList<Services>();
private ListView listView;
private CustomServiceListAdapter servicesAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_services, container,
false);
Popups.showToast("Service onCreateView called", getActivity());
listView = (ListView) rootView.findViewById(R.id.list);
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
getActivity().getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonObjectRequest postReq = new JsonObjectRequest(Request.Method.GET,
AppConfig.URL_GETPOSTS, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
hidePDialog();
try {
JSONArray jArr = response.getJSONArray("jobs");
for (int ia = 0; ia < jArr.length(); ia++) {
JSONObject obj = jArr.getJSONObject(ia);
Jobs jobs = new Jobs();
jobs.setAdded_date(obj.getString("added_date"));
jobs.setJob_name(obj.getString("job_name"));
jobs.setJob_category(obj
.getString("job_category"));
jobs.setThumbnailUrl(obj.getString("job_image"));
jobsList.add(jobs);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG,
" onErrorResponse FragmentServices"
+ error.getMessage());
error.printStackTrace();
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(getActivity(), postReq);
servicesAdapter = new CustomServiceListAdapter(this, getActivity(),
servicesList);
listView.setAdapter(servicesAdapter);
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
样本json如下: -
{
success: "true",
products: [
{
product_id: "1",
product_code: "P001",
product_name: "Samsung Galaxy",
product_cateory: "Boats",
product_short_desc: "Samsung Galaxy",
product_long_desc: " Samsung Galaxy ",
product_price: "12000.00",
product_stock: "5",
added_date: "2015-03-12 00:00:00",
user_name: "jay23",
product_image: "http://karmickdev.com/eazy/uploads/product_image/medium_thumbs/cce84246260c8681c2576e6d3a09b1d3.jpeg"
},
{
product_id: "2",
product_code: "PRO74789",
product_name: "Test Image Another",
product_cateory: "Jewelry",
product_short_desc: "This is first test product in edit",
product_long_desc: " Long Description for a product ",
product_price: "12000.00",
product_stock: "200",
added_date: "2015-03-12 13:11:51",
user_name: "jay23",
product_image: "http://karmickdev.com/eazy/uploads/product_image/medium_thumbs/47f7b32186bbd62099d53835a66001be.jpeg"
},
{
product_id: "4",
product_code: "PRO63250",
product_name: "Test Product Title",
product_cateory: "Cell Phone",
product_short_desc: "This is short description of product",
product_long_desc: " This is long description of product ",
product_price: "20000.00",
product_stock: "200",
added_date: "2015-03-25 07:21:42",
user_name: "Jayatish",
product_image: "http://karmickdev.com/eazy/uploads/product_image/medium_thumbs/a7938d5fdb42710b6f6ee43f6966c620.JPG"
},
{
product_id: "6",
product_code: "PRO52149",
product_name: "a",
product_cateory: "Bikes",
product_short_desc: "this is nice product",
product_long_desc: " This is long description of product ",
product_price: "2.00",
product_stock: "0",
added_date: "2015-04-03 12:19:33",
user_name: "SouvikSinha",
product_image: "http://karmickdev.com/eazy/uploads/product_image/medium_thumbs/567b58142bf561596d537b42454229ed.jpg"
}
],
jobs: [
{
job_id: "3",
job_code: "JOB43070",
job_name: "Test Job Section",
job_category: "Admin/Office,Biotech/Science",
job_description: " This is second Job for new Test ",
added_date: "2015-03-12 11:18:43",
user_name: "Jay12",
job_image: "http://karmickdev.com/eazy/uploads/job_image/medium_thumbs/9ee09665fc31a105a2da2fcc9e95a120.jpg"
},
{
job_id: "4",
job_code: "JOB27838",
job_name: "This job is for Jayatish das",
job_category: "Accounting,Business/Management,Computer Science,Custom Service,Education,Food",
job_description: " Jayatish Das first test job section. ",
added_date: "2015-03-12 11:19:35",
user_name: "Jayatish",
job_image: "http://karmickdev.com/eazy/uploads/job_image/medium_thumbs/5b243b957427562cf1f47301934f3b9b.jpg"
},
{
job_id: "5",
job_code: "JOB63638",
job_name: "Sumita didi test job",
job_category: "Accounting,Finance,Admin/Office,Architecture/Engineering",
job_description: " Thsi is first test for Sumita Didi ",
added_date: "2015-03-24 14:30:41",
user_name: "Jayatish",
job_image: "http://karmickdev.com/eazy/uploads/job_image/medium_thumbs/1fb9b643205e5f599e8de02ce07a2958.jpg"
},
{
job_id: "6",
job_code: "JOB41434",
job_name: "Indian Railway Recruitment notification 2015 - 338 Apprentice Trainee Vacancy",
job_category: "Art/Media/Design,Computer Science,Custom Service",
job_description: " South Western Railway has issued a recruitment notification for the recruitment of Act Apprentice through recruitment notification South Western Railway Recruitment 2015 L/P.924/Act-App/Vol.XXX/2015 Date.25.03.2015. Candidates who have completed 10th, 12th, ITI can apply for the new recruitment notification Western Railway ",
added_date: "2015-03-27 11:27:01",
user_name: "Jayatish",
job_image: "http://karmickdev.com/eazy/uploads/job_image/medium_thumbs/52ca4f77c5ac405d66c41275de53a12b.JPG"
},
{
job_id: "11",
job_code: "JOB01456",
job_name: "new%20add%20job",
job_category: "Accounting,Finance,Art/Media/Design",
job_description: " this%20is%20a%20job%20of%20sleeping%20rate%20$2000%20per%20hour%20 ",
added_date: "2015-03-31 15:20:58",
user_name: "SouvikSinha",
job_image: "http://karmickdev.com/eazy/uploads/job_image/medium_thumbs/3c5188e850d4462ee88254b9bf4e435f.JPG"
},
{
job_id: "20",
job_code: "JOB92583",
job_name: "ygjhgjhgfhjmnhf",
job_category: "Accounting,Finance,Art/Media/Design",
job_description: " dtdfyddddli8.fddf8ufdu8f ",
added_date: "2015-04-01 08:04:52",
user_name: "SouvikSinha",
job_image: "http://karmickdev.com/eazy/uploads/job_image/medium_thumbs/0ab83ce2aa3699138b500d5e23712f1a.jpg"
},
{
job_id: "21",
job_code: "JOB25216",
job_name: "ygjhgjhgfhjmnhf",
job_category: "Accounting,Finance,Art/Media/Design",
job_description: " dtdfyddddli8.fddf8ufdu8f ",
added_date: "2015-04-01 08:05:06",
user_name: "SouvikSinha",
job_image: "http://karmickdev.com/eazy/uploads/job_image/medium_thumbs/4179ef985f337016ba9ba8dd9cb4a544.jpg"
},
{
job_id: "22",
job_code: "JOB81636",
job_name: "ygjhgjhgfhjmnhf",
job_category: "Accounting,Finance,Art/Media/Design",
job_description: " dtdfyddddli8.fddf8ufdu8fhnbvnbvn ",
added_date: "2015-04-01 08:06:53",
user_name: "SouvikSinha",
job_image: "http://karmickdev.com/eazy/uploads/job_image/medium_thumbs/86a8fce0d5e290865c00493ef1969e9e.jpg"
},
{
job_id: "23",
job_code: "JOB09814",
job_name: "nbvjvjh",
job_category: "Accounting,Finance,Admin/Office",
job_description: " gkbijbij ",
added_date: "2015-04-01 14:41:46",
user_name: "SouvikSinha",
job_image: "http://karmickdev.com/eazy/uploads/job_image/medium_thumbs/0f021bc28e72db6f8ce9b442b44465af.JPG"
},
{
job_id: "24",
job_code: "JOB29694",
job_name: "Test Job Again For Medium Image",
job_category: "Accounting,Finance",
job_description: " This is test for job image in Medium image ",
added_date: "2015-04-08 08:41:41",
user_name: "SouvikSinha",
job_image: "http://karmickdev.com/eazy/uploads/job_image/medium_thumbs/8426cfe6ccb230b520e0867c945312d1.JPG"
}
],
service: [
{
service_id: "1",
service_code: "SER63850",
service_name: "Test Service Section",
service_category: "Automotive,Gardening",
service_description: " This is first test section in service page ",
added_date: "2015-03-25 10:32:12",
user_name: "Jayatish",
service_image: "http://karmickdev.com/eazy/uploads/service_image/medium_thumbs/f16cbc8d61069ccef049ea8f59603c2f.jpeg"
},
{
service_id: "2",
service_code: "SERV0098",
service_name: "Serv Title",
service_category: "Computer,Financial,Household,Labor",
service_description: " Desk Service ",
added_date: "2015-03-26 00:00:00",
user_name: "Jayatish",
service_image: "http://karmickdev.com/eazy/uploads/service_image/medium_thumbs/a1bf16b788104a09243cc552e84d0711.jpeg"
},
{
service_id: "4",
service_code: "SER09294",
service_name: "This is first service section",
service_category: "Automotive,Computer,Gardening,Financial,Household,Pet",
service_description: " This is the first service test section for new customer. ",
added_date: "2015-03-27 13:48:04",
user_name: "Jayatish",
service_image: "http://karmickdev.com/eazy/uploads/service_image/medium_thumbs/a7ef640334755861985e5f816990f3d5.jpg"
},
{
service_id: "5",
service_code: "SER89410",
service_name: "new",
service_category: "Beauty,Computer",
service_description: " ggfgxfgxgfxgxcggxcggcxgvxcvxcvcxvcxvcxvxcvxvxvxvvcxxvvcvxv ",
added_date: "2015-04-03 12:01:53",
user_name: "SouvikSinha",
service_image: "http://karmickdev.com/eazy/uploads/service_image/medium_thumbs/c985904dc3068c245e5a9946f55159a4.jpg"
},
{
service_id: "6",
service_code: "SER92767",
service_name: "new",
service_category: "Beauty,Computer,Gardening,Household",
service_description: " ggfgxfgxgfxgxcggxcggcxgvxcvxcvcxvcxvcxvxcvxvxvxvvcxxvvcvxv ",
added_date: "2015-04-03 12:02:02",
user_name: "SouvikSinha",
service_image: "http://karmickdev.com/eazy/uploads/service_image/medium_thumbs/765f295c99661d276b1bf80d9a7991fe.jpg"
},
{
service_id: "7",
service_code: "SER43296",
service_name: "ffdghdh",
service_category: "Automotive,Beauty,Computer,Gardening,Household,Labor,Legal,Lessons",
service_description: " fgfdghdghfghdfhhdfhdhdhhfhfdhfdhdfhfdhfdhdfhfddfhfdhddhfghdhdhdhdhdghghghhddhghgdhd ",
added_date: "2015-04-03 12:11:11",
user_name: "SouvikSinha",
service_image: "http://karmickdev.com/eazy/uploads/service_image/medium_thumbs/191979c862d368518b8e52b888b4b934.jpg"
}
]
}
上面的json是一个json数组,我必须拉出指定的数组并放在指定的标签中
我无法找出我的错误。 我没有收到数据。 伙计们请帮忙
答案 0 :(得分:0)
你提供的Json没有通过JSonLint验证,可能是未加载数据的原因。
第1行的解析错误:{success:“true”,
----- ^期待'STRING','}'
所有的json标签都应该包含在这些内容中:“”
例如:
“成功”:“真实”
另外,我建议使用另一个系统来检查数据的检索,并刷新listview适配器,Adapter.notifyDataSetChanged()可以在将对象添加到列表后执行操作,但请查看greenrobot中的EventBus ,它是一个很容易管理数据检索事件的好库。
另外,在服务中你还在使用作业循环。
try {
JSONArray jArr = response.getJSONArray("jobs");
for (int ia = 0; ia < jArr.length(); ia++) {
JSONObject obj = jArr.getJSONObject(ia);
Jobs jobs = new Jobs();
jobs.setAdded_date(obj.getString("added_date"));
jobs.setJob_name(obj.getString("job_name"));
jobs.setJob_category(obj
.getString("job_category"));
jobs.setThumbnailUrl(obj.getString("job_image"));
jobsList.add(jobs);
}