我正在尝试以gridview的形式设置图像,但我无法看到任何图像。我希望它们在活动的缩略图视图中。如何在两列的gridview中显示图像:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_fit_employess"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:id="@+id/admin_homepage_grid"
/>
</LinearLayout>
这是我的Image Adapter类
public class ImageAdapter extends BaseAdapter {
int[] icons={ R.mipmap.new_admissions,R.mipmap.inbox,R.mipmap.courses,R.mipmap.parent,R.mipmap.teacher
};
private Context context;
public ImageAdapter(Context c)
{
context = c;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView==null)
{
imageView=new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(600,600));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5,5,5,5);
}
else{
imageView=(ImageView)convertView;
}
imageView.setImageResource(icons[position]);
return imageView;
}
}
这是我的活动课
public class LibAdminHomepage extends AppCompatActivity {
GridView gv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lib_admin_homepage);
gv=(GridView)findViewById(R.id.admin_homepage_grid);
gv.setAdapter(new ImageAdapter(this));
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
Toast.makeText(LibAdminHomepage.this,""+position,Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(LibAdminHomepage.this,""+position,Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(LibAdminHomepage.this,""+position,Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(LibAdminHomepage.this,""+position,Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(LibAdminHomepage.this,""+position,Toast.LENGTH_SHORT).show();
break;
}
}
});
}
}
答案 0 :(得分:1)
用这个替换你的getCount()
适配器方法,你会得到图像
@Override
public int getCount() {
return icons.length;
}
答案 1 :(得分:0)
您的getCount
适配器
试试这段代码:
public class ImageAdapter extends BaseAdapter {
int[] icons={ R.mipmap.new_admissions,R.mipmap.inbox,R.mipmap.courses,R.mipmap.parent,R.mipmap.teacher
};
private Context context;
public ImageAdapter(Context c)
{
context = c;
}
@Override
public int getCount() {
return icons.length;
}
@Override
public Object getItem(int position) {
return icons[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView==null)
{
imageView=new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(600,600));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5,5,5,5);
}
else{
imageView=(ImageView)convertView;
}
imageView.setImageResource(icons[position]);
return imageView;
}