gpt4 book ai didi

android - 根据传入的 JSON 数据设置 ToggleButton 状态

转载 作者:行者123 更新时间:2023-11-29 12:44:10 25 4
gpt4 key购买 nike

我正在尝试关注 Android tutorial从 Android 应用程序修改 MySQL 数据库数据。我对 JSON 解析的概念相当陌生,并且一直坚持根据通过 JSON 从数据库接收到的“状态”值在应用程序的 ListView 中设置切换值。在我的数据库中,表“devices”中有一个名为“status”的字段,并且 ToggleButton 与通过 JSON 接收的每一行相关联。我想使用检索到的值(0 或 1)将应用程序中的关联切换设置为“开”(如果该值为 1)和“关闭”(如果检索到的值为 0)。但我不知道如何实现这一点。

这是显示我的 ListView 的 Activity 中的 doInBackground 函数:

protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());

try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);

// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);

// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String status = c.getString(TAG_STATUS);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_STATUS, status);

// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewDeviceActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllDevicesActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME, TAG_STATUS},
new int[] { R.id.pid, R.id.name, R.id.status });
// updating listview
setListAdapter(adapter);
}
});

}

XML 文件是:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="@+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />

<TextView
android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />

<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="7dip"
android:textSize="17dip"
android:textStyle="bold" />

<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="7dp"
android:text="ToggleButton" />

</RelativeLayout>

值(名称、ID 和状态)接收得很好,到目前为止我已经达到: Screenshot of the app after execution of above code.

但我不知道如何使用收到的“状态”值来更改与每个 ListView 项关联的切换。我尝试在 onCreate 方法中定义 ToggleButton,如下所示:

tb = (ToggleButton) findViewById(R.id.toggleButton1);

然后在上面的 doInBackground 方法的 for 循环中尝试这个:

if (status == "1")
{
tb.setChecked(true);
}

但是如果我这样做,应用程序就会崩溃,可能是因为我不知道将相关代码放在哪里。任何有关此问题的帮助都将不胜感激。

谢谢!

最佳答案

我使用 editText 从 JSON 检索值,然后将其转换为切换按钮

在 onPostExecute() 中尝试一下

protected void onPostExecute(Void result) {
try {
EditText1.setText(json.getString("your_param"));
EditText1.setVisibility(View.INVISIBLE);


if (EditText1.getText().toString().equalsIgnoreCase("1")) {

toggleButton1.setChecked(true);
} else if (EditText1.getText().toString().equalsIgnoreCase("0")) {

toggleButton1.setChecked(false);

}
}

关于android - 根据传入的 JSON 数据设置 ToggleButton 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25686261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com