到目前为止,我已经
了private int [] imageAsBytes = {R.drawable.imagethree};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomSwipeAdapter(Context ctx) throws JSONException {this.ctx = ctx;}
@Override
public int getCount() {return imageAsBytes.length;}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == (LinearLayout)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = layoutInflater.inflate(R.layout.swipe_layout, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
TextView textView = (TextView) itemView.findViewById(R.id.textView);
imageView.setImageResource(imageAsBytes[position]);
textView.setText("Image: " + position);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.invalidate();
}
{R.drawable.imagethree}一切正常,但我需要在int []部分使用byte []或String。
private byte [] imag = Base64.decode(base.getBytes(),Base64.DEFAULT);
String base =“/ 9j / 4AAQSkZJRgABAQAAAQABAAD ......”;
答案 0 :(得分:0)
公共类ViewPagerAdapter扩展了PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
JSONArray slikeArray;
JSONObject oglasiObject;
JSONObject slikeObject;
String ogl2;
String ogl;
byte[][] b = new byte[][]{};
ArrayList<byte[]> data = new ArrayList<byte[]>();
public String getJSONUrl(String url) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
public ViewPagerAdapter(Context context, String mParam3) throws JSONException {
this.context = context;
this.ogl2 = mParam3;
String urlPosiljaoc = "http://192.168.0.5/Oglasi/oglasi/svi";
final JSONArray oglasiArray = new JSONArray(getJSONUrl(urlPosiljaoc));
try {
for (int m = 0; m < oglasiArray.length(); m++) {
oglasiObject = oglasiArray.getJSONObject(m);
ogl = oglasiObject.getString("OglasID");
if (ogl.equals(ogl2)) {
slikeArray = oglasiObject.getJSONArray("SlikeInfo");
byte[] imageAsBytes = new byte[]{};
String [] strArray = new String[slikeArray.length()];
for (int u = 0; u < slikeArray.length(); u++) {
slikeObject = slikeArray.getJSONObject(u);
String base = slikeObject.getString("Data");
strArray[u] = base;
data.add(Base64.decode(strArray[u].getBytes(), Base64.DEFAULT));
}
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public int getCount() {
return data.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.custom_layout, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
Glide.with(context).load(data.get(position)).asBitmap().into(imageView);
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}