Solved

How to display json data in listview using android

Posted on 2012-12-22
1
3,558 Views
Last Modified: 2013-01-07
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
Comment
Question by:jnj_web_solutions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 12

Accepted Solution

by:
PCableGuy earned 500 total points
ID: 38720506
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
parent control advice for app searches 4 32
GeoTag photo with Andriod 4 40
OneNote in Android phones cannot 'pinch' to re-size to fill screen 8 60
Samsung Dex 3 42
Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
A short article about a problem I had getting the GPS LocationListener working.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question