如何使用按钮在自定义列表视图中获取值..(android)

时间:2012-05-20 19:13:29

标签: android listview button

我真的需要帮助... 我有一个列表视图列表中的按钮。 我想要做的是获取按下按钮的当前列表的值..(id和name) 我有一个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 ..

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

2 个答案:

答案 0 :(得分:0)

如果您要点击按钮行中的信息,可以执行以下操作:

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
}

答案 1 :(得分:0)

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

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