开发者_Python百科
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this questionI have been banging my head against the wall- can anyone help with working code for :
http://developer.android.com/resources/tutorials/views/hello-gridview.html
Every time I code it- Eclipse gives me loads of unfathomable errors(especially for a beginner) but even when I copy and paste code- it still doesn't work with android 2.2 sdk. Any help will be welcome!
Secondly it also mentions copying some sample images into the drawable folder under res, however there are drawable-hdpi, ldpi, and mdpi folders- which one should I copy the images into ( or all 3?)
Thanks in advance
Please see my comment to your question related to the Android Tutorials. About the images you should copy them to all 3 OR create a new directory called drawable. Let me explain it with a bit more of detail.
Each device has a density like HDPI, LDPI and MDPI (tablets have even more). The idea of android is to use the images that best fit your device by going to these folders. If it can't find an image, it will look for it on a 'generic' folder called drawable. So, if you're only testing your logic and don't care for the look by now, just create the 'drawable' folder and put your images there.
Here is a working example using a grid view:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new GridView.OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v, int position, long id)
{
//Insert what to do when you click on an image.
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
public ImageAdapter(Context c)
{
context = c;
}
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIDs[position]);
return imageView;
}
Integer[] imageIDs = {
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon
};
}
If you copy and paste that ^^ it should work, you may need to import some stuff (do you know how to do that?!)
Any problems just comment :)
精彩评论