Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4057
  • Last Modified:

How to display json data in listview using android

Hi,
I have using below code to display datas in json. It displays output in table view. But i want to show datas in list view. Do you have any examples to show output in list view in my code.

package com.example.design;

import java.util.ArrayList;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class AllProductsActivity extends Activity {
      
      String data = "";
    TableLayout tl;
    TableRow tr;
    TextView label;
    ProgressDialog dialog;
   
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.all_products);
            tl = (TableLayout) findViewById(R.id.maintable);
            
            dialog = ProgressDialog.show(AllProductsActivity.this,"", "Loading Products....", true);
            
        final GetDataFromDB getdb = new GetDataFromDB();
        new Thread(new Runnable() {
            public void run() {
                data = getdb.getDataFromDB();
                System.out.println(data);
                 
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<Users> users = parseJSON(data);
                        addData(users);                    
                    }
                });
                 
            }
        }).start();
      }

      public ArrayList<Users> parseJSON(String result) {
        ArrayList<Users> users = new ArrayList<Users>();
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                Users user = new Users();
                user.setId(json_data.getInt("PId"));
                user.setName(json_data.getString("Name"));
                user.setPlace(json_data.getString("Price"));
                user.setDescription(json_data.getString("Description"));
                users.add(user);
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());  
        }
        return users;
    }
 
    void addHeader(){
        /** Create a TableRow dynamically **/
        tr = new TableRow(this);
 
        /** Creating a TextView to add to <span id="IL_AD1" class="IL_AD">the row</span> **/
        label = new TextView(this);
        label.setText("Product Name");
        label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        label.setPadding(5, 5, 5, 5);
        label.setBackgroundColor(Color.RED);
        LinearLayout Ll = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(label,params);
        tr.addView((View)Ll); // Adding textView to tablerow.
 
        /** Creating Qty Button **/
        TextView place = new TextView(this);
        place.setText("Price");
        place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        place.setPadding(5, 5, 5, 5);
        place.setBackgroundColor(Color.RED);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(place,params);
        tr.addView((View)Ll); // Adding textview to tablerow.
       
        /** Creating Description Button **/
        TextView description = new TextView(this);
        description.setText("Description");
        description.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        description.setPadding(5, 5, 5, 5);
        description.setBackgroundColor(Color.RED);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(description,params);
        tr.addView((View)Ll); // Adding textview to tablerow.
 
         // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }
     
    @SuppressWarnings({ "rawtypes" })
    public void addData(ArrayList<Users> users) {
 
        addHeader();
         
        for (Iterator i = users.iterator(); i.hasNext();) {
 
            Users p = (Users) i.next();
 
            /** Create a TableRow dynamically **/
            tr = new TableRow(this);
 
            /** Creating a TextView to add to the row **/
            label = new TextView(this);
            label.setText(p.getName());
            label.setId(p.getId());
            label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            label.setPadding(5, 5, 5, 5);
            label.setBackgroundColor(Color.GRAY);
            LinearLayout Ll = new LinearLayout(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 2, 2, 2);
            //Ll.setPadding(10, 5, 5, 5);
            Ll.addView(label,params);
            tr.addView((View)Ll); // Adding textView to tablerow.
 
            /** Creating Qty Button **/
            TextView place = new TextView(this);
            place.setText(p.getPlace());
            place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            place.setPadding(5, 5, 5, 5);
            place.setBackgroundColor(Color.GRAY);
            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 2, 2, 2);
            //Ll.setPadding(10, 5, 5, 5);
            Ll.addView(place,params);
            tr.addView((View)Ll); // Adding textview to tablerow.
           
            /** Creating Qty Button **/
            TextView Description = new TextView(this);
            Description.setText(p.getDescription());
            Description.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            Description.setPadding(5, 5, 5, 5);
            Description.setBackgroundColor(Color.GRAY);
            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 2, 2, 2);
            //Ll.setPadding(10, 5, 5, 5);
            Ll.addView(Description,params);
            tr.addView((View)Ll); // Adding textview to tablerow.
 
             // Add the TableRow to the TableLayout
            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        }
        dialog.dismiss();
    }
      
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.all_products, menu);
            return true;
      }

}
0
jnj_web_solutions
Asked:
jnj_web_solutions
1 Solution
 
PCableGuyCommented:
You can put the data into a Listview instead of using a table. You have a good start because you already have your data in an ArrayList

You'll need to create a custom adapter Class that extends a BaseAdapter that takes your ArrayList of User as an argument in its Constructor. In my experience, understanding how to extend a BaseAdapter is the key to understanding a custom ListView.

There are other items to consider, but this is an example that is similar to your goal:

http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now