我正在使用GridView
和自定义适配器(MenuAdapter
)来显示一些文本和图片,但是当我启动活动时,它不会显示这些项目。
MenuAdapter:
private Context mContext;
private final String[] gridViewString;
private final int[] gridViewImageId;
public MenuAdapter(Context context, String[] gridViewString, int[] gridViewImageId) {
this.mContext = context;
this.gridViewImageId = gridViewImageId;
this.gridViewString = gridViewString;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public String[] getGridViewString() {
return gridViewString;
}
public int[] getGridViewImageId() {
return gridViewImageId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
view = new View(this.mContext);
view = inflater.inflate(R.layout.gridview_layout, null);
TextView text = (TextView) view.findViewById(R.id.grid_text);
ImageView image = (ImageView) view.findViewById(R.id.grid_image);
text.setText(this.gridViewString[position]);
image.setImageResource(this.gridViewImageId[position]);
} else {
view = convertView;
}
return view;
}
MenuActivity:
private GridView gridViewMenu;
private MenuAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
String[] gridViewString = {
getString(R.string.coke),
} ;
int[] gridViewImageId = {
R.drawable.campione,
};
this.adapter = new MenuAdapter(this, gridViewString, gridViewImageId);
this.gridViewMenu = (GridView) findViewById(R.id.gridView_menu);
this.gridViewMenu.setAdapter(this.adapter);
this.gridViewMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MenuActivity.this, "GridView Item: " + adapter.getGridViewString()[position], Toast.LENGTH_LONG).show();
// Clicked item index
int itemPosition = position;
Intent iback = new Intent(view.getContext(), MainPageActivity.class);
iback.putExtra(MainPageActivity.EXTRA_SELECTED_VALUE, itemPosition);
setResult(Activity.RESULT_OK, iback);
finish();
}
});
}
gridview_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/grid_image"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/campione" />
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:text="TextView"
android:textSize="15dp" />
</LinearLayout>
activity_menu.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="@+id/gridView_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:numColumns="3" >
</GridView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu del bar"
android:textSize="25dp"
android:textStyle="bold" />
</RelativeLayout>
对于“什么都不显示”我的意思是它只显示标题菜单del bar
答案 0 :(得分:2)
此处更改为
@Override
public int getCount() {
return 0;
}
到
@Override
public int getCount() {
return gridViewString.length;
}