ورود

View Full Version : کمک استفاده از ViewPager به کمک json



so2011
جمعه 26 تیر 1394, 15:32 عصر
من viewpager دارم که اطلاعاتش رو از آرایه ی مشخص شده میگیره . حالا من چه طور میتونم بجای اینکه داده ها را از array بگیره,از json بگیره!؟ لطفا کمک کنید...
ینی می خوام اطلاعات رو از http://myurl.ir/json بگیره
این کد هاش:

public class MainActivity extends Activity {

// Declare Variables
ViewPager viewPager;
PagerAdapter adapter;
String[] rank;
String[] country;
String[] population;
int[] flag;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from viewpager_main.xml
setContentView(R.layout.viewpager_main);

// Generate sample data
rank = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };

country = new String[] { "China", "India", "United States",
"Indonesia", "Brazil", "Pakistan", "Nigeria", "Bangladesh",
"Russia", "Japan" };

population = new String[] { "1,354,040,000", "1,210,193,422",
"315,761,000", "237,641,326", "193,946,886", "182,912,000",
"170,901,000", "152,518,015", "143,369,806", "127,360,000" };

flag = new int[] { R.drawable.china, R.drawable.india,
R.drawable.unitedstates, R.drawable.indonesia,
R.drawable.brazil, R.drawable.pakistan, R.drawable.nigeria,
R.drawable.bangladesh, R.drawable.russia, R.drawable.japan };

// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(MainActivity.this, rank, country, population, flag);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);

}
}

و اینم adaper

public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
String[] rank;
String[] country;
String[] population;
int[] flag;
LayoutInflater inflater;

public ViewPagerAdapter(Context context, String[] rank, String[] country,
String[] population, int[] flag) {
this.context = context;
this.rank = rank;
this.country = country;
this.population = population;
this.flag = flag;
}

@Override
public int getCount() {
return rank.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

// Declare Variables
TextView txtrank;
TextView txtcountry;
TextView txtpopulation;
ImageView imgflag;

inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
View itemView = inflater.inflate(R.layout.viewpager_item, container,
false);

// Locate the TextViews in viewpager_item.xml
txtrank = (TextView) itemView.findViewById(R.id.rank);
txtcountry = (TextView) itemView.findViewById(R.id.country);
txtpopulation = (TextView) itemView.findViewById(R.id.population);

// Capture position and set to the TextViews
txtrank.setText(rank[position]);
txtcountry.setText(country[position]);
txtpopulation.setText(population[position]);

// Locate the ImageView in viewpager_item.xml
imgflag = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set to the ImageView
imgflag.setImageResource(flag[position]);

// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);

return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);

}
}

Pedram_mrn
جمعه 26 تیر 1394, 19:17 عصر
خیلی خیلی اتفاقی این سوال شمارو تو سرچ گوگل دیدم...

شما json رو از کجا میگیری از وب سرویسی خاصی؟ اگه آره بهت پیشنهاد میکنم از کتابخونه retrofit استفاده کنی json هات رو هم خودش برات پارس میکنه به کلاسی که براش نوشتی و یه POJO بهت میده.

اگه نه خودت میتونی از کتابخونه هایی مثل Gson و jackson استفاده کنی و json رو parse کنی و ادپترتو بسازی.

شاید یکم گنگ به نظر برسه جوابم اگه مبتدی هستی، ولی راه درست استفاده از json همینه برو خودت یه سرچ بزن دستت میاد، دنبال منبع فارسی هم نباش.