Android Gridview 按钮

发布于 2024-12-12 04:42:53 字数 2066 浏览 0 评论 0原文

我想要带有按钮的网格视图。以下是主页的代码。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2024-12-19 04:42:53

看起来适配器中的空 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文