How to Display Fetched JSON Data into Listview Using Baseadapter

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<HashMap<String, String>> 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");
}

}
}

}

How to display fetched json data into listView using ArrayAdapter

1.Create the Book List and then pass the list to the BaseAdapter.

    final ArrayList<Book> books=new ArrayList<>();
booksAdapter=new BooksAdapter(this,-1, books);
listView=(ListView)findViewById(R.id.listView);
listView.setAdapter(booksAdapter);

2.Change the base adapter constructor which should accept book list.

public BooksAdapter(Context context, int resource, ArrayList<Book> books) {
super(context, resource, books);
}

3.Once received the data from server call booksAdapter.notifyDataSetChanged()
Hope this will solve your problem.

getting JSON result in custom listview android

You should not call the jobj.fetchJSON() method in your BaseAdapter class as it is called for each item in the list. This will make your application too slow.

First of all, you need to provide all the data to the BaseAdapter class. A very used practice is by providing this data in its constructor. So, your Base Adapter class constructor and the getView method should look like this:

public class adapterforlist extends BaseAdapter{

...
private List<Feed> feeds;
public adapterforlist(Context c, List<Feed> feedsList) {
this.context=c;
this.feeds = feedsList;
}

@Override
public View getView(int position, View rootview, ViewGroup viewgrp) {

LayoutInflater ll = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

rootview=ll.inflate(R.layout.custom, null, true);
tv1=(TextView)rootview.findViewById(R.id.textView1);
tv2=(TextView)rootview.findViewById(R.id.textView2);

// Create each item of the listView with every item of the data provided via constructor.
Feed feed = feeds.get(position);
tv1.setText(feed.getname());
tv2.setText(feed.getemail());

return rootview;
}
...
}

Now, for being able to do this you'll need the following:

  1. Create a class to represent the Feed. It can be a simple POJO (Plain Old Java Object).
  2. Fill a list of Feed objects and inject it to the BaseAdapter via contructor.

Your Feed POJO should look like this:

public class Feed {

private String name;
private String email;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

}

And your list of Feed objects can be created in this way;

public List<Feed> readandparseJSON (String in) {

List<Feed> feeds = new ArrayList<Feed>();

try {
JSONObject reader = new JSONObject(in);
JSONArray feed = reader.getJSONArray("feed");
JSONObject reader1= feed.getJSONObject(feed.length());

for (int i=0; i<=reader1.length();i++)
{
Feed feed = new Feed();
feed.setName(reader1.getString("name"));
feed.setUrl(reader1.getString("url"));
feeds.add(feed);
}

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

return feeds;

}

Please, let me know if you have more questions.

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<HashMap<String, String>> 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");
}

}
}

}


Related Topics



Leave a reply



Submit