Gridview.xml
one, built himself res / layout under
android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
android: orientation = "vertical"
>android: id = "@ + id / gridview_id"
android: layout_width = "fill_parent"
android: layout_height = "wrap_content"
android: columnWidth = "60dp"
android: numColumns = "auto_fit"
android: horizontalSpacing = "1dp"
android: verticalSpacing = "10dp"
android: stretchMode = "columnWidth"
android: gravity = "center">
Two activity.java
package com.example.gridview;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener {
private GridView gridview;
private int [] imageIDs = {
R.drawable.p1, R.drawable.p2,
R.drawable.p3, R.drawable.p4,
R.drawable.p5, R.drawable.p6,
R.drawable.p7, R.drawable.p8,
R.drawable.p9
};
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.gridview);
gridview = (GridView) findViewById (R.id.gridview_id);
gridview.setAdapter (new ImageAdapter (this));
gridview.setOnItemClickListener (this);
}
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter (Context c) {
context = c;
}
public int getCount () {
/ / TODO Auto-generated method stub
return imageIDs.length;
}
@ Override
public Object getItem (int position) {
/ / TODO Auto-generated method stub
return imageIDs [position];
}
@ Override
public long getItemId (int position) {
/ / TODO Auto-generated method stub
return position;
}
@ Override
public View getView (int position, View convertView, ViewGroup parent) {
ImageView imageview;
if (convertView == null) {
imageview = new ImageView (context);
imageview.setLayoutParams (new android.view.ViewGroup.LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageview.setScaleType (ImageView.ScaleType.CENTER);
imageview.setPadding (8, 8, 8, 8);
}
else {
imageview = (ImageView) convertView;
}
imageview.setImageResource (imageIDs [position]);
return imageview;
}
}
public void onItemClick (AdapterView parent, View view, int position, long id) {
/ / TODO Auto-generated method stub
Toast.makeText (this, "first" + position + "pictures", Toast.LENGTH_SHORT) show ();.
}
}
Error message
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->Reply:
Used in the GridView control, in addition to writing an xml file (item_gridview.xml), then convertView = LayoutInflater.from (context) inflate (R.layout.item_gridview, null).;
Reply:
No use ah. . . . .
Reply:
In fact, sometimes I have a such a wonderful mistake, then I put a change of control id,,, not out of this error, and still do not know what the reason is,
No comments:
Post a Comment