我将 String [] 和图片传递给图像网格的适配器类。 请检查我的代码,让我知道错误应该在哪里,因为我没有收到任何错误和输出。当程序工作时,我在同一个Adapter类中设置 String [] :(
MainActivity
public class MainActivity extends AppCompatActivity {
public static String[] mThumbIds = {
"http://10.0.2.2/e-stitch/img/1.gif",
"http://10.0.2.2/e-stitch/img/1.gif",
"http://10.0.2.2/e-stitch/img/1.gif",
"http://10.0.2.2/e-stitch/img/1.gif",
"http://10.0.2.2/e-stitch/img/1.gif",
"http://10.0.2.2/e-stitch/img/1.gif",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageAdapter imageAdapter = new ImageAdapter (MainActivity.this);
imageAdapter.setimag(mThumbIds,mThumbIds.length);
final GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// i.putExtra("id", position);
// startActivity(i);
}
});
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
private Context mContext;
int imageTotal;
public static String[] mThumbIds;
public ImageAdapter(Context c) {
mContext = c;
}
public void setimag(String[] mThumbIds,int count){
this.mThumbIds = mThumbIds;
this.imageTotal = count;
}
public int getCount() {
return imageTotal;
}
@Override
public String getItem(int position) {
return mThumbIds[position];
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(480, 480));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
String url = getItem(position);
Picasso.with(mContext)
.load(url)
.placeholder(R.drawable.loader)
.fit()
.centerCrop().into(imageView);
return imageView;
}
}
请帮帮我,因为我是android的新手:(
答案 0 :(得分:4)
这是您错误的地方gridview.setAdapter(new ImageAdapter(this));
,而不是传递初始化的适配器,而是传递没有图像ID的新适配器。
更正后的代码
ImageAdapter imageAdapter = new ImageAdapter (MainActivity.this);
imageAdapter.setimag(mThumbIds,mThumbIds.length);
final GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(imageAdapter);