PARSing JSON to ListView

前端 未结 3 1821
别那么骄傲
别那么骄傲 2021-01-15 11:00

Okay I have went through tons of examples on PARSing my JSON results, with no luck. I have the below JSON example I don\'t want status info or geoLocation right now. I just

3条回答
  •  有刺的猬
    2021-01-15 11:31

    public class JSONParsingExampleActivity extends Activity {
    /** Called when the activity is first created. */
    
     private ArrayList id;
     private ArrayList name;
     private ArrayList email;
     private ArrayList address;
     private ArrayList gender;
     private ArrayList mobile;
     private ArrayList home;
     private ArrayList office;
     ListView view;
     ProgressDialog mDialog=null;       // Thread code
     private Runnable viewOrders;   // Thread code
     private Thread thread1;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        id=new ArrayList();
        name=new ArrayList();
        email=new ArrayList();
        address=new ArrayList();
        gender=new ArrayList();
        mobile=new ArrayList();
        home=new ArrayList();
        office=new ArrayList();
        view = (ListView)findViewById(R.id.listview);
        mDialog=new ProgressDialog(this);   // Thread code
        mDialog.setMessage("Loading....");
    
    
        viewOrders =new Runnable()  // Thread code
        {
            public void run()       // Thread code
            {
                Json_function();
                runOnUiThread(returnRes);
            }
        };
        thread1=new Thread(null,viewOrders,"Bacground");
        thread1.start();
        mDialog.show();
    }
    
    private Runnable returnRes=new Runnable()   // Thread code
    {
    
        @Override
        public void run()   // Thread code 
        {
        mDialog.cancel();
    
            for(int i=0;i adapter= new ArrayAdapter(JSONParsingExampleActivity.this, android.R.layout.simple_list_item_1, name);
    
            view.setAdapter(new CustomAdapter(JSONParsingExampleActivity.this));        
        }       
    };
    
    private void Json_function() 
    {
        String result=null;
    
        try
        {
            result=getDataFromWebService("http://api.androidhive.info/contacts/");
            System.out.println("length old--> " +result);
        }
        catch(Exception e)
        {
    
        }
        try
        {
            JSONObject json_data=new JSONObject(result);
            JSONArray json_array=json_data.getJSONArray("contacts");
            System.out.println("json array length : - " +json_array.length());
    
            for(int i=0;i " +strUrl);
    
            HttpClient httpclient=new DefaultHttpClient();
            HttpPost httppost=new HttpPost(strUrl);
    
            HttpResponse httpresponse=httpclient.execute(httppost);
            HttpEntity httpentity=httpresponse.getEntity();
    
            is=httpentity.getContent();
    
            int in=httpresponse.getStatusLine().getStatusCode();
            System.out.println("Response code:--> " +in);
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Eroor in http connection" +e.toString());
        }
        try
        {
            int ch;
    
            while((ch=is.read())!=-1)
                strBuffer.append((char)ch);
    
            is.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return strBuffer.toString();
    }
    
        public class CustomAdapter extends BaseAdapter {
        private Context mContext;
        Application app;
        private LayoutInflater inflater=null;
    
        public CustomAdapter(Context c) {
            mContext = c;
             inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }   
    
        public int getCount() {
            return id.size();
        }
    
        public Object getItem(int position) {
            return null;
        }
    
        public long getItemId(int position) {
            return 0;
        }
    
        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View vi=convertView;
            if (convertView == null)        
                 vi = inflater.inflate(R.layout.list, null);
    
    
            TextView txt=(TextView)vi.findViewById(R.id.txtview_id);
            TextView txt1=(TextView)vi.findViewById(R.id.txtview_name);
            TextView txt2=(TextView)vi.findViewById(R.id.txtview_email);
            TextView txt3=(TextView)vi.findViewById(R.id.txtview_address);
            TextView txt4=(TextView)vi.findViewById(R.id.txtview_gender);
            TextView txt5=(TextView)vi.findViewById(R.id.txtview_mobile);
            TextView txt6=(TextView)vi.findViewById(R.id.txtview_home);
            TextView txt7=(TextView)vi.findViewById(R.id.txtview_office);
    
            txt.setText("Id : " + id.get(position));
            txt1.setText("Name : " + name.get(position));
            txt2.setText("Email : " + email.get(position));
            txt3.setText("Address : " + address.get(position));
            txt4.setText("Gender : " + gender.get(position));
            txt5.setText("Mobile : " + mobile.get(position));
            txt6.setText("Home : " + home.get(position));
            txt7.setText("Office : " + office.get(position));
    
            return vi;
        };
    }
    }
    

提交回复
热议问题