【发布时间】: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