بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
نمایش ریسایکلر ویو در فرگمنت (خواهشا راهنمایی کنید)
سلام وقتتون بخیر
می خوام کد زیر رو داخل یک فرگمنت پیدا سازی کنم
ممنون میشم راهنمایی کنید (ضروریه)
public class MainActivity extends AppCompatActivity {
ArrayList<MainData> allSampleData;
String title,price,image;
MainAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
allSampleData=new ArrayList<>();
RecyclerView recyclerView=(RecyclerView)findViewById(R.id.my_recyclerview);
new GetJSON(MainActivity.this).execute();
recyclerView.setHasFixedSize(true);
adapter=new MainAdapter(allSampleData,this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
public class GetJSON extends AsyncTask<Void,Void,String>{
ProgressDialog dialog=new ProgressDialog(MainActivity.this);
Context context;
public GetJSON(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("در حال دریافت اطلاعات...");
dialog.setCancelable(false);
dialog.show();
}
@Override
protected String doInBackground(Void... voids) {
String json="http://www.uploadina.com/918226ddf71a978d/sample.json";
return JSONParse.GetJSON(json);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (dialog.isShowing()){
dialog.dismiss();
}
if (s!=null){
try{
JSONObject jo=new JSONObject(s);
for (int j=1;j<=jo.length();j++) {
MainData dm = new MainData();
if (j == 1) {
dm.setHeaderTitle("بازی");
ArrayList<ChildData> singleItem = new ArrayList<ChildData>();
JSONArray game = jo.getJSONArray("game");
for (int i = 0; i < game.length(); i++) {
JSONObject object = game.getJSONObject(i);
title = object.getString("name");
price = object.getString("price");
image = object.getString("image");
singleItem.add(new ChildData(title, price, image));
}
dm.setAllItemsinSection(singleItem);
} else if (j == 2) {
dm.setHeaderTitle("آب و هوا");
ArrayList<ChildData> singleItem1 = new ArrayList<ChildData>();
JSONArray weather = jo.getJSONArray("weather");
for (int i = 0; i < weather.length(); i++) {
JSONObject object = weather.getJSONObject(i);
title = object.getString("name");
price = object.getString("price");
image = object.getString("image");
singleItem1.add(new ChildData(title, price, image));
}
dm.setAllItemsinSection(singleItem1);
} else if (j == 3) {
dm.setHeaderTitle("ورزشی");
ArrayList<ChildData> singleItem2 = new ArrayList<ChildData>();
JSONArray sport = jo.getJSONArray("sport");
for (int i = 0; i < sport.length(); i++) {
JSONObject object = sport.getJSONObject(i);
title = object.getString("name");
price = object.getString("price");
image = object.getString("image");
singleItem2.add(new ChildData(title, price, image));
}
dm.setAllItemsinSection(singleItem2);
}else if (j == 4) {
dm.setHeaderTitle("طراحی و هنر");
ArrayList<ChildData> singleItem3 = new ArrayList<ChildData>();
JSONArray art = jo.getJSONArray("art");
for (int i = 0; i < art.length(); i++) {
JSONObject object = art.getJSONObject(i);
title = object.getString("name");
price = object.getString("price");
image = object.getString("image");
singleItem3.add(new ChildData(title, price, image));
}
dm.setAllItemsinSection(singleItem3);
}
else if (j == 5) {
dm.setHeaderTitle("اخبار و رویداد");
ArrayList<ChildData> singleItem4 = new ArrayList<ChildData>();
JSONArray news = jo.getJSONArray("news");
for (int i = 0; i < news.length(); i++) {
JSONObject object = news.getJSONObject(i);
title = object.getString("name");
price = object.getString("price");
image = object.getString("image");
singleItem4.add(new ChildData(title, price, image));
}
dm.setAllItemsinSection(singleItem4);
}
allSampleData.add(dm);
adapter.notifyDataSetChanged();
}
}catch (JSONException e){
e.printStackTrace();
}
}
}
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
}
برای این سوال پاسخی وجود ندارد.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .