0

到目前为止,我有

私有 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 一起使用。

私有字节[] imag = Base64.decode(base.getBytes(), Base64.DEFAULT);

字符串基数 = "/9j/4AAQSkZJRgABAQAAAQABAAD...";

4

1 回答 1

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);

}

}

于 2017-11-09T18:57:21.930 回答