我正在尝试制作一个显示图像的网格视图,因此它显示了gridview但没有图像,如果点击它,则gridview会弹出链接。我不知道为什么我的图像在gridview上丢失了......
继承我的片段类
public class FragmentD extends Fragment {
public FragmentD(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_d, container, false);
try {
EditText main_searchField = (EditText) rootView.findViewById(R.id.editText_search_d);
String main_search = main_searchField.getText().toString();
Spinner main_categoryField = (Spinner) rootView.findViewById(R.id.spinner_d);
String category = main_categoryField.getSelectedItem().toString();
GridView main_gridviewField = (GridView) rootView.findViewById(R.id.gridView_main);
main_gridviewField.setAdapter(new MyGridViewAdapter(getActivity()));
main_gridviewField.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
Toast.makeText(getActivity(), MyGridViewConfig.getResim_list().
get(position), Toast.LENGTH_SHORT).show();
}
});
if(main_gridviewField.isActivated()){
ImageView sad_face = (ImageView)getActivity().findViewById(R.id.imageView_main_sadface);
sad_face.setVisibility(View.INVISIBLE);
TextView nosuchdata = (TextView)getActivity().findViewById(R.id.textView_main_nodatafound);
nosuchdata.setVisibility(View.INVISIBLE);
}
} catch (Exception e){
Toast.makeText(getActivity(), "EPIC FAIL", Toast.LENGTH_LONG).show();
}
//new search_mainActivity(getActivity()).execute(main_search, category);
return rootView;
}
}
继承人我的适配器
public class MyGridViewAdapter extends BaseAdapter implements ListAdapter {
private Context context;
public MyGridViewAdapter(Context context) {
super();
this.context = context;
MyGridViewConfig.addImageUrls();
}
public int getCount() {
return MyGridViewConfig.getResim_list().size();
}
public Object getItem(int position) {
return MyGridViewConfig.getResim_list().get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView==null)
{
imageView=new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(100,100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5,5,5,5);
}else{
imageView=(ImageView)convertView;
}
imageView.setImageDrawable(LoadImageFromURL(MyGridViewConfig.
getResim_list().get(position)));
return imageView;
}
private Drawable LoadImageFromURL(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src");
return d;
}catch (Exception e) {
System.out.println(e);
return null;
}
}
}
继承我的配置java
public class MyGridViewConfig {
private static ArrayList<String> resim_list=new ArrayList<String>();
public static ArrayList<String> getResim_list() {
return resim_list;
}
public static void setResim_list(ArrayList<String> resim_list) {
MyGridViewConfig.resim_list = resim_list;
}
public static void addImageUrls(){
// Here you have to specify your image url path
resim_list.add("http://fc08.deviantart.net/fs71/f/2010/319/d/6/neko_chibi__s_by_alykun17-d32y2v5.jpg");
resim_list.add("http://fc08.deviantart.net/fs71/f/2010/319/d/6/neko_chibi__s_by_alykun17-d32y2v5.jpg");
resim_list.add("http://fc08.deviantart.net/fs71/f/2010/319/d/6/neko_chibi__s_by_alykun17-d32y2v5.jpg");
resim_list.add("http://fc08.deviantart.net/fs71/f/2010/319/d/6/neko_chibi__s_by_alykun17-d32y2v5.jpg");
resim_list.add("http://fc08.deviantart.net/fs71/f/2010/319/d/6/neko_chibi__s_by_alykun17-d32y2v5.jpg");
resim_list.add("http://fc08.deviantart.net/fs71/f/2010/319/d/6/neko_chibi__s_by_alykun17-d32y2v5.jpg");
}
}
答案 0 :(得分:0)
你设置了android.permission.INTERNET权限吗?
答案 1 :(得分:0)
您无法在主UI线程上下载图像,因此请在AsyncTask中执行此操作。创建这个类:
class LoadImageTask extends AsyncTask<String, Void, Drawable > {
public LoadImageTask(ImageView iv)
{
imageView = iv;
}
ImageView imageView;
protected Drawable doInBackground(String... urls) {
try {
URLConnection connection = new URL(urls[0]).openConnection();
connection.connect();
InputStream is = (InputStream) connection.getContent();
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (Exception e) {
// e.printStackTrace();
return null;
}
}
protected void onPostExecute(Drawable drawable) {
if(drawable != null)
imageView.setImageDrawable(drawable);
}
}
然后更改你的getView()函数以使用AsyncTask而不是LoadImageFromURL:
// imageView.setImageDrawable(LoadImageFromURL(MyGridViewConfig.
// getResim_list().get(position)));
new LoadImageTask(imageView).execute(MyGridViewConfig.getResim_list().get(position));