Android Gridview 按钮
我想要带有按钮的网格视图。以下是主页的代码。
GridView gridview1 = (GridView) findViewById(R.id.gridview);
gridview1.setAdapter(new Buttonadapter(this));
gridview1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View arg1, int position, long id)
{
TextView temp=(TextView)arg1;
CharSequence temp2=temp.getText();
Intent myIntent = new Intent(arg1.getContext(), Diseasemain.class);
myIntent.putExtra("disease", temp2);
startActivityForResult(myIntent, 0);
}
});
这是 ButtonAdapter 类的代码,
public class Buttonadapter extends BaseAdapter {
private Context mContext;
public Buttonadapter(Context c)
{ mContext = c; }
public int getCount()
{ return mThumbIds.length; }
public Object getItem(int position)
{ return null; }
public long getItemId(int position)
{ return 0; } // create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent)
{ Button btn;
if (convertView == null)
{ // if it's not recycled, initialize some attributes
btn = new Button(mContext);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
btn.setLayoutParams(new GridView.LayoutParam(85,85));
btn.setPadding(8, 8, 8, 8);
}
else
{
btn = (Button) convertView;
}
btn.setText(mThumbIds[position]);
return btn; }
private String[] mThumbIds =
{
"hi",
"bye",
"heelo"
}
;}
问题是每当我单击任何按钮时,它都不会触发 onclick 事件,也不会启动下一页。
I want gridview with buttons. following is the code of main page.
GridView gridview1 = (GridView) findViewById(R.id.gridview);
gridview1.setAdapter(new Buttonadapter(this));
gridview1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View arg1, int position, long id)
{
TextView temp=(TextView)arg1;
CharSequence temp2=temp.getText();
Intent myIntent = new Intent(arg1.getContext(), Diseasemain.class);
myIntent.putExtra("disease", temp2);
startActivityForResult(myIntent, 0);
}
});
here is the code of buttonadapter class
public class Buttonadapter extends BaseAdapter {
private Context mContext;
public Buttonadapter(Context c)
{ mContext = c; }
public int getCount()
{ return mThumbIds.length; }
public Object getItem(int position)
{ return null; }
public long getItemId(int position)
{ return 0; } // create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent)
{ Button btn;
if (convertView == null)
{ // if it's not recycled, initialize some attributes
btn = new Button(mContext);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
btn.setLayoutParams(new GridView.LayoutParam(85,85));
btn.setPadding(8, 8, 8, 8);
}
else
{
btn = (Button) convertView;
}
btn.setText(mThumbIds[position]);
return btn; }
private String[] mThumbIds =
{
"hi",
"bye",
"heelo"
}
;}
the problem is whenever I click on any button its not firing the onclick event nd not starting the next page also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来适配器中的空 onclick 方法正在停止
itemclick 监听器被触发。
尝试将启动下一页的代码放在 onclick 中。
looks like the empty onclick method in your adapter is stopping
the itemclick listener from firing.
Try putting the code that starts the next page in the onclick instead.