Android 使用JSON的customadapter中的服务器端值
//CustomAdapter.javaAndroid 使用JSON的customadapter中的服务器端值,android,json,custom-adapter,Android,Json,Custom Adapter,//CustomAdapter.java public class CustomAdapter extends PagerAdapter{ Context context; int[] imageId = {R.drawable.slider1, R.drawable.slider2}; String[] caption = { "1", "2" }; public CustomAdapter(Context context){ this.context = context; }
public class CustomAdapter extends PagerAdapter{
Context context;
int[] imageId = {R.drawable.slider1, R.drawable.slider2};
String[] caption = { "1", "2" };
public CustomAdapter(Context context){
this.context = context;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View viewItem = inflater.inflate(R.layout.image_item, container, false);
ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView);
TextView textView = (TextView) viewItem.findViewById(R.id.textView1);
imageView.setImageResource(imageId[position]);
textView.setText(caption[position]);
((ViewPager)container).addView(viewItem);
return viewItem;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageId.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == ((View)object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
}
我的结果是
{
"slider": [
{
"name": "Demo Event",
"images": "uploads\/event\/demo.png",
"location": "Ernakulam"
},
{
"name": "cfghedrgyedyg",
"images": "uploads\/event\/Array466.jpg",
"location": "dfgred"
},
{
"name": "Demo",
"images": "uploads\/event\/Array698.jpg",
"location": "Thodupuzha"
},
{
"name": "Event",
"images": "uploads\/event\/Array745.jpg",
"location": "Angamaly"
},
{
"name": "ghtrfhy",
"images": "uploads\/event\/Array350.jpg",
"location": "Thodupuzha"
}
]
}
我的问题是,如何更改int[]imageId={R.drawable.slider1,R.drawable.slider2}中的值代码>和字符串[]标题={“1”,“2”}使用来自服务器的值编写>代码。即,使用JSON
是否有可能实现这一点。
我试了很多。但是没有成功
提前感谢。创建这样的业务实体/类
public class ImageDetails()
{
public int ImageId;
public String Caption;
}
创建此类作为对象的列表并填充详细信息
List<ImageDetails> imgDetails=new ArrayList<ImageDetails>();
ImageDetails img1=new ImageDetails();
img1.ImageId=1;
img1.Caption="Caption";
发布您的json响应,然后通过网络调用和all@KarthikaPB:-在此customadapter中,如何从服务器获取值。@Anjalitripath:-int[]imageId={JSON结果中的“images”字段}
和String[]caption={JSON结果中的“name”字段}
public CustomAdapter(Context context,List<ImageDetails> imageDetails){
this.context = context;
this.ImageDetails=imageDetails;
}
TextView textView = (TextView) viewItem.findViewById(R.id.textView1);
textView.setText(ImageDetails.get(position).Caption);