1
votes

I have a GridView arranged in a 3x4 matrix, each item in the GridView is a FrameLayout containing an ImageView and a Textview. Initially the TextViews are set to display the letter "X". When clicked on, the letter is changed to "Y".

It works on all the GridView items except the first one. If I click on the first one (top left) then nothing happens, no matter how long I wait. If I then click anywhere else on the Android emulator screen (even outside the GridView itself), at that point the top left GridView item changes to "Y".

I have a gut feeling it is something to do with the fact that "getView" in "ImageAdapter" is being called several times with "position=0" before the GridView is rendered, the sequence is as follows:

0 0 1 2 3 4 5 6 7 8 9 10 11 0

Now maybe that's just how Android works, I don't know, but it seems strange. If anyone can help me out I'd appreciate it so much.

UPDATE: it appears the problem is related to "MyView=convertView" in getView(), if I simply remove this and always create a new View (as if convertView is null always) then the problem no longer exists. Now I don't think this solution will be an issue as I don't use scrolling and the number of Views created should be 12 (i.e. a 3x4 GridView), but if anyone can explain the correct solution to the problem it would help a lot, thanks.

My main activity (and only activity):

package com.xxxxxx.test;

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

public class MainActivity extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

        init_gridview();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void init_gridview()
    {
            setContentView(R.layout.game);
            GridView gridview = (GridView) findViewById(R.id.gridview);
            gridview.setNumColumns(3);
            gridview.setColumnWidth(32);
            gridview.setAdapter(new ImageAdapter(this));

            gridview.setOnItemClickListener(new OnItemClickListener() 
            {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
                {
                    grid_click(position,v);
                }
            });
    }

    public class ImageAdapter extends BaseAdapter 
    {
            private Context mContext;

            public ImageAdapter(Context c) 
            {
                mContext = c;
            }

            public int getCount() 
            {
                return(3*4);
            }

            public Object getItem(int position) 
            {
                return null;
            }

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

            public View getView(int position, View convertView, ViewGroup parent) 
            {
                View MyView = convertView;

                if (convertView == null) 
                {  
                    LayoutInflater li = getLayoutInflater();
                    MyView = li.inflate(R.layout.grid_item, null);
                }
                else
                {
                    MyView=convertView;
                }

                TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
                tv.setText("X");

                ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image);
                iv.setImageResource(R.drawable.tile);

                MyView.setLayoutParams(new GridView.LayoutParams(32,32));
                return(MyView); 
        }
    }

    private void grid_click(int position, View v)
    {
        View clicked_view=v;

        TextView tv2 = (TextView)clicked_view.findViewById(R.id.grid_item_text);
        tv2.setText("Y");
    }
}

The main layout, called game.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Layout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@android:color/white">

<GridView 
        android:id="@+id/gridview"
        android:background="@android:color/white"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:columnWidth="106dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="0dp"
        android:horizontalSpacing="0dp"
        android:stretchMode="none"
        android:gravity="center"
        android:listSelector="@android:color/transparent"
    />
</LinearLayout> 

Finally the FrameLayout for each item in the GridView, called grid_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/grid_item_image"
        android:src="@drawable/tile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="contentDescription"
    />

    <TextView
        android:id="@+id/grid_item_text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textStyle="bold"  
        android:textSize="17sp"  
        android:shadowColor="#555555"
        android:shadowDx="1.0"
        android:shadowDy="1.0"
        android:shadowRadius="1.5"
    />   

</FrameLayout>
1

1 Answers

0
votes

I think the problem is with your grid_click function it assumes that the view being clicked is the whole cell (FrameLayout) when it could possibly be just the image or text view itself.