【问题标题】:how to get a value in Customized listview with button .. (android)如何使用按钮..(android)在自定义列表视图中获取值
【发布时间】:2012-05-20 19:13:29
【问题描述】:

我真的需要这方面的帮助.. 我有一个列表视图,列表中有按钮。 我想要做的是获取按下按钮的当前列表的值..(id和名称) 我有一个 myClickHandler ,当按下按钮时它会调用它..

            public void myClickHandler(View v){
     ListView lvItems = getListView();
    String name = null ;
    for (int i=0; i<lvItems.getChildCount(); i++) 
        {
    }

        AlertDialog alertDialog = new AlertDialog.Builder(
            AllProductsActivity.this).create();
        alertDialog.setTitle("Alert Dialog");
        alertDialog.setMessage("You have selected "+ name);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
    }
            });

并会显示一些带有给定详细信息的 alertDialog..

非常感谢任何愿意帮助我的人..

【问题讨论】:

  • 欢迎来到 SO。如果以下答案之一解决了您的问题,您应该接受它(单击相应答案旁边的复选标记)。这有两件事。它让每个人都知道您的问题已得到解决,并让帮助您的人获得帮助。

标签: android listview button


【解决方案1】:

请看listview的教程: http://www.youtube.com/watch?v=wDBM6wVEO70

然后,通过 getView 使每个视图的 onClickListener 相同,并从它们的 viewHolder 中获取值。 就是这样。

【讨论】:

    【解决方案2】:

    如果您要从单击的按钮所在的行中查找信息,您可以这样做:

    public void myClickHandler(View v){ 
        LinearLayout ll = v.getParent();  // v is the button, the parent should be the rowlayout, assuming LinearLayout
        TextView tv = ll.findViewById(R.id.listviewtext);  // find your textview in the LinearLayout for the row
        String ListText = tv.getText().toString();  // Get the text form the TextView
    
        // rest of your code
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 2016-03-02
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多