对于循环遍历所有

时间:2015-03-20 09:00:21

标签: java android listview for-loop arraylist

我使用for循环将所有列表项目图像上传到服务器,我也可以上传它们,但我担心的是为什么我没有获得每个列表项的状态。

就像我在列表中有8个项目并且我的上传所有按钮上传所有列表项目图像到服务器成功,但我获得50%列表项目的状态而100%项目上传..可能是什么原因?< / p>

例如:我在列表中有8个项目,从缩略图1到缩略图8,当我点击上传全部按钮时,我成功获取服务器上的所有8个图像,但在我的列表中显示仅上传了4或5条记录为什么?

这是我的代码:

btnUploadAll.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        for (int i=0; i<ImageList.size(); i++)
        {
            new UploadFileAsync().execute(String.valueOf(i));                                                            
        }   
    }
});

以下是完整的代码: -

    public class MainActivity extends Activity {

    static ListView lstView;
    private Handler handler = new Handler();;
    static List<MyData> ImageList;
    String strPath;
    int position;
    File newFile;
    ViewHolder holder;
    View v;
    String fileName;
    ImageAdapter mAdapter;
    Button btnUploadAll;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        btnUploadAll = (Button) findViewById(R.id.btnUploadAll);

        btnUploadAll.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                for (int i=0; i<ImageList.size(); i++)
                {
                    new UploadFileAsync().execute(String.valueOf(i));   
                }       
            }
        });

        /*** Get Images from SDCard ***/
        ImageList = getSD();
        // ListView and imageAdapter
        lstView = (ListView) findViewById(R.id.listView);
        mAdapter = new ImageAdapter(this);
        lstView.setAdapter(mAdapter);

    }

    private List<MyData> getSD() {
        List<MyData> it = new ArrayList<MyData>();
        String root_sd = Environment.getExternalStorageDirectory().toString();
        File f = new File(root_sd + "/mydata");
        File[] files = f.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            Log.d("Count", file.getPath());
            MyData data = new MyData();
            data.setImages(file.getPath());
            data.setStatusEnable(true);
            it.add(data);
        }
        return it;
    }

    static class ViewHolder {
        public ViewHolder(View convertView) {
            // TODO Auto-generated constructor stub
        }

        TextView textName;
        ImageView thumbnail;
        TextView textStatus;
        Button btnUpload;

    }

    public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {

        }

        public int getCount() {
            // TODO Auto-generated method stub
            return ImageList.size();
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // Avoid unneccessary calls to findViewById() on each row, which is
            // expensive!

            holder = null;

            if (convertView == null) {
                convertView = getLayoutInflater().inflate(
                        R.layout.adapter_main, null);
                holder = new ViewHolder(convertView);

                // Create a ViewHolder and store references to the children
                // views
                holder.textName = (TextView) convertView
                        .findViewById(R.id.textName);
                holder.thumbnail = (ImageView) convertView
                        .findViewById(R.id.thumbnail);
                holder.btnUpload = (Button) convertView
                        .findViewById(R.id.btnUpload);
                holder.textStatus = (TextView) convertView
                        .findViewById(R.id.textStatus);

                // The tag can be any Object, this just happens to be the
                // ViewHolder
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.btnUpload.setEnabled(ImageList.get(position)
                    .isStatusEnable());
            holder.textStatus.setText(ImageList.get(position).getMessage());
            strPath = ImageList.get(position).getImages().toString();

            // Get File Name
            fileName = strPath.substring(strPath.lastIndexOf('/') + 1,
                    strPath.length());
            File file = new File(strPath);
            @SuppressWarnings("unused")
            long length = file.length();
            holder.textName.setText(fileName);

            final BitmapFactory.Options options = new BitmapFactory.Options();

            Bitmap bm = BitmapFactory.decodeFile(strPath, options);
            holder.thumbnail.setImageBitmap(bm);                       

            // btnUpload
            holder.btnUpload.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Upload
                    startUpload(position);
                }
            });

            return convertView;

        }
    }

    // Upload

    public void startUpload(final int position) {

        Runnable runnable = new Runnable() {

            public void run() {

                handler.post(new Runnable() {

                    public void run() {
                        v = lstView.getChildAt(position
                                - lstView.getFirstVisiblePosition());
                        holder = (ViewHolder) v.getTag();
                        synchronized (this) {
                            ImageList.get(position).setStatusEnable(false);
                            mAdapter.notifyDataSetChanged();
                        }

                        new UploadFileAsync().execute(String.valueOf(position));
                    }
                });
            }
        };
        new Thread(runnable).start();
    }

    // Async Upload
    public class UploadFileAsync extends AsyncTask<String, Void, Void> {

        private ProgressDialog pDialog;
        String resServer;

        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(String... params) {
            position = Integer.parseInt(params[0]);
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;
            int resCode = 0;
            String resMessage = "";

            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";

            // File Path
            String strSDPath = ImageList.get(position).getImages().toString();

            // Upload to PHP Script
            String strUrlServer = "http://10.0.2.2/uploadFile.php";

            try {
                /** Check file on SD Card ***/
                File file = new File(strSDPath);
                if (!file.exists()) {
                    resServer = "{\"StatusID\":\"0\",\"Message\":\"Please check path on SD Card\"}";
                    return null;
                }

                FileInputStream fileInputStream = new FileInputStream(new File(
                        strSDPath));

                URL url = new URL(strUrlServer);
                HttpURLConnection conn = (HttpURLConnection) url
                        .openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("POST");

                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("Content-Type",
                        "multipart/form-data;boundary=" + boundary);

                DataOutputStream outputStream = new DataOutputStream(
                        conn.getOutputStream());
                outputStream.writeBytes(twoHyphens + boundary + lineEnd);
                outputStream
                        .writeBytes("Content-Disposition: form-data; name=\"filUpload\";filename=\""
                                + strSDPath + "\"" + lineEnd);
                outputStream.writeBytes(lineEnd);

                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];

                // Read file
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                while (bytesRead > 0) {
                    outputStream.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }

                outputStream.writeBytes(lineEnd);
                outputStream.writeBytes(twoHyphens + boundary + twoHyphens
                        + lineEnd);

                // Response Code and Message
                resCode = conn.getResponseCode();
                if (resCode == HttpURLConnection.HTTP_OK) {
                    InputStream is = conn.getInputStream();
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();

                    int read = 0;
                    while ((read = is.read()) != -1) {
                        bos.write(read);
                    }

                    byte[] result = bos.toByteArray();
                    bos.close();

                    resMessage = new String(result);

                }

                Log.d("resCode=", Integer.toString(resCode));
                Log.d("resMessage=", resMessage.toString());

                fileInputStream.close();
                outputStream.flush();
                outputStream.close();

                resServer = resMessage.toString();

            } catch (Exception ex) {
                ex.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(Void unused) {
            statusWhenFinish(position, resServer);
         // dismiss the dialog once product deleted
            pDialog.dismiss();
        }

    }

    // When Upload Finish
    @SuppressWarnings("unused")
    protected void statusWhenFinish(int position, String resServer) {

        /*** Default Value ***/
        String strStatusID = "";
        String strError = "";

        try {

            JSONObject c = new JSONObject(resServer);
            strStatusID = c.getString("StatusID");
            strError = c.getString("Message");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // // prepare Status
        if (strStatusID.equals("0")) {         
                    ImageList.get(position).setMessage("Failed");
                    ImageList.get(position).setStatusEnable(true);  
                    mAdapter.notifyDataSetChanged();

        } else if (strStatusID.equals("1")) {
                    ImageList.get(position).setMessage("Already Exists");
                    ImageList.get(position).setStatusEnable(false); 
                    mAdapter.notifyDataSetChanged();

        } else if(strStatusID.equals("2")) {
                    ImageList.get(position).setMessage("Uploaded");
                    ImageList.get(position).setStatusEnable(false); 
                    mAdapter.notifyDataSetChanged();
        } else {

        }       

    }

    /**
     * Introduce a class with below attributes to hold a state of each row in
     * single element
     * 
     */
    public class MyData {
        /* Image url or path of image in single row */
        private String images;

        /* anme of image in single row */
        private String name;

        /* status ID of image in single row */
        private String statusID;

        /* message of image in single row */
        private String message;

        private boolean statusEnable;

        public boolean isStatusEnable() {
            return statusEnable;
        }

        public void setStatusEnable(boolean statusEnable) {
            this.statusEnable = statusEnable;
        }

        // Generate getters and setter
        public String getImages() {
            return images;
        }

        public void setImages(String images) {
            this.images = images;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getStatusID() {
            return statusID;
        }

        public void setStatusID(String statusID) {
            this.statusID = statusID;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

    }

}

3 个答案:

答案 0 :(得分:1)

这可能与您的AsyncTask HTTP请求异步这一事实有关,而for循环是同步

您所做的假设是UploadFileAsync的每个实例都是串行执行的。实际发生的是每个实例的doInBackground()方法同时执行(取决于CPU具有多少核心以及网络库是否支持并发线程池)。因此,在上传所有文件时,只有先完成的文件才会显示完成状态。

要解决此问题,请发布您的AsyncTaskListView代码。

答案 1 :(得分:1)

哦,我懒得写优化代码。这里的工作:

public class MainActivity extends Activity {

    static ListView lstView;
    private Handler handler = new Handler();;
    static List<MyData> ImageList;
    String strPath;
    int position;
    File newFile;
    ViewHolder holder;
    View v;
    String fileName;
    ImageAdapter mAdapter;
    Button btnUploadAll;
    int i=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        btnUploadAll = (Button) findViewById(R.id.btnUploadAll);

        btnUploadAll.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(ImageList.size()!=0)
                {
                    new UploadFileAsync().execute(String.valueOf(i));   
                }       
            }
        });

        /*** Get Images from SDCard ***/
        ImageList = getSD();
        // ListView and imageAdapter
        lstView = (ListView) findViewById(R.id.listView);
        mAdapter = new ImageAdapter(this);
        lstView.setAdapter(mAdapter);

    }

    private List<MyData> getSD() {
        List<MyData> it = new ArrayList<MyData>();
        String root_sd = Environment.getExternalStorageDirectory().toString();
        File f = new File(root_sd + "/mydata");
        File[] files = f.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            Log.d("Count", file.getPath());
            MyData data = new MyData();
            data.setImages(file.getPath());
            data.setStatusEnable(true);
            it.add(data);
        }
        return it;
    }

    static class ViewHolder {
        public ViewHolder(View convertView) {
            // TODO Auto-generated constructor stub
        }

        TextView textName;
        ImageView thumbnail;
        TextView textStatus;
        Button btnUpload;

    }

    public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {

        }

        public int getCount() {
            // TODO Auto-generated method stub
            return ImageList.size();
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // Avoid unneccessary calls to findViewById() on each row, which is
            // expensive!

            holder = null;

            if (convertView == null) {
                convertView = getLayoutInflater().inflate(
                        R.layout.adapter_main, null);
                holder = new ViewHolder(convertView);

                // Create a ViewHolder and store references to the children
                // views
                holder.textName = (TextView) convertView
                        .findViewById(R.id.textName);
                holder.thumbnail = (ImageView) convertView
                        .findViewById(R.id.thumbnail);
                holder.btnUpload = (Button) convertView
                        .findViewById(R.id.btnUpload);
                holder.textStatus = (TextView) convertView
                        .findViewById(R.id.textStatus);

                // The tag can be any Object, this just happens to be the
                // ViewHolder
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.btnUpload.setEnabled(ImageList.get(position)
                    .isStatusEnable());
            holder.textStatus.setText(ImageList.get(position).getMessage());
            strPath = ImageList.get(position).getImages().toString();

            // Get File Name
            fileName = strPath.substring(strPath.lastIndexOf('/') + 1,
                    strPath.length());
            File file = new File(strPath);
            @SuppressWarnings("unused")
            long length = file.length();
            holder.textName.setText(fileName);

            final BitmapFactory.Options options = new BitmapFactory.Options();

            Bitmap bm = BitmapFactory.decodeFile(strPath, options);
            holder.thumbnail.setImageBitmap(bm);                       

            // btnUpload
            holder.btnUpload.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Upload
                    startUpload(position);
                }
            });

            return convertView;

        }
    }

    // Upload

    public void startUpload(final int position) {

        Runnable runnable = new Runnable() {

            public void run() {

                handler.post(new Runnable() {

                    public void run() {
                        v = lstView.getChildAt(position
                                - lstView.getFirstVisiblePosition());
                        holder = (ViewHolder) v.getTag();
                        synchronized (this) {
                            ImageList.get(position).setStatusEnable(false);
                            mAdapter.notifyDataSetChanged();
                        }

                        new UploadFileAsync().execute(String.valueOf(position));
                    }
                });
            }
        };
        new Thread(runnable).start();
    }

    // Async Upload
    public class UploadFileAsync extends AsyncTask<String, Void, Void> {

        private ProgressDialog pDialog;
        String resServer;

        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(String... params) {
            position = Integer.parseInt(params[0]);
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;
            int resCode = 0;
            String resMessage = "";

            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";

            // File Path
            String strSDPath = ImageList.get(position).getImages().toString();

            // Upload to PHP Script
            String strUrlServer = "http://10.0.2.2/uploadFile.php";

            try {
                /** Check file on SD Card ***/
                File file = new File(strSDPath);
                if (!file.exists()) {
                    resServer = "{\"StatusID\":\"0\",\"Message\":\"Please check path on SD Card\"}";
                    return null;
                }

                FileInputStream fileInputStream = new FileInputStream(new File(
                        strSDPath));

                URL url = new URL(strUrlServer);
                HttpURLConnection conn = (HttpURLConnection) url
                        .openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("POST");

                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("Content-Type",
                        "multipart/form-data;boundary=" + boundary);

                DataOutputStream outputStream = new DataOutputStream(
                        conn.getOutputStream());
                outputStream.writeBytes(twoHyphens + boundary + lineEnd);
                outputStream
                        .writeBytes("Content-Disposition: form-data; name=\"filUpload\";filename=\""
                                + strSDPath + "\"" + lineEnd);
                outputStream.writeBytes(lineEnd);

                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];

                // Read file
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                while (bytesRead > 0) {
                    outputStream.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }

                outputStream.writeBytes(lineEnd);
                outputStream.writeBytes(twoHyphens + boundary + twoHyphens
                        + lineEnd);

                // Response Code and Message
                resCode = conn.getResponseCode();
                if (resCode == HttpURLConnection.HTTP_OK) {
                    InputStream is = conn.getInputStream();
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();

                    int read = 0;
                    while ((read = is.read()) != -1) {
                        bos.write(read);
                    }

                    byte[] result = bos.toByteArray();
                    bos.close();

                    resMessage = new String(result);

                }

                Log.d("resCode=", Integer.toString(resCode));
                Log.d("resMessage=", resMessage.toString());

                fileInputStream.close();
                outputStream.flush();
                outputStream.close();

                resServer = resMessage.toString();

            } catch (Exception ex) {
                ex.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(Void unused) {
            if(i<imageList.size()){
              i++;
              new UploadFileAsync().execute(String.valueOf(i));
            }
            statusWhenFinish(position, resServer);
         // dismiss the dialog once product deleted
            pDialog.dismiss();
        }

    }

    // When Upload Finish
    @SuppressWarnings("unused")
    protected void statusWhenFinish(int position, String resServer) {

        /*** Default Value ***/
        String strStatusID = "";
        String strError = "";

        try {

            JSONObject c = new JSONObject(resServer);
            strStatusID = c.getString("StatusID");
            strError = c.getString("Message");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // // prepare Status
        if (strStatusID.equals("0")) {         
                    ImageList.get(position).setMessage("Failed");
                    ImageList.get(position).setStatusEnable(true);  
                    mAdapter.notifyDataSetChanged();

        } else if (strStatusID.equals("1")) {
                    ImageList.get(position).setMessage("Already Exists");
                    ImageList.get(position).setStatusEnable(false); 
                    mAdapter.notifyDataSetChanged();

        } else if(strStatusID.equals("2")) {
                    ImageList.get(position).setMessage("Uploaded");
                    ImageList.get(position).setStatusEnable(false); 
                    mAdapter.notifyDataSetChanged();
        } else {

        }       

    }

    /**
     * Introduce a class with below attributes to hold a state of each row in
     * single element
     * 
     */
    public class MyData {
        /* Image url or path of image in single row */
        private String images;

        /* anme of image in single row */
        private String name;

        /* status ID of image in single row */
        private String statusID;

        /* message of image in single row */
        private String message;

        private boolean statusEnable;

        public boolean isStatusEnable() {
            return statusEnable;
        }

        public void setStatusEnable(boolean statusEnable) {
            this.statusEnable = statusEnable;
        }

        // Generate getters and setter
        public String getImages() {
            return images;
        }

        public void setImages(String images) {
            this.images = images;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getStatusID() {
            return statusID;
        }

        public void setStatusID(String statusID) {
            this.statusID = statusID;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

    }

}

答案 2 :(得分:0)

我的建议是,不是在AsynTask循环中循环for,而是循环for循环,以便在doInBackground() <{1}}的{​​{1}}方法中上传>