我工作gridview.i成功创建自定义gridview(图像和文本)我有32项。程序工作完美,但当我尝试运行程序gridview的最后项目有重复。我不知道我做错了什么? 如果有人知道解决方案请帮助我
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="@+id/grid_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ImageView>
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/gridview_background" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="82dp"
android:background="#ff0000" >
<GridView
android:id="@+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:horizontalSpacing="10dp"
android:numColumns="4"
android:verticalSpacing="10dp" >
</GridView>
</RelativeLayout>
public class CustomGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] Flag_ids;
private final int[] Imageid;
public CustomGridViewAdapter(Context c, String[] names, int[] Imageid) {
mContext = c;
this.Imageid = Imageid;
this.Flag_ids = names;
}
@Override
public int getCount() {
return Flag_ids.length;
}
@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) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.row_grid, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView) grid
.findViewById(R.id.grid_image);
textView.setText(Flag_ids[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
public class ChooseChempions extends Activity {
GridView grid;
String[] Flag_names = {
"alg", "arg", "aus", "bel",
"bos","bra", "cam", "chi",
"col", "cos", "cra", "cot",
"ecu", "eng", "fra","ger",
"gha", "gre", "hon","ira",
"ita", "jap", "kor","mex",
"net", "nig", "por", "rus",
"spa", "swi", "uru", "usa"
};
int[] Flags_id = {
R.drawable.alg, R.drawable.arg, R.drawable.aus, R.drawable.bel,
R.drawable.bos, R.drawable.bra, R.drawable.cam, R.drawable.chi,
R.drawable.col, R.drawable.cos, R.drawable.cra, R.drawable.cot,
R.drawable.ecu, R.drawable.eng, R.drawable.fra,R.drawable.ger,
R.drawable.gha, R.drawable.gre, R.drawable.hon, R.drawable.ira,
R.drawable.ita, R.drawable.jap, R.drawable.kor,R.drawable.mex,
R.drawable.net, R.drawable.nig, R.drawable.por, R.drawable.rus
,R.drawable.spa, R.drawable.swi, R.drawable.uru, R.drawable.usa
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_choose_chempions);
grid = (GridView) findViewById(R.id.grid_view);
CustomGridViewAdapter adapter = new CustomGridViewAdapter(
ChooseChempions.this, Flag_names, Flags_id);
Splashscreen.My_sessia = getSharedPreferences(Splashscreen.Sessia_name,
0);
grid.setAdapter(adapter);