Displaying Data From Url Into Listview
Solution 1:
Your response is like this:
[
{
"name": "Ravi Tamada",
"email": "ravi8x@gmail.com",
"phone": {
"home": "08947 000000",
"mobile": "9999999999"
}
},
{
"name": "Tommy",
"email": "tommy@gmail.com",
"phone": {
"home": "08946 000000",
"mobile": "0000000000"
}
}
]
Here it is json array. You can parse it this way:
Create a java object class. Your class is like this:
Your Phone Entity is here:
import com.google.gson.annotations.SerializedName;
publicclassPhoneObject{
@SerializedName("home")public String home;
@SerializedName("mobile")public String mobile;
}
Your User object is it:
import com.google.gson.annotations.SerializedName;
publicclassUserObject{
@SerializedName("name")public String name;
@SerializedName("email")public String email;
@SerializedName("phone")public PhoneObject phone;
public UserObject(){
phone=new Phone();
}
}
Now in your onResponse
callback use this line of code:
TypelistType=newTypeToken<List<UserObject>>() {}.getType();
List<UserObject> yourList = newGson().fromJson(response.toString(), listType);
Now you have UserObject List in yourList
. Next stage is create a ListView and initialize it. Create an adapter , set it's item layout and invalidate data of it's item.
Suppose your ListView is like this:
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#555"
android:dividerHeight="1dp"
android:listSelector="#000" />
and your list item row like this:
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="#fff"android:padding="8dp" ><TextViewandroid:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:textStyle="bold" /></RelativeLayout>
Now Your Adapter will like this:
publicclassCustomListAdapterextendsBaseAdapter {
private Context activity;
private LayoutInflater inflater;
private List<UserObject> items;
publicCustomListAdapter(Context activity, List<UserObject> items) {
this.activity = activity;
this.items = items;
}
@OverridepublicintgetCount() {
return items.size();
}
@Overridepublic Object getItem(int location) {
return items.get(location);
}
@OverridepubliclonggetItemId(int position) {
return position;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
TextViewname= (TextView) convertView.findViewById(R.id.name);
// getting movie data for the rowUserObjectm= items.get(position);
name.setText(m.name);
return convertView;
}
}
Now you just call this way in onResponse()
callback
CustomListAdapteradapter=newCustomListAdapter(this, yourList);
listView.setAdapter(adapter);
Edit: Why Java Object
You have a json Array. It's individual object is like this:
{
"name": "Ravi Tamada",
"email": "ravi8x@gmail.com",
"phone": {
"home": "08947 000000",
"mobile": "9999999999"
}
}
Here, it is a json object, it contains string's and tag's are "name", 'email', and an Json Object tag "phone".
So here this object contains some string and a json object name "phone".
From previous discussion, I created a Java Object name PhoneObject
it's tag's are initialized using this way:
@SerializedName("home")public String home;
Now "phone" tag is in main Object. So i created UserObject
and initialized PhoneObject
in that object.
Note: You have to realize the json structure and after that you have to start creating the java object.
All are done. If need more information visit here
Solution 2:
You can use :
ArrayAdapter<String> itemsAdapter =
newArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
ListView listView = (ListView) findViewById(R.id.lvItems);
listView.setAdapter(itemsAdapter);
helpful link : https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
Solution 3:
You have to write some code for this.
- Parse JSON response and create object(you can use HashMap also).
- Create a layout file in xml for showing the items of list view.
- Create a custom adapter class and inflate the xml layout, get views and set the values to the views.
- set the adapter to your list view.
you can take help from below links:
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
http://www.vogella.com/tutorials/AndroidListView/article.html
Post a Comment for "Displaying Data From Url Into Listview"