gpt4 book ai didi

php - 在输出上显示列名称(Android ListView )

转载 作者:行者123 更新时间:2023-11-29 11:18:17 24 4
gpt4 key购买 nike

我有这张 MySQL 数据库的图片:

enter image description here

这是我的 php 代码,用于通过 JSON 将数据从 MySQL 获取到 Android 中:

<?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 Java 代码:

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 上的输出将是这样的:

enter image description here

现在,我想要的输出是在输出旁边显示 MySQL 中的列名称(日期和时间除外)。

就像这样:

2016-06-16 17:32:32

qname : 5

qagent : 50

qevent : 7

info1 : 2

info2 : 91

info3 : 1

有人可以教我怎么做吗?抱歉,我是 Android 初学者。非常感谢!

最佳答案

简单地更改 block 分配条目怎么样?

String datetime = c.getString(TAG_DATETIME);

String datetime = String.format("%s : %s", TAG_DATETIME, c.getString(TAG_DATETIME));

并且您可以使用循环而不是许多本地 String xxx 变量来重构代码中的该部分。

或者你可以去检查SimpleAdapter的bindView实现,通过setViewBinder()或者扩展SimpleAdapter来执行你的自定义绑定(bind)功能。

或者你可以放弃SimpleAdapter,使用更灵活的Adapter类。

关于php - 在输出上显示列名称(Android ListView ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39381991/

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