我想向ArrayLists
下面代码中声明的三个元素添加元素,但我似乎遇到了一些与变量范围相关的问题。免责声明:我对 Java 很陌生,可能只是我在做一些非常愚蠢的事情。另请注意,我正在使用Parse Android API。(我在代码中添加了一些注释以更好地突出我正在尝试解决的问题)。谢谢!
public class MatchesActivity extends Activity implements OnItemClickListener {
ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> descriptions = new ArrayList<String>();
ArrayList<Bitmap> images = new ArrayList<Bitmap>();
String school;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.matches_layout);
ParseQuery query = new ParseQuery("Profile");
query.whereEqualTo("userName", ParseUser.getCurrentUser().getUsername().toString());
query.getFirstInBackground(new GetCallback() {
public void done(ParseObject obj, ParseException e) {
if (e == null) {
school = obj.getString("school");
ParseQuery query2 = new ParseQuery("Profile");
query2.whereEqualTo("school", school);
query2.findInBackground(new FindCallback() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
// scoreList.size() == 3 here
for (int i = 0; i < scoreList.size(); i++){
titles.add(scoreList.get(i).getString("fullName"));
descriptions.add(scoreList.get(i).getString("sentence"));
ParseFile profileImg = (ParseFile) scoreList.get(i).get("pic");
try {
profileImg.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
Bitmap bMap = BitmapFactory.decodeByteArray(data, 0,data.length);
images.add(bMap);
} else {
Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(),Toast.LENGTH_SHORT).show();
}
// AT THIS POINT THE ARRAYLIST "IMAGES" IS BEING ASSIGNED VALUES
}
});
} catch (NullPointerException npe) {
images.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_prof));
}
}
// HERE, THE SIZE OF TITLES AND DESCRIPTION IS 3, HOWEVER, IMAGES HAS NO ELEMENTS (WHEN I EXPECTED IT TO HAVE 3)
} else {
Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
// ALL LISTS ARE EMPTY AT THIS POINT (BUT I WOULD LIKE TO USE THEM HERE)
}
问题已解决:
由于它是由Yogendra Singh和twaddington 提出的,该getDataInBackground
方法作为辅助线程运行,在我到达代码中需要在那里检索信息的特定位置之前没有机会完成。由于我的最终任务是使用从我的 Parse 数据库中检索到的信息动态填充 listView,因此我只是决定按照此处的提示并在getDataInBackground
. 那行得通!感谢大家的帮助。