我正在构建一个由GridView组成的UI,其中包含从API中提取的图像,我正在使用ImageListAdapter和MovieFragment,当我启动应用程序时,我只是得到一个空屏幕。 我已经尝试使用预先下载的图像列表,它的工作原理。 从API获取数据是在AsyncTask上,我认为问题是时间问题,即gridview.setAdapter()过早调用的事实。 这是代码:
public static void main(String[] args) {
HashMap<Integer, HashSet<Integer>>
map1 = new HashMap<Integer, HashSet<Integer>>(),
map2 = new HashMap<Integer, HashSet<Integer>>();
HashSet<Integer> set1=new HashSet<Integer>();
set1.add(1);
set1.add(2);
map1.put(1,set1);
map2.put(1,set1);
System.out.println(map1.values()==map2.values());
System.out.println(map1.values()+" "+map2.values());
}
公共类MovieFragment扩展Fragment {
public class MainActivity extends AppCompatActivity {
private final String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager()
.beginTransaction()
.add(R.id.container, new MovieFragment())
.commit();
}
}}
}
答案 0 :(得分:0)
我终于解决了它,我使用了BaseAdapter的扩展而不是ArrayAdapter:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private String[] moviePath;
public ImageAdapter(Context c, String[] path) {
mContext = c;
this.moviePath = path;
}
public int getCount() {
return moviePath.length;
}
//Needed due to methods being abstract
public Object getItem(int position) {return null;}
public long getItemId(int position) {return 0;}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = (ImageView) convertView;
if (imageView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(500, 500));
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
//imageView.setImageResource(mThumbIds[position]);
Picasso.with(getActivity())
.load(moviePath[position])
.fit()
.into(imageView);
return imageView;
}
}
我还在onPostExecute()方法中使用了setAdapter()。
谢谢大家的帮助= D