Hey guys I am relatively new to android development and need some help with JSON Array so that I can create a custom listview. Here is what i know/Have
I created a Main.xml file (contains 2 textview's and a listview
then I created second xml file for the custom listview (filename: customlist.xml)
I successfully got data from the mySQL server into the JSON Array.
Now I am suck on how to send/use the data from JSON array(did convert to string) to the create a custom list view.
here is the code for the JSON Array:
try { //allocate memory for a JSON Array
JSONArray jArray = new JSONArray(result);
// arrays to hold animal name and ID based on the returned length of JSON array
final String [] apptArrayName = new String[jArray.length()];
final String [] apptArrayID = new String[jArray.length()];
//vars that will be grabbed from db
String animalName;
String ID;
String time;
String reason;
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
animalName=json_data.getString("animalName");
ID=json_data.getString("animalID");
time=json_data.getString("time");
reason=json_data.getString("reasonvisit");
//put the animal name and ID into corresponding arrays
//append time to show user time of appointment
apptArrayName[i] = animalName + " - " + time + " - " + reason;
apptArrayID[i] = ID;
}
//list view that is populated by data returned based on the current date
final ListView appointmentList = (ListView)findViewById(R.id.appointmentList);
final ArrayAdapter<String> arrayadpt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, apptArrayName);
appointmentList.setAdapter(arrayadpt);
//sets the onclicklistener of the selected item in arraylist (setting onclicklistener to go to next screen from the listview)
appointmentList.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
//make the list view option go to the next intent
animalIntent.putExtra("animalName", apptArrayName[position]);
animalIntent.putExtra("animalID", apptArrayID[position]);
startActivity(animalIntent);
}
});`
Any help is appreciated