我尝试在我的GridView上使用setOnItemClickListener和一些按钮,但它不起作用。 这是我的CustomGrid适配器:
package com.example.omar.soundboard;
/**
* Created by Omar on 10/02/2015.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import java.util.ArrayList;
public class CustomGrid extends BaseAdapter
{
private Context mContext;
private ArrayList<String> arr;
public CustomGrid(Context c, ArrayList<String> arr)
{
mContext = c;
this.arr = arr;
}
@Override
public int getCount()
{
return arr.size();
}
@Override
public Object getItem(int position)
{
return arr.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
Button btn = (Button) grid.findViewById(R.id.button);
btn.setLayoutParams(new RelativeLayout.LayoutParams(100,75));
btn.setPadding(1,1,1,1);
btn.setText(arr.get(position));
}
else
{
grid = (View) convertView;
}
return grid;
}
}
这是grid_single.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prova"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
这是活动代码:
package com.example.omar.soundboard;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
private GridView grid;
private ArrayList<String> nomi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nomi = new ArrayList<String>();
nomi.add("Cosimo");
nomi.add("Angela");
nomi.add("Prova");
nomi.add("Prova2");
final CustomGrid adapter = new CustomGrid(this, nomi);
grid = (GridView)findViewById(R.id.gridSound);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Test", Toast.LENGTH_LONG).show();
}
});
}
这是activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp" tools:context=".MainActivity">
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/gridSound"
android:numColumns="3"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
我在Android 2.3.3上测试它,按钮显示但是当我按下其中一个按钮时没有任何反应! 有人能帮我吗?我无法弄清楚问题是什么。