【问题标题】:Adding radio buttons to a table android studio将单选按钮添加到表 android studio
【发布时间】:2020-09-02 02:27:26
【问题描述】:

我正在开发一个考勤应用程序。所以我想找回我的卷号。并从我的数据库中命名数据并将其显示在表格布局中,并为每条记录添加 2 个单选按钮(存在和不存在),但我无法这样做。

这是我的代码:

package com.example.attendance;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class Take_attendance1 extends AppCompatActivity {

    TextView st_roll,st_name;
    Databases db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_attendance1);

        db = new Databases(this);
        st_roll = (TextView)findViewById(R.id.st_roll1);
        st_name = (TextView)findViewById(R.id.st_name1);
        String course = getIntent().getStringExtra("coursesel");
        String sem = getIntent().getStringExtra("semsel");

        db = new Databases(this);
        StringBuilder stringBuilder[] = db.getStudentData(course,sem);

        st_roll.setText(stringBuilder[1]); //for roll no
        st_name.setText(stringBuilder[2]); //for name
    }
}

这是我的应用程序的快照:

现在我想要所有记录的单选按钮,但它只显示第一个。

【问题讨论】:

标签: android sqlite android-studio radio-button tableview


【解决方案1】:

您可以尝试使用 RecyclerView 或 ListView 。这将比为个别学生设置文本视图和单选按钮花费更少的时间,因为您只需为一个学生 Viewholder 设计布局,然后只需通过适配器传递数据。

有关列表视图的帮助,请参阅:ListView Documentation

有关 RecyclerView 的帮助,请尝试以下两个链接:

ReyclerView Documentation

RecyclerView Guide

如果数据不多,我建议您使用 ListView。但是,如果您确实选择使用 RecyclerView,这可能会很有用。

CheckBox in RecyclerView keeps on checking different items

如果您很难在两者之间做出选择,请阅读此问题。

RecyclerView vs. ListView

编辑

因此,在 cmets 中,您问我如何区分单选按钮,因为您只有一个 id。

为您拥有的无线电组设置一个 OnCheckedStateChanged 侦听器。你可以创建一个 Hashmap/List/Array 或者你甚至可以直接更新你的基础。 这是一个例子。

public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {  
     Boolean attendance;   
     //set other data from database here.
     holder.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
      selected = holder.findViewById(checkedId)
      //checkedid is id of selected button.

    }
});
if(selected.getText().toString.equals(P))
   //update database
   //You will have the roll.no of the current student which I'm assuming is your primary key 


}

【讨论】:

  • 如果我使用 listview,我将如何区分不同的单选按钮,因为单选按钮只有一个 id,所以我如何为不同学生的出勤率取值。
  • @ashurawat 我已经编辑了我的答案,您现在可以查看
【解决方案2】:

尝试使用循环 但更好的选择是使用 listview 或其升级版本,即 recycler view

【讨论】:

    猜你喜欢
    • 2017-09-27
    • 2013-08-09
    • 2014-01-09
    • 2011-06-05
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多