Populating JSON from this link to android Listview

后端 未结 4 1445
余生分开走
余生分开走 2020-12-18 04:58

I am trying to display the elements in listview from the JSON::

JsonURL- https://dl.dropboxusercontent.com/s/rhk01nqlyj5gixl/jsonparsing.txt

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-18 05:15

    Continuing your class:

    public class MainActivity extends Activity {
    
        private static String url="https://www.dropbox.com/s/rhk01nqlyj5gixl/jsonparsing.txt?dl=1";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //Create a JSON parser Instance ----- Used JSON parser from Android
            JSONParser jParser=new JSONParser();
    
            //Getting JSON string from URL ------ Used JSON Array from Android
            JSONArray json=jParser.getJSONFromUrl(url);
    
            List yourData = new ArrayList();
    
            try {
                for(int i=0;i

    adater:

    public class ListAdapter extends ArrayAdapter {
    
        public ListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
            // TODO Auto-generated constructor stub
        }
    
        private List items;
    
        public ListAdapter(Context context, int resource, List items) {
    
            super(context, resource, items);
    
            this.items = items;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View v = convertView;
    
            TextView tt = null;
            TextView tt1 = null;
            TextView tt2 = null;
            TextView tt3 = null;
            TextView tt4 = null;
    
            if (v == null) {
    
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.itemlistrow, null);
    
                tt = (TextView) v.findViewById(R.id.age);
                tt1 = (TextView) v.findViewById(R.id.name);
                tt2 = (TextView) v.findViewById(R.id.city);
                tt3 = (TextView) v.findViewById(R.id.gender);
                tt4 = (TextView) v.findViewById(R.id.birthdate);
            }
    
            Item p = items.get(position);
    
            if (p != null) {
    
                if (tt != null) {
                    tt.setText(""+p.getAge());
                }
                if (tt1 != null) {
    
                    tt1.setText(""+p.getName());
                }
                if (tt2 != null) {
    
                    tt2.setText(""+p.getCity());
                }
    
                if (tt3 != null) {
    
                    tt3.setText(""+p.getGender());
                }
    
                if (tt4 != null) {
    
                    tt4.setText(""+p.getBirthdate());
                }
            }
    
    
    
            return v;
    
        }
    }
    

    object holding data:

    public class WhateverObject{
        private int age;
        private String name;
        private String city;
        private String gender;
        private String birthdate;        
    
        public WhateverObject(int age, String name, String city, String gender, String birthdate){
            this.age = age;
            this.name = name;
            this.city = city;
            this.gender = gender;
            this.birthdate = birthdate;
        }
    
        public int getAge(){
            return this.age;
        }
    
        public String getName(){
            return this.name;
        }
    
        public String getCity(){
            return this.city;
        }
    
        public String getGender(){
            return this.gender;
        }
    
        public String getBirthdate(){
            return this.birthdate;
        }
    }
    

    xml for listview item (save under itemlistrow name):

    
    
        
    
            
        
    
        
    
            
        
    
        
    
            
        
    
        
    
            
        
    
        
    
            
        
    
    
    

    Reused elements from https://stackoverflow.com/a/8166802/1276374


    Simpler example:

    public class MainActivity extends Activity {
    
        private static String url="https://www.dropbox.com/s/rhk01nqlyj5gixl/jsonparsing.txt?dl=1";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //Create a JSON parser Instance ----- Used JSON parser from Android
            JSONParser jParser=new JSONParser();
    
            //Getting JSON string from URL ------ Used JSON Array from Android
            JSONArray json=jParser.getJSONFromUrl(url);
    
            List> personList = new ArrayList>();
    
            try {
                for(int i=0;i createPerson(int age, String name, String city, String gender, String birthdate) {
            HashMap person = new HashMap();
            person.put("person", name+" | "+age + " | "+city + " | "+gender + " | "+birthdate);
            return person;
        }
    
    
    
    }
    

    Thanks to http://www.javacodegeeks.com/2013/06/android-listview-tutorial-and-basic-example.html

提交回复
热议问题