在GridView中更改onClick for ImageView的第一个图像

时间:2014-09-28 16:51:15

标签: java android gridview imageview

我正在开发一个有座位计划的Android应用程序。座位是放置在GridView中的ImageView。单击时,图像视图将设置为不同的图像。问题是无论我点击哪个座位,只更改了第一个座位的图像。希望能够得到一些帮助,为什么。以下是我的代码:

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:orientation="vertical">

    <TextView
        android:id="@+id/grid_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="15dp"
        android:text="@string/screen"
        android:textSize="20sp"
        android:gravity="center" >
    </TextView>

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:paddingTop="10dp">

    <GridView
        android:id="@+id/gridView"
        android:layout_width="240dp"
        android:layout_height="match_parent"   
        android:paddingTop="15dp"  
        android:listSelector="@android:color/transparent"
        android:numColumns="8"
        android:columnWidth="30dp"
        android:gravity="center"
        android:verticalSpacing="0dp"
        android:stretchMode="none"
        android:layout_centerInParent ="true">
    </GridView>

</RelativeLayout>
</LinearLayout>

seat_item.xml

<LinearLayout 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/seatItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:adjustViewBounds="true"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity implements OnItemClickListener {

    GridView gridview;
    ImageAdapter imageAdapter;
    ArrayList<Item> data = new ArrayList<Item>();
    ImageView image;
    boolean showingFirst = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView(); // Initialize the GUI Components
        fillData(); // Insert The Data
        setDataAdapter(); // Set the Data Adapter
    }

    // Initialize the GUI Components
    private void initView() {
        gridview = (GridView) findViewById(R.id.gridView);
        gridview.setOnItemClickListener(this);
    }

    // Insert The Data
    private void fillData() {
        for (int i = 1; i < 17; i++) {
            data.add(new Item(i, getResources().getDrawable(R.drawable.seat)));
        }
    }

    // Set the Data Adapter
    private void setDataAdapter() {
        imageAdapter = new ImageAdapter(getApplicationContext(),
                R.layout.seat_item, data);
        gridview.setAdapter(imageAdapter);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(final AdapterView<?> arg0, final View view,
            final int position, final long id) {
        image= (ImageView) findViewById(R.id.seatItem);
        if(showingFirst == true){
            String message = "Clicked: " + position;
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
                    .show();
            image.setImageResource(R.drawable.seat_blue);
            showingFirst = false;
        }else{
            image.setImageResource(R.drawable.seat);
            showingFirst = true;
        }
    }
}

ImageAdapter.java

public class ImageAdapter extends ArrayAdapter<Item> {
    private Context mContext;
    int resourceId;
    ArrayList<Item> data = new ArrayList<Item>();
    ImageView image;

    // Constructor
    public ImageAdapter(Context c,int layoutResourceId, ArrayList<Item> data) {
        super(c, layoutResourceId, data);
        this.mContext = c;
        this.resourceId = layoutResourceId;
        this.data = data;
    }

    @Override
    public Item getItem(int position) {
        System.out.println("Get: "+position);
        return data.get(position);
    }

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null)
        {
            final LayoutInflater layoutInflater =
                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(resourceId, parent, false);
            holder = new ViewHolder();
            holder.imgItem = (ImageView) convertView.findViewById(R.id.seatItem);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        Item item = getItem(position);
        System.out.println(position);
        holder.imgItem.setImageDrawable(item.getImage());

        return convertView;
    }
    static class ViewHolder
    {
        ImageView imgItem;
    }
}

2 个答案:

答案 0 :(得分:1)

使用onItemClick第二个参数view从ListView点击的行布局访问ImageView:

image= (ImageView)view.findViewById(R.id.seatItem);

答案 1 :(得分:0)

乍一看,我没有发现您的代码有任何问题。我建议你尝试将click操作委托给可点击的视图本身,而不是使用onItemClick,因此在getView方法中使用setonCLickListener到图像视图,或整个视图,如果这最适合你的解决方案。 在旁注中,我还建议您不要将活动用作回调,因为在大多数情况下,它会导致活动泄漏,创建类或使用无用的类。

希望这会对你有所帮助。