0
我使用GridView
和自定義適配器(MenuAdapter
)顯示一些文本和圖像,但是當我啓動活動時,它不顯示項目。GridView不顯示帶自定義適配器的項目
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>
對於 「深藏不露」 我的意思是,它只是顯示標題菜單欄德爾
**這是**什麼副本和pas te可以產生... btw偉大的眼睛伴侶!我會盡可能標記您的答案,謝謝 – UrbiJr
歡迎來。 NVM .. – Piyush