我有MySQL数据库的这张照片:
这是我的PHP代码,用于通过JSON从MySQL获取数据:
double()
这是我的Android的Java代码:
<?php
$con=mysqli_connect("localhost","root","");
mysqli_select_db($con,"qstatslite")or die("error");
$q = mysqli_query($con,"SELECT * FROM queue_stats ORDER BY queue_stats_id DESC LIMIT 20");
$return_arr = array();
while ($row = mysqli_fetch_array($q)) {
//$return_arr['queue_stats_id'] = $row['queue_stats_id'];
$row_array['datetime'] = $row['datetime'];
$row_array['qname'] = $row['qname'];
$row_array['qagent'] = $row['qagent'];
$row_array['qevent'] = $row['qevent'];
$row_array['info1'] = $row['info1'];
$row_array['info2'] = $row['info2'];
$row_array['info3'] = $row['info3'];
//$return_arr['queue'][] = array('name' = $tableName, 'queue' = $rows);
array_push($return_arr,$row_array);
}
$output = json_encode(array('queue' => $return_arr));
echo $output;
?>
android上的输出就是这样:
现在,我想要输出的是显示除了日期和时间之外的输出旁边的MySQL列名称。
就像这样:
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity2 extends ListActivity{
private ProgressDialog pDialog;
private static String IPaddress = "http://192.168.1.102/";
// URL to get contacts JSON
private static String url = IPaddress + "Projects/GetUsers.php";
private static final String TAG_QUEUE = "queue";
private static final String TAG_DATETIME = "datetime";
private static final String TAG_NAME = "qname";
private static final String TAG_AGENT = "qagent";
private static final String TAG_EVENT = "qevent";
private static final String TAG_INFO1 = "info1";
private static final String TAG_INFO2 = "info2";
private static final String TAG_INFO3 = "info3";
// contacts JSONArray
JSONArray queue = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>>queueList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
queueList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
// Calling async task to get json
new GetEventCounter().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetEventCounter extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity2.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
queue = jsonObj.getJSONArray(TAG_QUEUE);
for (int i = 0; i < queue.length(); i++) {
JSONObject c = queue.getJSONObject(i);
String datetime = c.getString(TAG_DATETIME);
String qname = c.getString(TAG_NAME);
String qagent = c.getString(TAG_AGENT);
String qevent = c.getString(TAG_EVENT);
String info1 = c.getString(TAG_INFO1);
String info2 = c.getString(TAG_INFO2);
String info3 = c.getString(TAG_INFO3);
HashMap<String, String> event = new HashMap<String, String>();
event.put(TAG_DATETIME, datetime);
event.put(TAG_NAME, qname);
event.put(TAG_AGENT, qagent);
event.put(TAG_EVENT, qevent);
event.put(TAG_INFO1, info1);
event.put(TAG_INFO2, info2);
event.put(TAG_INFO3, info3);
queueList.add(event);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity2.this, queueList,
R.layout.list_item, new String[] { TAG_DATETIME, TAG_NAME, TAG_AGENT, TAG_EVENT, TAG_INFO1, TAG_INFO2, TAG_INFO3},
new int[] { R.id.datetime, R.id.qname, R.id.qagent, R.id.qevent, R.id.info1, R.id.info2, R.id.info3 });
setListAdapter(adapter);
}
}
}
有人可以教我怎么做吗?对不起,我是Android的初学者。非常感谢!
答案 0 :(得分:1)
如何简单地更改块分配条目?
String datetime = c.getString(TAG_DATETIME);
String datetime = String.format("%s : %s", TAG_DATETIME, c.getString(TAG_DATETIME));
并且您的代码中的那部分可以使用循环而不是许多本地String xxx变量进行重构。
或者您可以查看SimpleAdapter的bindView工具,通过setViewBinder()
执行自定义绑定功能或扩展SimpleAdapter。
或者您可以放弃SimpleAdapter,使用更灵活的Adapter类。
答案 1 :(得分:0)
使用BaseAdapter代替SimpleAdapter。 -_-#