使用网格视图和图像查看以显示一些图像

时间:2013-12-03 09:11:36

标签: android arrays gridview imageview

我正在创建一个简单的Android应用程序,它将显示多个图像,在选定的一个上,系统会在图像视图中显示它..但问题是系统显示图像的id而不是特定的选择图像系统在图像视图中显示图像。

任何人都可以帮我解决这个问题吗?

log cat

12-03 08:57:16.493: E/AndroidRuntime(3126): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
12-03 08:57:16.493: E/AndroidRuntime(3126):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)

GridActivity.java

package com.devleb.listviewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class GridActivity extends Activity implements
        AdapterView.OnItemClickListener {

    private ImageView Selection;
    private static final Integer[] items = { R.drawable.image1,
            R.drawable.image2, R.drawable.image3, R.drawable.image4,
            R.drawable.image5, R.drawable.image6, R.drawable.image4,
            R.drawable.image2 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid);
        Selection = (ImageView) findViewById(R.id.selection);
        GridView grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(new ArrayAdapter<Integer>(this, R.layout.cell, items));

        grid.setOnItemClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.grid, menu);
        return true;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

        Selection.setImageResource(items[arg2]);
    }

}

activity_grid.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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=".GridActivity" >

    <ImageView
        android:id="@+id/selection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <GridView
        android:id="@+id/grid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="100dip"
        android:gravity="center"
        android:horizontalSpacing="5dip"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="40dip" >
    </GridView>

</LinearLayout>

cell.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imgv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

</ImageView>

2 个答案:

答案 0 :(得分:2)

我已经通过自定义适配器修改了您的代码,现在它工作正常,请注意它。

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends Activity implements
        AdapterView.OnItemClickListener {

    private ImageView selection;
    private static final Integer[] items = { R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView selection = findViewById(R.id.selection);
        GridView grid = findViewById(R.id.grid);
        // grid.setAdapter(new ArrayAdapter<Integer>(this, R.layout.cell,
        // items));
        grid.setAdapter(new CustomGridAdapter(this, items));
        grid.setOnItemClickListener(this);

    }

    // @Override
    // public boolean onCreateOptionsMenu(Menu menu) {
    // // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.grid, menu);
    // return true;
    // }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Clicked position is" + arg2,
            Toast.LENGTH_LONG).show();
        //Selection.setImageResource(items[arg2]);
    }

    // Here is your custom Adapter

    public class CustomGridAdapter extends BaseAdapter {
        private Activity mContext;

        // Keep all Images in array
        public Integer[] mThumbIds;

        // Constructor
        public CustomGridAdapter(MainActivity mainActivity, Integer[] items) {
            this.mContext = mainActivity;
            this.mThumbIds = items;
        }

        @Override
        public int getCount() {
            return mThumbIds.length;
        }

        @Override
        public Object getItem(int position) {
            return mThumbIds[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(mThumbIds[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
            return imageView;
        }

    }

}

答案 1 :(得分:1)

ArrayAdapter要求资源ID为TextView XML 异常意味着您不提供ArrayAdapter期望的内容。

使用此构造函数时:

new ArrayAdapter<String>(this, R.layout.cell,items)

R.Layout.cell必须是仅包含TextView的xml布局文件的ID(TextView 不能包含其他布局,例如LinearLayoutRelativeLayoutImageView等!),如下所示:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    // other attributes of the TextView
/>

如果您希望列表行布局略有不同,那么简单的TextView窗口小部件将使用此构造函数:

new ArrayAdapter<String>(this, R.layout.cell, 
   R.id.the_id_of_a_textview_from_the_layout, items)

您提供可以包含各种视图的布局的id,但必须包含TextViewid(第三个参数)您传递给ArrayAdapter,以便知道将Strings放在行布局中的位置。