Установить текстовую радио-кнопку из JSON Android Studio - AlertDialog - PullRequest
0 голосов
/ 12 января 2020

У меня есть имя пользователя от 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

Я хочу установить имя пользователя "Ronaldo" и "Velguri" для моего радио кнопка в form_tugas. xml. Что мне делать ?

Спасибо за помощь :)

1 Ответ

0 голосов
/ 12 января 2020

измените ваш метод 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();
    }
...