-1
高科技大家我已經使用JSONS每個標籤包含其中該標籤的所需的數據,將顯示有再循環器視圖中創建的選項卡,但其中在輸出相同的數據在每一個標籤顯示和還一個項目在它顯示每個標籤由具有在recyclerview相同數據
JSON
{
"status": "Success",
"message": "Menu is Listed Here",
"menu1": [{
"category": "Women",
"items": [{
"id": "41",
"title": "Plain Classic Abaya "
}, {
"id": "45",
"title": "Modest Abaya Collection "
}, {
"id": "48",
"title": "Khaleejia Abaya "
}, {
"id": "53",
"title": "Modern Jersy Classic Abaya"
}, {
"id": "54",
"title": "Sophisticated Abayas"
}, {
"id": "55",
"title": "Khaleejia Kaftans "
}, {
"id": "56",
"title": "Jersey Maxi Kaftans "
}, {
"id": "57",
"title": "Wedding Abaya"
}, {
"id": "58",
"title": "Latest Trend Abaya"
}]
}, {
"category": "Kids",
"items": [{
"id": "40",
"title": "Girls Abaya Plain "
}, {
"id": "52",
"title": "Girls Abaya Modern designs "
}]
}, {
"category": "Men",
"items": [{
"id": "42",
"title": "Modern Stylish Thobes "
}, {
"id": "46",
"title": "Embroidery Thobes "
}, {
"id": "47",
"title": "Simple Plain Thobes "
}]
}],
"wholesale": "Wholesale Products"
}
TabsFragment.java
public class TabsFragment extends Fragment {
RecyclerView recycler_view;
Recycler_Adapter recycler_adapter;
public TabsFragment() {
}
public static Fragment getInstance(int position) {
OneFragment fragment = new OneFragment();
Bundle bundle = new Bundle();
bundle.putInt("position", position);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
recycler_view = (RecyclerView) view.findViewById(R.id.recycler_view);
int position = getArguments().getInt("position", 0);
catmethod();
return view;
}
private void catmethod() {
String url = "http://www.souqalkhaleejia.com/webapis/categories.php";
JsonObjectRequest catreq = new JsonObjectRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray menuarray = response.getJSONArray("menu1");
for (int i = 0; i < menuarray.length(); i++) {
JSONObject menuobj = menuarray.getJSONObject(i);
JSONArray itemarray = menuobj.getJSONArray("items");
for (int j = 0; j < itemarray.length(); j++) {
JSONObject itemobj = itemarray.getJSONObject(j);
TestPojo test = new TestPojo();
test.setIdstring(itemobj.getString("id"));
test.setTitlestring(itemobj.getString("title"));
recycler_adapter = new Recycler_Adapter(getActivity(), itemarray);
recycler_view.setLayoutManager(new LinearLayoutManager(getActivity()));
recycler_view.setAdapter(recycler_adapter);
recycler_adapter.notifyDataSetChanged();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("CatMenu", String.valueOf(error));
}
});
AppController.getInstance().addToRequestQueue(catreq);
}
}
RecyclerViewAdapter.java
public class Recycler_Adapter extends RecyclerView.Adapter<ViewHolder> {
private Context context;
private JSONArray jsonArray;
public Recycler_Adapter(Context context, JSONArray jsonArray) {
this.context = context;
this.jsonArray = jsonArray;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View namelist = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_list_row, parent, false);
ViewHolder viewHolder = new ViewHolder(namelist);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
try {
for (int i=0;i<jsonArray.length();i++){
String idname = jsonArray.getJSONObject(position).getString("id");
String titlename = jsonArray.getJSONObject(position).getString("title");
holder.cat_item_id.setText(idname);
holder.cat_item_name.setText(titlename);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return jsonArray.length();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabs;
ViewPager viewpager;
List<TestPojo> testlit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
tabs = (TabLayout) findViewById(R.id.tabs);
viewpager = (ViewPager) findViewById(R.id.viewpager);
testlit=new ArrayList<TestPojo>();
catmethod();
}
private void catmethod() {
String url = "http://www.souqalkhaleejia.com/webapis/categories.php";
JsonObjectRequest catreq = new JsonObjectRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray menuarray = response.getJSONArray("menu1");
for (int i = 0; i < menuarray.length(); i++) {
JSONObject menuobj = menuarray.getJSONObject(i);
tabs.addTab(tabs.newTab().setText(menuobj.getString("category")));
test.setTitlestring(itemobj.getString("title"));
}
}
TabsAdapter tabsAdapter = new TabsAdapter(getSupportFragmentManager(), menuarray);
viewpager.setAdapter(tabsAdapter);
tabs.setupWithViewPager(viewpager);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("CatMenu", String.valueOf(error));
}
});
AppController.getInstance().addToRequestQueue(catreq);
}
}
TabsAdapter.class
public class TabsAdapter extends FragmentPagerAdapter {
private JSONArray jsonArray;
public TabsAdapter(FragmentManager manager, JSONArray jsonArray) {
super(manager);
this.jsonArray = jsonArray;
}
@Override
public Fragment getItem(int position) {
return OneFragment.getInstance(position);
}
@Override
public int getCount() {
return jsonArray.length();
}
@Override
public CharSequence getPageTitle(int position) {
String stringarray= null;
try {
stringarray = jsonArray.getJSONObject(position).getString("category");
} catch (JSONException e) {
e.printStackTrace();
}
return stringarray;
}
}
不,先生沒有工作 – prominere
PLZ讀catmethod在TestFragment和寫入使用每一行的。 –
它不接受編輯先生 – prominere