0

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

  1. I created a Main.xml file (contains 2 textview's and a listview

  2. then I created second xml file for the custom listview (filename: customlist.xml)

  3. 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

1 Answers1

0

Create your own subclass of ArrayAdapter and override the getView() method. In that method you will get the individual view for each listview item and that is where you populate the view. Search on "android custom list adapter" and you will find a ton of tutorials on how to do this. It is very straightforward.

David C Adams
  • 1,953
  • 12
  • 12
  • alright I just created an Array Adapter and now How do i send the JSON array from main activity to the array Adapter – user3235624 Jan 26 '14 at 20:05
  • When you create the ArrayAdapter you pass your JSON objects in as a List using this constructor ArrayAdapter(Context context, int resource, List objects). Then the ArrayAdapter will call getView(int position, View convertView, ViewGroup parent) for each item in the List. You inflate your list item layout and populate it in that method. You can get the specific JSON object from the Adapter using getItem(position) passing the position passed in to that getView() method. – David C Adams Jan 27 '14 at 04:29