下面的代码不会引发任何错误但是当我在模拟器中运行应用程序时,它会在应用程序中显示任何图像
MainActivity
package com.example.gridview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView;
public class MainActivity extends Activity {
GridView mygrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mygrid=(GridView) findViewById(R.id.gridView1);
mygrid.setAdapter(new demadap(this));
}
class coundic
{
String name;
int image;
public coundic(String name, int image)
{
this.name=name;
this.image=image;
}
}
class demadap extends BaseAdapter
{
ArrayList<coundic> list;
Context context;
String[] imagename;
int[] image;
public demadap(Context context)
{
this.context=context;
list=new ArrayList<coundic>();
Resources res=context.getResources();
String[] tempactor=res.getStringArray(R.array.actor);
int[] image={R.drawable.meme1, R.drawable.meme2, R.drawable.meme3, R.drawable.meme4};
for(int i=0;i<4;i++)
{
coundic tempdata=new coundic(tempactor[i],image[i]);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
class viewholder
{
ImageView actor;
public viewholder(View v) {
actor=(ImageView) v.findViewById(R.id.imageView1);
}
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2)
{
View row=arg1;
viewholder holder=null;
if(row==null)
{
LayoutInflater inflater=(LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
row=inflater.inflate(R.layout.single_image, arg2, false);
holder=new viewholder(row);
row.setTag(holder);
}
else
{
holder=(viewholder) row.getTag();
}
coundic temp=list.get(arg0);
holder.actor.setImageResource(temp.image);
return row;
}
}
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">gridview</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="actor">
<item>actor1</item>
<item>actor2</item>
<item>actor3</item>
<item>actor4</item>
</string-array>
</resources>
activitymain.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:numColumns="auto_fit"
android:horizontalSpacing="0dp"
android:verticalSpacing="0dp"
android:stretchMode="spacingWidthUniform">
</GridView>
</RelativeLayout>
singleimage.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/meme1"/>
</RelativeLayout>
的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gridview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.gridview.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
只需更改你的for循环,如下所示:
for(int i=0;i<image.length;i++)
{
coundic tempdata=new coundic(tempactor[i],image[i]);
}
只需更改ViewHolder
,如下所示:
class viewholder
{
ImageView actor;
}
在getView
方法中写如下:
@Override
public View getView(int arg0, View arg1, ViewGroup arg2)
{
View row=arg1;
viewholder holder=null;
if(row==null)
{
LayoutInflater inflater=(LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
row=inflater.inflate(R.layout.single_image, arg2, false);
holder=new viewholder();
holder.actor=(ImageView) row.findViewById(R.id.imageView1);
row.setTag(holder);
}
else
{
holder=(viewholder) row.getTag();
}
coundic temp=list.get(arg0);
holder.actor.setImageResource(temp.image);
return row;
}
答案 1 :(得分:0)
只需删除 activitymain.xml
中的行android:stretchMode="spacingWidthUniform"
还在demadap构造函数的for循环中添加list.add(tempdata)
。