将字符串传递给背景方法

时间:2014-06-09 16:00:08

标签: android callback parse-platform

问题摘要:我正在使用后台方法下载一堆图像,然后保存图像以便以后访问它们。我的问题是我需要一种方法将每个保存的图像与我在调用background方法之前已经确定的字符串相关联。使用getter和setter不会起作用,因为字符串可能会在调用每个回调方法之前发生变化。是继承getDataInBackgroundMethod的唯一方法吗?希望它们是一种更简单的方法:)除了传入变量外,实际代码工作正常。

  //.... Continued from previous code....

    //Iterate through the items of an array that has previously been returned

    Word wordObject = (Word) itr.next();

    //Get the title to store the picture under in local database.

    String stringIwouldLikeToPassIn =  wordObject.getTitle();

    //Parse file is the object responsible for holding the pictures

    ParseFile file = wordObject.getParseFile("Pics");

    file.getDataInBackground(new GetDataCallback() {

    //Get the actual data for the pictures and then store them in 
    //local memory in byte form. 

    public void done(byte[] data, ParseException e) {
    if (e == null) {

    try {
    //*** TO DO: I want to have the tempString be passed in so that    
    //*** I can save the file with the name of the object that was originally called

    ---> String stringFromCallBack = stringIwouldLikeToPassIn <---------
    //This doesn't work as you can't pass an extra paramater into the call back...

    FileOutputStream fos = context.openFileOutput(stringFromBeforeCallBack, Context.MODE_PRIVATE);
                                    fos.write(data);
                                    fos.close();

                                } catch (FileNotFoundException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                                } catch (IOException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                                }
                            } else {
                              // something went wrong
                            }
                          }
                        });

                    //Pin each word in the background so we can use the objects
                    wordObject.pinInBackground(null);

                    //Add each question round to the local relations
                    relation.add(wordObject);

                    //Write each word object to title
                    Log.d(DEBUG, wordObject.getTitle());

                  }

                  //After adding the relative array of question objects then we save
                  user.pinInBackground(null);
                  user.saveEventually();



            }



        });

//      }


    }

1 个答案:

答案 0 :(得分:0)

GetDataCallback添加构造函数,并在创建GetDataCallback实例时将字符串传递给构造函数。为此,您需要将stringIwouldLikeToPassIn声明为final