如何使用urls的arraylist在gridview中设置图像?

时间:2015-05-09 09:13:50

标签: android android-gridview

SampleGridViewAdapter.java

proguard.cfg

这是我想要设置package com.alifinteractive.viewtext; import android.content.Context; import android.database.DataSetObserver; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.List; class SampleGridViewAdapter extends ArrayAdapter { private final Context context; List<String> urls; String[] strBook; Bitmap bmp; public SampleGridViewAdapter(Context context, ArrayList<String> urls) { super(context, R.layout.list_image); this.context = context; this.urls = urls; Log.i("DDDDDDDDDD", String.valueOf(urls)); Object[] mStringArray = urls.toArray(); strBook = Arrays.copyOf(mStringArray, mStringArray.length, String[].class); for (int i = 0; i < strBook.length; i++) { Log.i("GGGGGGGGGGG", String.valueOf(strBook[i])); } } @Override public View getView(final int position, View view, ViewGroup parent) { if (view == null) { view = new ImageView(context); } loadBookIcons(view, position); return view; } private void loadBookIcons(final View view, final int position) { new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { try { InputStream in = new URL(strBook[position]).openStream(); bmp = BitmapFactory.decodeStream(in); } catch (Exception e) { Log.i("ERRORRRRRR", e.toString()); } return null; } @Override protected void onPostExecute(Void result) { if (bmp != null) { ((ImageView) view).setImageBitmap(bmp); } } }.execute(); } @Override public int getViewTypeCount() { // TODO Auto-generated method stub return 0; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public boolean isEmpty() { // TODO Auto-generated method stub return false; } @Override public void registerDataSetObserver(DataSetObserver arg0) { // TODO Auto-generated method stub } @Override public void unregisterDataSetObserver(DataSetObserver arg0) { // TODO Auto-generated method stub } @Override public boolean areAllItemsEnabled() { // TODO Auto-generated method stub return false; } @Override public boolean isEnabled(int arg0) { // TODO Auto-generated method stub return false; } @Override public int getCount() { // TODO Auto-generated method stub return 0; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } }

的主页Fragment
GridView

我想在package com.alifinteractive.viewtext; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import org.json.JSONArray; import org.json.JSONObject; import java.util.ArrayList; public class Home_new extends Fragment { //Login login; String json; JSONObject jsonObj; String imageUrl; //String name; ImageView img; ArrayList<String> Urls = new ArrayList<String>(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_home1, container, false); //GridView grid=(GridView)rootView.findViewById(R.id.grid_view); //grid.setAdapter(new SampleGridViewAdapter(getActivity(),Urls)); // username=Login.getUserObject(getActivity(), "username"); // password=Login.getUserObject(getActivity(), "password"); String oldjson = Login.getUserObject(getActivity(), "order_json"); json = oldjson.replaceAll("\\\\", ""); Log.i("QQQQQQQQ", json); try { JSONArray results = new JSONObject(json).getJSONArray("CustomerOrders"); for (int i = 0; i < 10; i++) { JSONObject menuObject = results.getJSONObject(i); Log.i("ooooooooo", String.valueOf(menuObject)); imageUrl = menuObject.getString("image"); Log.i("FFFFFFF", imageUrl); Urls.add(i, imageUrl); Log.i("LLLLLLL", String.valueOf(Urls)); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.i("ZZZZZZZ", String.valueOf(Urls)); // TODO Auto-generated catch block return rootView; } } 上设置adapter但收到错误,请提前感谢

1 个答案:

答案 0 :(得分:0)

@Override 
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout view = new LinearLayout(context);
view.setOrientation(LinearLayout.HORIZONTAL);
view.setPadding(1, 1, 1, 1);
//Icon of Item
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
view.setGravity(Gravity.CENTER_HORIZONTAL);    
   String url = getItem(position);
   Log.i("AAAAAA",String.valueOf(url));
    Picasso.with(context) //
   .load(url) //
   .error(R.drawable.error) //
   .fit() //
   .into(imageView);
  view.addView(imageView);  

  return view;

}