0

画面イメージの変更をスクロールすると、url からイメージをロードして gridview に設定しようとしています。私はアンドロイドの初心者です。私のコードの何が問題なのかわかりません。これが私のコードです..

public class Photos extends Activity {

public static final String TAG_IMAGE_NAME = "image_name";
public static final String TAG_IMAGE_THUMB_NAME = "image_thumb_name";
public static String URL = "http://...../..../..../mainAPI.php";

ArrayList<HashMap<String, String>> photoList;
String responseData = null;
static GridView gridView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photos);

    gridView = (GridView)findViewById(R.id.gridView);
    photoList = new ArrayList<HashMap<String,String>>();
    new AsyncData().execute();

    gridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
         public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), FullImage.class);
                // passing array index
                i.putExtra("ImageName", TAG_IMAGE_NAME);
                startActivity(i);
            }
    });
}

class AsyncData extends AsyncTask<String, Void, Void> {
    ProgressDialog pDialog;

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

    @Override
    protected Void doInBackground(String... args) {
        // TODO Auto-generated method stub
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(URL);
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("rquest","{\"method\":\"photogallery\",\"body\":[{}]}"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();
            responseData = EntityUtils.toString(resEntity);
            try {
                JSONArray data = new JSONArray(responseData);
                for (int i = 0; i < data.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    JSONObject c = data.getJSONObject(i);
                    String photoName = c.getString(TAG_IMAGE_NAME);
                    String imageThumbName = c.getString(TAG_IMAGE_THUMB_NAME);
                    map.put(TAG_IMAGE_NAME, photoName);
                    map.put(TAG_IMAGE_THUMB_NAME, imageThumbName);
                    photoList.add(map);
                }

            } catch (JSONException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        gridView.setAdapter(new PhotosAdapter(Photos.this, R.layout.photo_row, photoList));
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
    {
        finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
  }
}

PhgotosAdapter クラス

public class PhotosAdapter extends ArrayAdapter<HashMap<String, String>>{
Context context;
String uri;
Bitmap bitmap;
ArrayList<HashMap<String, String>> myList;
HashMap<String, String> myData;
int layout;
public PhotosAdapter(Context context, int textViewResourceId, List<HashMap<String, String>> objects) {
    super(context, textViewResourceId, objects);
    // TODO Auto-generated constructor stub
    this.context = context;
    this.myList = (ArrayList<HashMap<String, String>>) objects;
    this.layout = textViewResourceId;
}

@Override
 public View getView(int position, View convertView, ViewGroup parent)  {
    // TODO Auto-generated method stub
    View row = null;
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    row = inflater.inflate(layout, parent, false);
    myData = myList.get(position);
    ImageView image = (ImageView)row.findViewById(R.id.imagePhoto);


    try{
        ImageDownloadTask task = new ImageDownloadTask(image);
        task.execute();
        uri = "" + myData.get(Photos.TAG_IMAGE_THUMB_NAME).replace(" ", "%20");
        image.setImageBitmap(bitmap);
        image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return row;
}
public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

class ImageDownloadTask extends AsyncTask<Void, Integer, Bitmap> {
    private ImageView mView;
    ProgressDialog pDialog;
    ImageDownloadTask(ImageView view){
        mView = view;
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        try {
            bitmap =  getBitmapFromURL(uri);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bitmap;
    }
    @Override
    protected void onPostExecute(Bitmap result) {
       mView.setImageBitmap(result);

    }
  }
}

何が問題なのかわかりません。助けて解決策を教えてください。

4

3 に答える 3

0
ImageLoader imageLoader;
imageLoader=new ImageLoader(context.getApplicationContext());
imageLoader.DisplayImage(urlname, imageViewname);
try it
于 2013-05-03T11:09:01.890 に答える
0

Universal Image Loaderを使用できます

または遅延リスト

于 2013-05-03T11:13:38.913 に答える