使用 JSON 的自定义适配器内的服务器端值
server side values inside customadapter using JSON
//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;
}
@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);
}
}
我的 json 结果是,
{
"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};
和 String[] caption = { "1", "2" };
中的值。即,使用 JSON.
是否可以实现这个。
我试了很多。但是没有成功。
提前致谢。
像这样创建一个企业entity/class
public class ImageDetails()
{
public int ImageId;
public String Caption;
}
创建此 class 作为对象的列表并填充详细信息
List<ImageDetails> imgDetails=new ArrayList<ImageDetails>();
ImageDetails img1=new ImageDetails();
img1.ImageId=1;
img1.Caption="Caption";
更改适配器的构造函数以接受此列表
public CustomAdapter(Context context,List<ImageDetails> imageDetails){
this.context = context;
this.ImageDetails=imageDetails;
}
终于在getview中了
TextView textView = (TextView) viewItem.findViewById(R.id.textView1);
textView.setText(ImageDetails.get(position).Caption);
//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;
}
@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);
}
}
我的 json 结果是,
{
"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};
和 String[] caption = { "1", "2" };
中的值。即,使用 JSON.
是否可以实现这个。 我试了很多。但是没有成功。
提前致谢。
像这样创建一个企业entity/class
public class ImageDetails()
{
public int ImageId;
public String Caption;
}
创建此 class 作为对象的列表并填充详细信息
List<ImageDetails> imgDetails=new ArrayList<ImageDetails>();
ImageDetails img1=new ImageDetails();
img1.ImageId=1;
img1.Caption="Caption";
更改适配器的构造函数以接受此列表
public CustomAdapter(Context context,List<ImageDetails> imageDetails){
this.context = context;
this.ImageDetails=imageDetails;
}
终于在getview中了
TextView textView = (TextView) viewItem.findViewById(R.id.textView1);
textView.setText(ImageDetails.get(position).Caption);