MjBodz MjBodz - 3 months ago 16
Android Question

Displaying Column Names on the output (Android List View)

I have this picture of MySQL database:

enter image description here

And This is My php codes for fetching the data's from MySQL into Android through JSON :

<?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;

?>


And This is My Java Codes for the Android :

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);
}

}

}


And the Output on the android would be this :

enter image description here

Now, What I want for the Output is to Display The names of Columns from MySQL beside the output except for the Date and Time..

Just like this :

2016-06-16 17:32:32

qname : 5

qagent : 50

qevent : 7

info1 : 2

info2 : 91

info3 : 1


Can Someone teach me how to do that please? Sorry I'm a beginner in Android. Thanks a Lot!

Answer

What about simplely change the block assign the entry?

String datetime = c.getString(TAG_DATETIME);

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

and that part in your code you can refactor with a loop instead of many local String xxx variables.

Or you can go to check the SimpleAdapter's bindView implement, do your custom bind function by setViewBinder() or extends SimpleAdapter.

Or you can abandon the SimpleAdapter, use more flexible Adapter classes.