从gridView在imageView中显示图像(从数据库中获取所有图像)

时间:2016-11-23 11:02:16

标签: android gridview

我想在ImageView中设置Image。 gridView中的这个图像。gridview中的所有图像都是在运行时从MySql DataBase中获取的。这是我的代码。

GalleryFragment.java

public class GalleryFragment extends Fragment {
GridView grid;
private List<User> userList;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.gallery, null);

    grid=(GridView)rootView.findViewById(R.id.grid);

    imagesFromDb();


    return rootView;
}
private void imagesFromDb() {
    //Code Which Is Get all images from database
    setData();
    }
private void setData() {
        AdapterGallery adapter = new AdapterGallery(getActivity(),userList);
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Intent i = new Intent(getActivity(), Galleryfullimage.class);
            i.putExtra("id", position);
            startActivity(i);

        }
    });

}
}

AdapterGallery.java

public class AdapterGallery extends BaseAdapter {

    private Context mContext;
    private List<User> userList;

    public AdapterGallery(Context context, List<User> userList){
        mContext=context;
        this.userList=userList;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return userList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        //View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.gallerysecond, null);

        TextView en = (TextView) convertView.findViewById(R.id.grid_text);
        ImageView imageView= (ImageView) convertView.findViewById(R.id.grid_image);

        User contacts = userList.get(position);
        en.setText(contacts.gall_name);
        Picasso.with(mContext).load(ServerHelper.galleryimgpath+contacts.gall_img)
                .placeholder(mContext.getResources().getDrawable(R.drawable.ic_launcher))
                .error(mContext.getResources().getDrawable(R.drawable.ramanisir)).into(imageView);

        Log.e("Path", ServerHelper.galleryimgpath + contacts.gall_img);

        return convertView;

    }
}

Galleryfullimage.java

    public class Galleryfullimage extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.galleryfullimage);
   }

}

我在Showfullimage.java?i中为显示完整图像做了什么有布局文件galleryfullimage.xml。在那个文件中有一个ImageView。

请指导我。

提前谢谢。

2 个答案:

答案 0 :(得分:1)

使用以下内容更改GalleryFragment中的OnItemClick:

 @Override
    public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
           Intent i = new Intent(getActivity(), Galleryfullimage.class);
           i.putExtra("User_Image", userList.get(position).gall_img);
           startActivity(i);          
    }

Galleryfullimage活动中的下一个用法:

public class Galleryfullimage extends Activity {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.galleryfullimage);
         String imageName = getIntent().getStringExtra("User_Image");
         //Now show this image to imageview using same picasso code
         Picasso.with(this).load(ServerHelper.galleryimgpath+imageName )
         .placeholder(getResources().getDrawable(R.drawable.ic_launcher))
         .error(getResources().getDrawable(R.drawable.ramanisir))
         .into(imageView/*Full image view*/);
       }
}

答案 1 :(得分:1)

您似乎只将位置传递给GallaryFullImage活动。 因此,在新活动中,您将不具有特定网格模型的对象引用。 因此,不要仅传递位置,而是将Image的完整路径作为意图参数传递。

 grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                  User contacts = ((User) grid.getAdapter()).getItem(position);
                  Intent i = new Intent(getActivity(),Galleryfullimage.class);
                //i.putExtra("id", position);
                  i.putExtra("Path", ServerHelper.galleryimgpath + contacts.gall_img);
                  startActivity(i);
        }
    });   

然后在Gallaryfullimage活动中获取此路径并进行设置。