【问题标题】:Set Text Radio Button from JSON Android Studio - AlertDialog从 JSON Android Studio 设置文本单选按钮 - AlertDialog
【发布时间】:2020-01-12 17:58:58
【问题描述】:

我有来自 JSON(数据库)的用户名。如果我从 json 设置文本单选按钮该怎么办?

这是我的自定义布局 form_tugas.xml

 <RadioGroup
        android:id="@+id/radiogrup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="40dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2">

        <androidx.appcompat.widget.AppCompatRadioButton
            android:id="@+id/radio1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Nama"
            android:checked="true">
        </androidx.appcompat.widget.AppCompatRadioButton>
        <androidx.appcompat.widget.AppCompatRadioButton
            android:id="@+id/radio2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Nama">

        </androidx.appcompat.widget.AppCompatRadioButton>

    </RadioGroup>

这是我的代码

private void formTugas()
{

    getJson("http://192.168.43.144/TA/getUserTeknisi.php");
    dialog = new AlertDialog.Builder(this);
    inflater = getLayoutInflater();
    dialogView = inflater.inflate(R.layout.form_tugas,null);
    dialog.setView(dialogView);
    dialog.setCancelable(true);
    dialog.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    dialog.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    dialog.show();
}

这是我从 JSON 获取用户名时的代码

private void ambilUserTeknisi(String json) throws JSONException
{
    JSONObject jsonObject = new JSONObject(json);
    JSONArray jsonArray  = jsonObject.getJSONArray("data");

    int x = 1;

    for (int u = 0; u < jsonArray.length(); u++)
    {
        JSONObject ob = jsonArray.getJSONObject(u);
        String user  = ob.getString("username");

        users.add(new User(user));

        Log.d("username",""+users.get(u).getUsername());

    }
}

这是我的日志

2020-01-12 16:25:15.510 15549-15549/com.example.adum D/username: Ronaldo
2020-01-12 16:25:15.510 15549-15549/com.example.adum D/username: Velguri

我想在 form_tugas.xml 中为我的单选按钮设置用户名“Ronaldo”和“Velguri”。我应该怎么办 ?

感谢您的帮助:)

【问题讨论】:

    标签: android json radio-button


    【解决方案1】:

    将您的 formTugas 方法更改为:

     private void formTugas(){
    
            getJson("http://192.168.43.144/TA/getUserTeknisi.php");
            //add your json parse method here
            ambilUserTeknisi("your json")
    
            dialog = new AlertDialog.Builder(this);
            inflater = getLayoutInflater();
            dialogView = inflater.inflate(R.layout.form_tugas,null);
            dialog.setView(dialogView);
            //find your view
            AppCompatRadioButton radio1= (AppCompatRadioButton) dialog.findViewById(R.id.radio1);
            AppCompatRadioButton radio2= (AppCompatRadioButton) dialog.findViewById(R.id.radio2);
            //set text to your view
            radio1.settext(users.get(0).name); //set name from your user object 
            radio2.settext(users.get(1).name);
    
            dialog.setCancelable(true);
            dialog.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
    
                }
            });
            dialog.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
    
                }
            });
            dialog.show();
        }
    

    【讨论】:

    • form_tugas.xml 是我的自定义布局。我试过你的代码,但我的应用程序崩溃了。如果我添加我的代码 setContentView(R.layout.form_tugas);在方法形式 tugas 中,它可以工作,但它没有显示为警报对话框。我该怎么办?
    • 在第三行你需要传递你的 json 来解析数据。你能把你的崩溃发到这里来帮助你吗?
    • 这是我的日志猫
    • 2020-01-12 19:00:02.152 29051-29051/? I/art:原因:java.lang.ClassNotFoundException:在路径上找不到类“android.view.View$OnUnhandledKeyEventListener”:DexPathList [[zip 文件“/data/app/com.example.adum-1/base .apk"],nativeLibraryDirectories=[/data/app/com.example.adum-1/lib/arm64, /system/lib64, /vendor/lib64]]
    • 我认为我的应用程序因此而崩溃。谢谢你的帮助:)
    猜你喜欢
    • 1970-01-01
    • 2011-08-14
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多