简体   繁体   中英

Json object displaying on listview

 public ArrayList<String> getData()
    {

        ArrayList<String> listItems = new ArrayList<String>();

        String result = "";
        InputStream   isr=null;

        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/get_data.php");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
               isr = entity.getContent();
    }
    catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
            //resultView.setText("Couldnt connect to database");
    }

         try{
             BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso- 8859-1"),8);
             StringBuilder sb = new StringBuilder();
             String line = null;
             while ((line = reader.readLine()) != null) {
                     sb.append(line + "\n");
             }
             isr.close();

             result=sb.toString();
     }
         catch(Exception e){
             Log.e("log_tag", "Error  converting result "+e.toString());
        }
          //parse json data
           try {
               String s = "";
               JSONArray jArray = new JSONArray(result);


               for(int i=0; i<jArray.length();i++)
               {
                   JSONObject json=jArray.getJSONObject(i);


                listItems.add(json.getString("name"));
                listItems.add(json.getString("ph.no"));
                listItems.add(json.getString("id"));
                     // adding HashList to ArrayList

              }   


              // resultView.setText(s);

           } catch (Exception e) 
           {
            // TODO: handle exception
               Log.e("log_tag", "Error Parsing Data "+e.toString());
           }


           return listItems;

           // selecting single ListView item


 }

I am developing an android application called phone book. I am extracting details of person from sever as json object. Displaying details on android

I am able to display contact details of a particular person in different rows. How to display details of a single person on same row.

You can Customizing Android ListView Items with Custom ArrayAdapter.

You can do this way,

/* In main activity */
ListView myListView = (ListView)findViewById(R.id.myListView);
final ArrayList<Phonebook> todoItems = new ArrayList<Phonebook>();
final YourAdapter x= new YourAdapter(this,R.layout.your_listviewlayout,todoItems);
myListView.setAdapter(x);

Phonebook phnbk= new Phonebook();
// enter code for set values to Phonebook class variables
/* Inserting the values to array */
todoItems.add(phnbk);

/* Customized array adaptor class */
private class YourAdapter extends ArrayAdapter<Phonebook> 
{
    private ArrayList<Phonebook> items;
    public YourAdapter (Context context, int textViewResourceId, ArrayList<Phonebook> items) 
    {
        super(context, textViewResourceId, items);
        this.items = items;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View v = convertView;
        if (v == null) 
        {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.your_listviewlayout, null);
        } 
        Phonebook o = items.get(position);
        if (o != null) 
        {
            //insert values to each text view in your list view layout
            tv1.setText(o.name);
            tv2.setText(o.phnnum);
                        tv3.setText(o.email);                   
        }
        return v;
    }
}

/* Phonebook class */
public class Phonebook{    
    public String name;
    public String phnnum;
    public String email;
    public Phonebook(){
        super();
    }

    public Phonebook(String name, String phnnum, String email) {
        super();
        this.name = name;
        this.phnnum = phnnum;
        this.email = email;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM