图像未在网格视图中显示

时间:2016-04-28 14:09:58

标签: android gridview activeandroid

我正在尝试使用Active Android填充存储在数据库中的图像的网格视图。添加帖子时正在填充数据库,但是,图像未显示在网格视图中。

我不认为网格视图有任何问题,因为在调试时,我正在显示布局边界并显示网格视图项(没有图像)。

这是我的代码。

发布课程

@Table(name = "Posts")
public class Post extends Model {
    @Column(name = "Title")
    public String title;
    @Column(name = "Content")
    public String description;
    @Column(name = "Section")
    public String section;
    @Column(name = "Image")
    public Bitmap image;

    public Post() {
        super();
    }
}

图像适配器类

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private List<Post> posts;
    public ImageAdapter(Context c, List<Post> posts)
    {
        mContext = c;
        this.posts=posts;
    }

    //returns number of items in ArrayList
    public int getCount() {
        return posts.size();
    }

    //returns a reference to the current image
    public Object getItem(int position)
    {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    //creating a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageBitmap(posts.get(position).image);
        return imageView;
    }
}

Fragment Class - 网格视图的代码位于onCreateView方法

public class Fragment extends Fragment {

    public static final String ARG_PAGE = "ARG_PAGE";
    private int mPage;
    List<Post> toShow;

    //the inflation logic is defined for the fragment tab content
    public static Fragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        Fragment fragment = new Fragment ();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
        Toast.makeText(getContext(), "Science", Toast.LENGTH_SHORT).show();
    }

    public static List<Post> getAll() {
        return new Select()
                .from(Post.class)
                .execute();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_fragment, container, false);
        toShow=getAll();
        GridView gridView = (GridView)view.findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(getContext(),toShow));
        return view;
    }
}

这就是我填充数据库的方法

 public void submitAction(View view)
    {
        /*This method creates a new post and populates it with the data added by the user. The data is then stored in the database
        * using the Active Android Library.*/
        Post p = new Post();
        EditText title = (EditText) findViewById(R.id.post_title_input);
        String tit = title.getText().toString();
        EditText description = (EditText)findViewById((R.id.editText));
        String desc = description.getText().toString();
        Bitmap img = yourSelectedImage;
        p.title=tit;
        p.description=desc;
        p.section="science";
        p.image=img;

        if(tit.matches("")) //not recognised
        {
            Toast.makeText(getApplicationContext(), "Title should not be empty", Toast.LENGTH_LONG).show();
        }else if(desc.matches("")) //not recognised
        {
            Toast.makeText(getApplicationContext(), "Description should not be empty", Toast.LENGTH_LONG).show();
        }else if(img==null)
        {
            Toast.makeText(getApplicationContext(), "Image should be selected", Toast.LENGTH_LONG).show();
        }else
        {
            p.save();// storing entry in database
            Toast.makeText(getApplicationContext(), "Image Saved", Toast.LENGTH_LONG).show();
            Intent i = new Intent(this, News.class);
            startActivity(i);
        }
    }

非常感谢任何帮助。感谢

0 个答案:

没有答案
相关问题