how to display fetched json data into listview using baseadapter

前端 未结 7 810
不知归路
不知归路 2020-11-29 07:22

I am new to android and java.Recently I am having problem with displaying fetched json data into listview using baseadapter. At first I have used this code

          


        
7条回答
  •  春和景丽
    2020-11-29 07:37

    Get schedule

    public class GetSchedule extends Activity {
    
        // JSON Node Names
        private static final String TAG_SNO = "sno";
        private static final String TAG_STNCODE = "stnCode";
        private static final String TAG_STATION = "station";
        // private static final String TAG_ROUTENO= "routeNo";
        private static final String TAG_ARRIVALTIME = "arrivalTime";
        private static final String TAG_DEPTIME = "depTime";
    
        private JSONArray station = null;
    
        private ListView list;
        //private static String url = "http://railpnrapi.com/api/route/train/";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_get_schedule);
    
            try {
                ArrayList> stnNamelist = new    ArrayList>();
    
                // Getting JSON Array
                // JSONObject job = new JSONObject();
                //JSONArray jArray = ModuleClass.trainScheduleJSONObject.getJSONArray("stnName");
                JSONObject json_data = null;
                station= ModuleClass.trainScheduleJSONObject.getJSONArray(TAG_STATION);
                for (int i = 0; i < station.length(); i++) {
                    json_data = station.getJSONObject(i);
    
                    // JSONObject c = stnName.getJSONObject(0);
    
                    // Storing  JSON item in a Variable
    
                    String sno = json_data.getString(TAG_SNO);
                    String stnCode = json_data.getString(TAG_STNCODE);
                    // String distance= c.getString(TAG_DISTANCE);
                    // String routeNo = c.getString(TAG_ROUTENO);
                    String arrivalTime = json_data.getString(TAG_ARRIVALTIME);
                    String depTime = json_data.getString(TAG_DEPTIME);
                    // String haltTime = c.getString(TAG_HALTTIME);
                    // String tday= c.getString(TAG_TDAY);
                    // String remark = c.getString(TAG_REMARK);
                    // Adding value HashMap key => value
                    stnNamelist = new ArrayList>();
    
                    HashMap map = new HashMap();
                    map.put(TAG_SNO, sno);
                    map.put(TAG_STNCODE, stnCode);
                    map.put(TAG_DEPTIME, depTime);
                    map.put(TAG_ARRIVALTIME, arrivalTime);
                    // map.put(TAG_DEPTIME,depTime );
    
                    stnNamelist.add(map);
                    list = (ListView) findViewById(R.id.listView1);
                    final SimpleAdapter sd;
                    sd = new SimpleAdapter(this, stnNamelist, R.layout.activity_get_schedule, 
                            new String[] { TAG_SNO, TAG_STNCODE, TAG_ARRIVALTIME, TAG_DEPTIME },
                            new int[] { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4});
                    list.setAdapter(sd);
                    /*
                     * list.setOnItemClickListener(new
                     * AdapterView.OnItemClickListener() {
                     * 
                     * @Override public void onItemClick(AdapterView parent, View
                     * view, int position, long id) { Toast.makeText(
                     * MainActivity.this, "You Clicked at " +
                     * stnNamelist.get(+position).get( "name"), Toast.LENGTH_SHORT)
                     * .show(); } });
                     */
                }
                // Set JSON Data in TextView
                // uid.setText(id);
                // name1.setText(name);
                // email1.setText(email);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    
    
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.get_schedule, menu);
            return true;
        }
    
    }
    

提交回复
热议问题