gpt4 book ai didi

java - 具有 GridView 的 fragment 可显示 url 路径中的图像

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:48 24 4
gpt4 key购买 nike

我正在尝试制作一个显示图像的 GridView ,因此它显示 GridView 但没有图像,如果单击它, GridView 会弹出链接。我不知道为什么我的图像在 gridview 上丢失了......

这是我的 fragment 类

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");

}
}

最佳答案

您无法在主 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));

关于java - 具有 GridView 的 fragment 可显示 url 路径中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28091965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com