所以我从 Parse 中检索图像并在 ScrollView LinearLayout 中动态显示它们,但我似乎无法添加 clickListener 或将它们添加到视图中。这是我所拥有的:
MyTaskParams params = new MyTaskParams(drinkList.get(j).getString("Name"), data, lPos, drinkList.get(j).getObjectId(), j);
MyTask myTask = new MyTask();
myTask.execute(params);
然后在我传递所有数据并尝试将 ImageButton 添加到视图中。
private static class MyTaskParams {
String name;
byte[] data;
int lPos;
String id;
int j;
public MyTaskParams(String name, byte[] data, int lPos, String id, int j) {
this.data = data;
this.name = name;
this.lPos = lPos;
this.id = id;
this.j = j;
}
}
private class MyTask extends AsyncTask<MyTaskParams, Void, MyTaskParams> {
@Override
protected MyTaskParams doInBackground(MyTaskParams... params) {
String name = params[0].name;
byte[] data = params[0].data;
int lPos = params[0].lPos;
String id = params[0].id;
int j = params[0].j;
Log.d("DOINBACKGROUND", data.toString());
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
btn = new ImageButton(getActivity());
btn.setId(j);
btn.setTag(name);
btn.setImageBitmap(bmp);
btn.setBackgroundDrawable(null);
btn.setMaxHeight(100);
btn.setMaxWidth(100);
btn.setOnClickListener(new DrinkButtonListener(getActivity(), lPos, id));
lLayout[lPos].addView(btn);
return null;
}
}
为什么我不能创建 ClickListener 或将按钮添加到视图中?我得到错误:
“只有创建视图层次结构的原始线程才能触及它的视图”