我正在尝试使用 GridView 显示图像。这是我第一次使用 GridView,所以我使用这里的示例并将其实现到我的(我已经尝试了其中包含的示例,它的工作原理)。
但是,我检查了很多次,LogCat 没有错误,没有强制关闭,图像没有显示。我不知道哪里错了。
这是我的代码:
选择图片.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg_inner">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/book_inner"
android:layout_marginTop="50dp"
/>
<ImageButton
android:id="@+id/homeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home_btn"
android:background="@null"
/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/bg_arrow_btn"
android:layout_alignParentRight="true"
/>
<ImageButton
android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/right_arrow"
android:background="@null"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_marginRight="7dp"
android:layout_marginLeft="7dp"
/>
<ImageButton
android:id="@+id/prevBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/left_arrow"
android:background="@null"
android:layout_toLeftOf="@+id/nextBtn"
android:layout_marginTop="5dp"
/>
<GridView
android:id="@+id/gridView1"
android:numColumns="3"
android:gravity="center"
android:columnWidth="30dp"
android:stretchMode="columnWidth"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="70dp"
>
</GridView>
</RelativeLayout>
动物按钮.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:visibility="gone">
</TextView>
ImageAdapter.java
public class ImageAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<String> listNm;
private ArrayList<Integer> listAnim;
public ImageAdapter(Activity activity,ArrayList<String> listName, ArrayList<Integer> listImage) {
super();
this.listNm = listName;
this.listAnim = listImage;
this.activity = activity;
}
public static class ViewHolder
{
public ImageView imgViewAnim;
public TextView txtViewAnim;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.animalbutton, null);
view.txtViewAnim = (TextView) convertView.findViewById(R.id.textView1);
view.imgViewAnim = (ImageView) convertView.findViewById(R.id.grid_item_image);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewAnim.setText(listNm.get(position));
view.imgViewAnim.setImageResource(listAnim.get(position));
return convertView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listNm.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
选择pic.java
public class choosepic extends Activity {
/** Called when the activity is first created. */
ImageAdapter mAdapter;
GridView gridView;
static final String[] animal = new String[] {
"cat", "cow","croc", "duck", "elephant", "giraffe", "lion", "moose", "mouse"};
private ArrayList<String> listNm;
private ArrayList<Integer> listAnim;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choosepic);
gridView = (GridView) findViewById(R.id.gridView1);
prepare_list1();
mAdapter = new ImageAdapter(this,listNm, listAnim);
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Toast.makeText(getApplicationContext(), mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
}
});
}
public void prepare_list1(){
listNm = new ArrayList<String>();
listAnim = new ArrayList<Integer>();
for (int i = 0; i < animal.length; i++) {
listNm.add(animal[i]);
listAnim.add(getResources().getIdentifier("anim_"+animal[i], "drawable", getPackageName()));
}
}
}
我需要帮助。我感谢任何帮助。先感谢您!