【问题标题】:Adding radio button in table row and detecting the button click in android在表格行中添加单选按钮并检测android中的按钮点击
【发布时间】:2018-03-25 16:16:39
【问题描述】:

我在表格行中添加了一个radiobutton,它动态显示来自db 的数据。我想在点击radiobutton 时编辑该行。如何识别radiobutton在哪一行被点击?

String selquery = "select * from student";
Cursor cursor = StudentDB.rawQuery(selquery,null) ;
if(cursor != null){
    if ((cursor != null && cursor.getCount()< 1))
    {
        cursor.close();
    }else{

        int numberOfRecords = cursor.getCount();
        cursor.moveToFirst();
        TableLayout table = (TableLayout)dialog.findViewById(R.id.datatablellayout);
        for(int i=0;i< numberOfRecords;i++){
            // Inflate your row "template" and fill out the fields.
            TableRow row = (TableRow)LayoutInflater.from(SampleDbAct.this).inflate(R.layout.tablewithrow, null);
            ((TextView)row.findViewById(R.id.datatable_name)).setText(cursor.getString((cursor.getColumnIndex("name"))));
            ((TextView)row.findViewById(R.id.datatable_rno)).setText(cursor.getString((cursor.getColumnIndex("roll_number"))));
            ((TextView)row.findViewById(R.id.datatable_place)).setText(cursor.getString((cursor.getColumnIndex("place_name"))));
            ((TextView)row.findViewById(R.id.datatable_class)).setText(cursor.getString((cursor.getColumnIndex("class"))));
            ((TextView)row.findViewById(R.id.datatable_email)).setText(cursor.getString((cursor.getColumnIndex("e_mail"))));
            ((RadioButton)row.findViewById(R.id.radio_btn)).setText(cursor.getString((cursor.getColumnIndex("e_mail"))));
            table.addView(row);

            /*System.out.println("You have entered the Name as:" +cursor.getString((cursor.getColumnIndex("name")))+":");
            System.out.println("You have entered the Roll No as:" +cursor.getString((cursor.getColumnIndex("roll_number"))));
            System.out.println("You have entered the Class as:" +cursor.getString((cursor.getColumnIndex("class"))));
            System.out.println("You have entered the place as:" +cursor.getString((cursor.getColumnIndex("place_name"))));
            System.out.println("You have entered the email as:" +cursor.getString((cursor.getColumnIndex("e_mail"))));*/

            if(!cursor.isLast())
                cursor.moveToNext();
        }
        table.requestLayout();
        cursor.close();
    }
}

StudentDB.close();

【问题讨论】:

    标签: android database radio-button


    【解决方案1】:

    您可以通过使用 button.setOnClickListener 设置单选按钮的 onClickListener 来检测单选按钮的点击。当监听器被调用时,它会传入被触摸的视图。如果仅视图还不够,您可以使用视图的标签传递一些信息,以确定行的数据(通常是数组索引或生成行的对象),使用 setTag() 和 getTag()。

    【讨论】:

    • 能否请您给我代码...对不起,我无法弄清楚,因为我是 android 的初学者
    • 谢谢 Gabe Sechan .. 如果我能得到一些关于如何做到这一点的代码,我会很高兴..
    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多