how to display fetched json data into listview using baseadapter

First u need t create row_listitem.xml file like:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="@drawable/list_selector"
android:orientation="vertical"
android:padding="5dp" >

<ImageView
    android:id="@+id/iv_icon_social"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_centerVertical="true"
    android:background="@drawable/image_border"
    android:src="@drawable/sms_t"
    android:visibility="gone" />

<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="fill_parent"
    android:layout_height="85dp"
    android:layout_marginRight="50dp"
    android:layout_marginTop="0dp"
    android:layout_toRightOf="@+id/iv_icon_social"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:padding="5dip"
    android:visibility="visible" >

    <TextView
        android:id="@+id/txt_ttlsm_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:text="Sample text"
        android:textSize="18dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txt_ttlcontact_row2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="3dp"
        android:paddingLeft="10dp"
        android:maxEms="20"
        android:maxLines="2"
        android:singleLine="false"
        android:ellipsize="end"
        android:text="Sample text2"
        android:textColor="#808080"
        android:textSize="15dp"
        android:textStyle="normal"
        android:visibility="visible" />


</LinearLayout>

</RelativeLayout>

Now, u need to create Custom BaseAdapter like:

  public class BaseAdapter2 extends BaseAdapter {

private Activity activity;
// private ArrayList&lt;HashMap&lt;String, String&gt;&gt; data;
private static ArrayList title,notice;
private static LayoutInflater inflater = null;

public BaseAdapter2(Activity a, ArrayList b, ArrayList bod) {
    activity = a;
    this.title = b;
    this.notice=bod;

    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return title.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.row_listitem, null);

    TextView title2 = (TextView) vi.findViewById(R.id.txt_ttlsm_row); // title
    String song = title.get(position).toString();
    title2.setText(song);


    TextView title22 = (TextView) vi.findViewById(R.id.txt_ttlcontact_row2); // notice
    String song2 = notice.get(position).toString();
    title22.setText(song2);

return vi;

}

}

Now, u can set up your main activity like:

public class MainActivity extends Activity {

ArrayList<String> title_array = new ArrayList<String>();
ArrayList<String> notice_array = new ArrayList<String>();
ListView list;
BaseAdapter2 adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView) findViewById(R.id.listView1);
    new TheTask().execute();
}

class TheTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        String str = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://10.0.2.2/BSDI/show.php");
            HttpResponse response = httpclient.execute(httppost);
            str = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return str;

    }



@Override
protected void onPostExecute(String result) {

    super.onPostExecute(result);

    String response = result.toString();
    try {


        JSONArray new_array = new JSONArray(response);

        for (int i = 0, count = new_array.length(); i < count; i++) {
            try {
                JSONObject jsonObject = new_array.getJSONObject(i);
                title_array.add(jsonObject.getString("title").toString());
                notice_array.add(jsonObject.getString("notice").toString());

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        adapter = new BaseAdapter2(MainActivity.this, title_array, notice_array);
        list.setAdapter(adapter);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        // tv.setText("error2");
    }

}
}

 }

@Allen Chun , actually its not in my mind currently that what I have changed in my code to run my code perfectly. But I am sharing all my codes which are working perfectly.

Its my layout code,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/fsr"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Notice Board"
    android:textSize="20px" />

   <TextView
    android:id="@+id/err_msg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:gravity="center" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="match_parent" 

    android:id="@+id/list_notice2"

    android:layout_gravity="center" />




</LinearLayout>

This is my custom listview layout codes,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" 
    android:paddingTop="10dp"
    android:layout_gravity="center"
      android:textAppearance="?android:attr/textAppearanceMedium" />
    />

<TextView
    android:id="@+id/notice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Small Text"
    android:paddingTop="5dp"
    android:layout_gravity="center"
    android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

its my activity named "NoticeBoard" .

public class NoticeBoard extends Activity {


ArrayList<String> title_array = new ArrayList<String>();
ArrayList<String> notice_array = new ArrayList<String>();

ListView list;
base_adapter3 adapter;
 TextView f,msg;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_notice);
    list = (ListView) findViewById(R.id.list_notice2);
    msg=(TextView) findViewById(R.id.err_msg);
    new test_ays().execute();
}




class test_ays extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        String str = null ;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/BSDI/show.php");

            HttpResponse response = httpclient.execute(httppost);
            str = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();



        }
        return str;


    }



@Override
protected void onPostExecute(String result) {

    super.onPostExecute(result);
    if(result!=null) // add this
    {  
     String response = result.toString();


   try {

        JSONArray new_array = new JSONArray(response);

        for (int i = 0, count = new_array.length(); i < count; i++) {
            try {
                JSONObject jsonObject = new_array.getJSONObject(i);
                title_array.add(jsonObject.getString("title").toString());
                notice_array.add(jsonObject.getString("notice").toString());

            } catch (JSONException e) {
                e.printStackTrace();

            }
        }

        adapter = new base_adapter3(NoticeBoard.this, title_array, notice_array);
      list.setAdapter(adapter);
       // f=(TextView) findViewById(R.id.textTuh);
       // f.setText(title_array);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
       e.printStackTrace();

        // tv.setText("error2");
    }
    }

    else{
        msg.setText("You need a working data connection...");
    }


}
}

}

And its my custom baseadapter codes,

public class base_adapter2 extends BaseAdapter {


    private Activity activity;
    //private ArrayList&lt;HashMap&lt;String, String&gt;&gt; data;
    private static ArrayList title,notice;
    private static LayoutInflater inflater = null;

    public base_adapter2(Activity a, ArrayList b) {
      activity = a;
      this.title = b;
     // this.notice=bod;

      inflater = (LayoutInflater) activity
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
      return title.size();
    }

    public Object getItem(int position) {
      return position;
    }

    public long getItemId(int position) {
      return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
      View vi = convertView;
      if (convertView == null)
          vi = inflater.inflate(R.layout.bsdi_adapter, null);


      TextView title2 = (TextView) vi.findViewById(R.id.txt1); // title
      String song = title.get(position).toString();
      title2.setText(song);



    return vi;

    }

}

In this example, I am connecting to Twitters public timeline JSON url.

package net.inchoo.demo.andy1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

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

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class HomeActivity extends ListActivity {

    /** Called when the activity is first created. */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, this.fetchTwitterPublicTimeline()));        
    }

    public ArrayList<String> fetchTwitterPublicTimeline()
    {
        ArrayList<String> listItems = new ArrayList<String>();

        try {
            URL twitter = new URL(
                    "http://twitter.com/statuses/public_timeline.json");
            URLConnection tc = twitter.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    tc.getInputStream()));

            String line;
            while ((line = in.readLine()) != null) {
                JSONArray ja = new JSONArray(line);

                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    listItems.add(jo.getString("text"));
                }
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return listItems;
    }
}

Please direct your attention on the listItems.add(jo.getString(“text”)); line. This is the part that I am grabbing a “text” attribute/property of single JSON object. To get a more “visual” picture of all available attributes/properties you might want to take a look at the XML version of twitters public timeline. This way you will get nice colored XML in your browser, where you can see all available attributes.

Link: http://inchoo.net/dev-talk/android-development/simple-android-json-parsing-example-with-output-into-listactivity/