我试图在网格视图项上放置一个imageButton和ImageView,所以这里的代码如下所示,我要做的是 创建一个扩展BaseAdapter的适配器,然后创建一个Viewholder并将ImageView和Imagebutton放入持有者。 但是,没有错误,但是当打开网格视图时,App会停止。
任何线索都会有很大的帮助......谢谢!!
public class DestinationActivity extends Activity implements OnClickListener, OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_view);
GridView grid = (GridView) findViewById(R.id.gvGrid);
myImageAdapter = new ImageAdapter(this);
grid.setAdapter(myImageAdapter);
}
class ViewHolder {
int id;
ImageView imageview;
ImageButton button;
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();
public ImageAdapter(Context c) {
mContext = c;
}
void add(String path) {
itemList.add(path);
}
@Override
public int getCount() {
return itemList.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) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.image_button, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.button = (ImageButton) convertView.findViewById(R.id.ibClose);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.button.setId(position);
holder.imageview.setId(position);
holder.imageview.setPadding(1, 1, 1, 1);
holder.imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageview.setLayoutParams(new GridView.LayoutParams(330, 330));
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position),
330, 330);
holder.imageview.setImageBitmap(bm);
holder.button.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
// // TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hello..!!", Toast.LENGTH_SHORT).show();
}
});
holder.imageview.setImageBitmap(null);
holder.id = position;
return convertView ;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/thumbImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ImageButton
android:id="@+id/ibClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/close_dialog" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gvGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:clipToPadding="false"
android:columnWidth="10dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="2"
android:scrollbarStyle="outsideOverlay"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
/>
</LinearLayout>
答案 0 :(得分:1)
而不是下面的行:
Text::CSV
改为:
convertView = mInflater.inflate(R.layout.gallerydeleteitems, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.imageview = (ImageButton) convertView.findViewById(R.id.ibClose);
答案 1 :(得分:0)
您没有找到ImageButton的ID。检查适配器的getView()方法:
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.imageview = (ImageButton) convertView.findViewById(R.id.ibClose);
另外,下次发布错误日志。
答案 2 :(得分:0)
public class DestinationActivity extends Activity implements OnClickListener, OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_view);
GridView grid = (GridView) findViewById(R.id.gvGrid);
myImageAdapter = new ImageAdapter(this);
grid.setAdapter(myImageAdapter);
}
static class ViewHolder {
ImageView imageview;
ImageButton imageButton;
int position;
}
public class ImageAdapter extends BaseAdapter {
public ImageAdapter() {
}
private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();
public ImageAdapter(Context c) {
mContext = c;
}
void add(String path) {
itemList.add(path);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
}
public View getView(int position, View convertView, final ViewGroup parent) {
Bitmap bm = null;
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.delete_destination, parent, false);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.imageButton = (ImageButton) convertView.findViewById(R.id.ibClose);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.imageButton.setId(position);
holder.imageview.setId(position);
try {
bm = decodeSampledBitmapFromUri(itemList.get(position), 330, 330);
} catch (Throwable e) {
}
holder.imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageview.setPadding(1, 1, 1, 1);
holder.position = position;
holder.imageview.setImageBitmap(bm);
return view;
}
}