我已经通过跟随slidenerd的tutorial制作了一个带有gridview的应用。我的问题是我可以看到视图,但它是空的。当我移动手指时,我可以看到顶部,按钮变为蓝色。 这是我的代码: 适配器:
public class GridViewAdapter extends BaseAdapter {
private ArrayList<SingleButton> buttons;
private MainActivity mainActivity;
public GridViewAdapter(MainActivity mainActivity) {
this.mainActivity = mainActivity;
this.buttons = new ArrayList<SingleButton>();
Resources res = mainActivity.getApplicationContext().getResources();
String[] temp = res.getStringArray(R.array.buttons);
int[] buttonImages = { R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4, R.drawable.pic5,
R.drawable.pic6, R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9 };
for (int count = 0; count < 9; ++count) {
this.buttons
.add(new SingleButton(buttonImages[count], temp[count]));
}
}
@Override
public int getCount() {
return this.buttons.size();
}
@Override
public Object getItem(int position) {
return this.buttons.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater layoutInflater = (LayoutInflater) mainActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.single_button, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.buttonImage.setImageResource(buttons.get(position).image);
holder.buttonText.setText(buttons.get(position).buttonText);
return row;
}
class ViewHolder {
ImageView buttonImage;
TextView buttonText;
public ViewHolder(View v) {
this.buttonImage = (ImageView) v.findViewById(R.id.imageViewButton);
this.buttonText = (TextView) v.findViewById(R.id.textViewButton);
}
}
class SingleButton {
int image;
String buttonText;
SingleButton(int image, String buttonText) {
this.image = image;
this.buttonText = buttonText;
}
}
single_button.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageViewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/textViewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageViewButton"
android:layout_centerHorizontal="true"
android:maxLines="1" />
</RelativeLayout>
主要活动布局:
<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"
android:background="@drawable/background"
tools:context="com.example.app.MainActivity" >
<ImageView
android:id="@+id/imageViewLogo"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingBottom="5dp"
android:src="@drawable/company_logo" />
<GridView
android:id="@+id/gridViewButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageViewLogo"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="spacingWidth"
android:verticalSpacing="10dp" >
</GridView>
</RelativeLayout>