需要帮忙绘制从一类到GridView控件 [英] Need Help getting drawable from one class into gridView
问题描述
我有一个复选框,选中那个时候,原来的图标变成bitmapdrawable。然后我要位图绘制,以便能够在GridView展现出来。我似乎无法得到这个工作。
这里是我的复选框:
I have a checkbox, that when checked, turns an icon into a bitmapdrawable. I then want that bitmap drawable to be able to show up in a gridView. I can't seem to get this working. Here is my checkbox:
addCheckbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (addCheckbox.isChecked()) {
System.out.println("Checked");
PackageManager pm = mContext.getPackageManager();
Drawable icon = null;
try {
icon = pm
.getApplicationIcon(entry.packageName);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Drawable default_icon = pm.getDefaultActivityIcon();
if (icon instanceof BitmapDrawable
&& default_icon instanceof BitmapDrawable) {
BitmapDrawable icon_bd = (BitmapDrawable) icon;
Bitmap icon_b = icon_bd.getBitmap();
BitmapDrawable default_bd = (BitmapDrawable) pm
.getDefaultActivityIcon();
Bitmap default_b = default_bd.getBitmap();
if (icon_b == default_b) {
// It's the default icon
}
}
} else {
System.out.println("Un-Checked");
}
}
然后我试图让drawablebitmap在我的GridView在这里显示出来:
Then I am trying to get the drawablebitmap to show up in my gridView here:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class GridView extends Activity implements OnItemLongClickListener, OnDragListener{
ArrayList<Integer> drawables = new ArrayList<Integer>();
private BaseAdapter adapter;
private int draggedIndex = -1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drag_and_drop_app);
drawables = new ArrayList<Integer>();
drawables.add(R.drawable.pattern1);
drawables.add(R.drawable.pattern2);
android.widget.GridView gridView = (android.widget.GridView) findViewById(R.id.grid_view);
gridView.setOnItemLongClickListener(this);
gridView.setAdapter(adapter = new BaseAdapter() {
@Override
// Get a View that displays the data at the specified position in
// the data set.
public View getView(int position, View convertView,
ViewGroup gridView) {
// try to reuse the views.
ImageView view = (ImageView) convertView;
// if convert view is null then create a new instance else reuse
// it
if (view == null) {
view = new ImageView(GridView.this);
}
view.setImageResource(drawables.get(position));
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
view.setTag(String.valueOf(position));
return view;
}
@Override
// Get the row id associated with the specified position in the
// list.
public long getItemId(int position) {
return position;
}
@Override
// Get the data item associated with the specified position in the
// data set.
public Object getItem(int position) {
return drawables.get(position);
}
@Override
// How many items are in the data set represented by this Adapter.
public int getCount() {
return drawables.size();
}
});
}
有人告诉我试试
ArrayList的绘制对象,而不是整数,它工作得很好,但我不知道如何添加该位图。
I was told to try ArrayList Drawable instead of Integer, which worked fine but I'm not sure on how to add the bitmap.
我如何添加在我的复选框作出的位图onCheckedChange方法在我的GridView中显示?
How can I add the bitmap made in my checkbox onCheckedChange method to show up in my gridView?
推荐答案
当您使用ArrayList的绘制对象,而不是整数,那么你必须将位图添加到该数组绘制,然后调用notifyDataSetChanged适配器()函数。它会像
When you are using ArrayList Drawable instead of Integer then you must add your bitmap to that drawable array and then call notifyDataSetChanged() function of the adapter. It will be something like
drawables.add(default_bd);
adapter.notifyDataSetChanged()
这篇关于需要帮忙绘制从一类到GridView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!