我试图按照Android tutorial修改Android应用程序中的MySQL数据库数据。我对JSON解析的概念相当陌生,并且根据通过JSON从数据库接收的“状态”值,我坚持在应用程序的列表视图中设置切换值。在我的数据库中,我在表'devices'中有一个名为'status'的字段,并且ToggleButton与通过JSON接收的每一行相关联。如果值为1,我想使用检索值(0或1)将应用程序中的相关切换设置为ON,如果检索值为0,则将其设置为OFF。但我不知道如何实现这一点。从传入的JSON数据中设置ToggleButton状态
下面是该活动的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.
但我不知道如何使用收到的'状态'值来更改切换关联与每个列表视图项目。我想在这样的onCreate方法来定义切换按钮:
tb = (ToggleButton) findViewById(R.id.toggleButton1);
再尝试这种在上面doInBackground方法的循环:
if (status == "1")
{
tb.setChecked(true);
}
,但应用程序崩溃,如果我这样做,可能是因为我不知道在哪里放置相关的代码。任何帮助,这是赞赏。
谢谢!
“应用程序崩溃” - > logcat的 – 2Dee 2014-09-05 12:51:56
@ 2Dee:从我看,我怀疑LOGCAT会有帮助,因为我无法理解如何使用传入的数据。将上述代码片段放在doInBackground方法中(导致崩溃)只是我的一个尝试。如果有人愿意看看代码,并指导我如何使用传入的“状态”值来修改“切换按钮”状态,那么我可以从中继续。 – 2014-09-06 05:44:05
首先,你可以看一下http://stackoverflow.com/questions/4369537/update-ui-from-thread来理解AsyncTask的基础知识(例如,现在,你在onPostExecute中使用Runnable,这是无用的) 。然后在自定义适配器上找到一个很好的教程,以便学习如何在列表视图中操作视图。这应该让你去;) – 2Dee 2014-09-06 10:35:50