Android - Gridview with Custom BaseAdapter,创建onclicklistener

时间:2013-11-18 16:21:25

标签: android gridview onclicklistener baseadapter

我创建了一个显示字母表字母的gridview。我使用自定义BaseAdapter使用字符串数组填充gridview。

我想要做的是能够获得所点击单元格的值(字母)。为了验证它是否有效,我创建了一个TextView,我希望当用户点击一个项目(单元格)以使用所选单元格的值设置TextView的文本时

enter image description here

我做了一个不起作用的尝试。这是我的代码:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myGridView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:numColumns="7" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/feedback"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

cell.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/grid_item"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="05"
        android:textSize="20sp"
        android:enabled="true"
        android:clickable="true">
    </Button>

</LinearLayout>

MainActivity

public class MainActivity extends Activity {

    private TextView text;
    private GridView gridView;
    private final String[] items = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

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

        text = (TextView) findViewById(R.id.feedback);

        gridView = (GridView) this.findViewById(R.id.myGridView);
        CustomGridAdapter gridAdapter = new CustomGridAdapter(MainActivity.this, items);
        gridView.setAdapter(gridAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                text.setText((String) (gridView.getItemAtPosition(position)));
                Log.i("ITEM_CLICKED", "" + (String) (gridView.getItemAtPosition(position)));
            }
        });

    }
}

CustomGridAdapter

public class CustomGridAdapter extends BaseAdapter {

    private Context context;
    private String[] items;
    LayoutInflater inflater;

    public CustomGridAdapter(Context context, String[] items) {
        this.context = context;
        this.items = items;
        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.cell, null);
        }
        Button button = (Button) convertView.findViewById(R.id.grid_item);
        button.setText(items[position]);

        return convertView;
    }

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

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

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

我做错了什么?

编辑:看来是gridView.setOnItemClickListener(...);没有被调用/执行。我已将其更改为此以便检查它,我在logcat中看不到任何内容。

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("ON_ITEM_CLICK_LISTENER", "item clicked");
            }
        });

3 个答案:

答案 0 :(得分:6)

你的情况下的问题是按钮消耗了点击而没有到达GridView。我看到两个选择:

  1. 简单的:cell.xml Button android:clickable="false" android:focusable="false"MainActivity
  2. Grosso-modo:将Activity作为参数发送到适配器,并从public class MainActivity extends Activity { private TextView text; private GridView gridView; private final String[] items = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.feedback); gridView = (GridView) this.findViewById(R.id.myGridView); CustomGridAdapter gridAdapter = new CustomGridAdapter(MainActivity.this, items); gridView.setAdapter(gridAdapter); } public void itemClicked(int position) { text.setText(items[position]); } } 公开公共方法。如果您考虑重新使用适配器,则发送抽象类型的活动或接口。
  3. 东西:

    public class CustomGridAdapter extends BaseAdapter {
    
        private MainActivity context;
        private String[] items;
        LayoutInflater inflater;
    
        public CustomGridAdapter(MainActivity context, String[] items) {
            this.context = context;
            this.items = items;
            inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        public View getView(final int position, View convertView, ViewGroup parent) {
    
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.cell, null);
            }
            Button button = (Button) convertView.findViewById(R.id.grid_item);
            button.setText(items[position]);
            button.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    context.itemClicked(position);
                }
            });
    
            return convertView;
        }
    
        @Override
        public int getCount() {
            return items.length;
        }
    
        @Override
        public Object getItem(int position) {
            return items[position];
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    }
    

    并在您的适配器中:

    {{1}}

答案 1 :(得分:3)

实际上对于gridview setOnItemClickListener,你没有做错任何事。这是创建问题的单元格视图。尝试将单元格视图按钮更改为textview,它应该工作。现在,当您点击gridview中的项目时,实际上没有点击该项目,因为您的单元格视图按钮具有自己的OnClickListener实现,这就是为什么不调用setOnItemClickListener。

答案 2 :(得分:0)

您可以使用setTag()和getTag(),它允许您放置/获取任何类型的对象。 所以在将来,你将能够传递任何东西(如果你之后进行了适当的演员)

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

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.cell, null);
    }

    Button button = (Button) convertView.findViewById(R.id.grid_item);
    button.setText(items[position]);
    convertView.setTag(items[position]); 
    return convertView;
}

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String str = (String) view.getTag();
            text.setText(str);
            Log.i("ITEM_CLICKED", "" + (String) (gridView.getItemAtPosition(position)));
        }
    });