如何创建一个从json提供图像的图库视图?

时间:2015-12-12 12:48:27

标签: android json

如何使用厨房视图创建Android应用程序但是从json获取图像?

我认为它就像一个列表视图

和图库必须是水平的

像它一样创建

ArrayList<String> listPhotos = new ArrayList<String>();
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_nek);
    Gallery gallery = (Gallery) findViewById(R.id.photos);
            gallery.setAdapter(new ImageAdapter(this));
    startWebService();
    }

    public class ImageAdapter extends BaseAdapter {
            private Context context;
            private int itemBackground;
            public ImageAdapter(Context c)
            {
                context = c;
            }
            // returns the number of images
            public int getCount() {
                return listPhotos;
            }
            // returns the ID of an item
            public Object getItem(int position) {
                return position;
            }
            // returns the ID of an item
            public long getItemId(int position) {
                return position;
            }
            // returns an ImageView view
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView imageView = new ImageView(context);
                imageView.setImageResource(listPhotos[position]);
                imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
                imageView.setBackgroundResource(itemBackground);
                return imageView;
            }
        }

        public class JsonReadTastPhotos extends AsyncTask<String, Void, String> {
            public JsonReadTastPhotos() {
                super();
            }

            @Override
            protected void onPreExecute(){
                super.onPreExecute();
                pDialog = new ProgressDialog(NekActivity.this);
                pDialog.setTitle("Please wait");
                pDialog.setMessage("Loading...");
                pDialog.setIndeterminate(true);
                pDialog.setCancelable(false);
                pDialog.show();
            }

            @Override
            protected String doInBackground(String... params) {
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                nameValuePairs.add(new BasicNameValuePair("id",id));
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(params[0]);
                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpClient.execute(httpPost);
                    jsonResult = inputStreamToString(response.getEntity().getContent()).toString();

                } catch (Exception e){
                    NekActivity.this.finish();
                }
                return null;
            }

            public StringBuilder inputStreamToString(InputStream is) {
                String line = "";
                StringBuilder answer = new StringBuilder();
                BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                try {
                    while ((line = rd.readLine()) != null) {
                        answer.append(line);
                    }
                } catch (Exception ex) {
                    NekActivity.this.finish();
                }

                return answer;

            }

            @Override
            protected void onPostExecute(String s){

                ListDrawer();
                pDialog.dismiss();
            }
        }

        public void startWebServicePhotos(){
            JsonReadTastPhotos task = new JsonReadTastPhotos();
            task.execute(new String[]{urlPhotos});
        }

        private void ListDrawerPhotos() {
            try {
                JSONObject jsonResponse = new JSONObject(jsonResult);
                JSONArray jsonMain = jsonResponse.optJSONArray("nek");
                for (int i = 0; i<jsonMain.length(); i++){
                    JSONObject jsonChild = jsonMain.getJSONObject(i);

                    String image = jsonChild.optString("name");

                    listPhotos.add(image);
                }

            }catch (Exception ee) {
                NekActivity.this.finish();
            }
        }

但我对listphotos [位置]

有疑问

0 个答案:

没有答案