我正在开发一个Android应用程序,它显示图像的gridview显示和textview。直到现在我用gridview显示来自drawable的图像。我从arraylist变量中的json获取文本值。
我过去只显示图像的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_front_page);
GridView grid=(GridView)findViewById(R.id.maincatgrid);
grid.setAdapter(new ImageAdapter(this));
grid.setColumnWidth( 170 );
grid.setVerticalSpacing(20 );
grid.setStretchMode( GridView.STRETCH_COLUMN_WIDTH );
grid.setNumColumns( GridView.AUTO_FIT );
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c){
mContext = c;
}
public Integer[] mThumbIds = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
R.drawable.image7,
R.drawable.image8,
};
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View MyView = convertView;
// TODO Auto-generated method stub
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(170, 150));
imageView.setAdjustViewBounds(false);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
}
我从包含 15 个值的异步任务中获取 textview 值。当没有来自 mThumbIds 的图像(即 textvalues>images)时,它应该放置我的默认图像 R.drawable.no_image。有什么方法可以实现这一点,然后请建议我。提前致谢。