Thursday, August 15, 2013

Base Adapter With Json Data

Step1: You Just Follow Simple Step.

1.Create a new Activity in your Project. (MainActivity.java)


package com.valkesh.baseadapterdemo;

import java.util.ArrayList;

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

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.valkesh.adapter.FriendAdapter;
import com.valkesh.model.FriendDetailsListModel;
import com.valkesh.model.JSONParser;

public class MainActivity extends Activity {
private Context mContext;
private ListView FriendList;
private FriendAdapter friend_Adapter;
private String url = "";
private ArrayList<FriendDetailsListModel> mFriendsList = new ArrayList<FriendDetailsListModel>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FriendList = (ListView) findViewById(R.id.lv_friend_list);
url = "http://valkeshpatel.com/friend/f_id=1";
new ProgressTask().execute();
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}

class ProgressTask extends AsyncTask<String, String, String> {
private ProgressDialog dialog;
private String response = null;

@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(mContext);
dialog.setMessage("Loading Customer...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}

@Override
protected String doInBackground(String... params) {
JSONParser jParser = new JSONParser();
// get JSON data from URL
response = jParser.getResponseFromUrl(url);
return null;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// if (dialog.isShowing()) {
dialog.dismiss();
// }

if (response.equalsIgnoreCase("") || response.trim().length() <= 0) {
Toast.makeText(mContext, "No data found", Toast.LENGTH_SHORT)
.show();
} else {
try {
Log.d("Drive_List_response", "" + response);
JSONArray objJsonArry = new JSONArray(response);
for (int i = 0; i < objJsonArry.length(); i++) {
JSONObject objJosn = objJsonArry.getJSONObject(i);
FriendDetailsListModel objModel = new FriendDetailsListModel();
objModel.setF_name(objJosn.getString("name"));
objModel.setDob(objJosn.getString("age"));
objModel.setGender(objJosn.getString("gender"));
mFriendsList.add(objModel);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("ERROR", e.toString());
}
Log.e("LISTSIZE", "" + mFriendsList.size());
friend_Adapter = new FriendAdapter(mContext, mFriendsList);
FriendList.setAdapter(friend_Adapter);
}
}
}
}


2.Create a new Activity XML File, Layout Folder In Project. (activity_main.xml).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/common_bg"
    tools:context=".DisplayActivity" >

   <ListView
        android:id="@+id/lv_friend_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:cacheColorHint="#000000"
        android:divider="@null"
        android:dividerHeight="5dp" />

</RelativeLayout>

3.Create a new BaseAdapter File In Project. (FriendAdapter.java).

package com.valkesh.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.valkesh.baseadapterdemo.R;
import com.valkesh.model.FriendDetailsListModel;

public class FriendAdapter extends BaseAdapter implements
OnClickListener {


private Context mContext;
private ArrayList<FriendDetailsListModel> mFriendsListOriginal = new ArrayList<FriendDetailsListModel>();
private static LayoutInflater inflater = null;

public FriendAdapter(Context context,
ArrayList<FriendDetailsListModel> data) {
mContext = context;
mFriendsListOriginal = data;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return mFriendsListOriginal.size();
}

@Override
public Object getItem(int position) {
return mFriendsListOriginal.get(position);
}

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

static class ViewHolder {
TextView email_id, f_name, age, gender;
Button addHim;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;

LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.activity_display, null);
holder = new ViewHolder();
holder.f_name = (Button) convertView.findViewById(R.id.f_name);
holder.age = (Button) convertView.findViewById(R.id.age);
holder.gender = (Button) convertView.findViewById(R.id.gender);
holder.addHim = (Button) convertView.findViewById(R.id.trackhim);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
FriendDetailsListModel rowItem = (FriendDetailsListModel) getItem(position);

final String email_id_on = rowItem.getEmail_id();
final String name_on = rowItem.getEmail_id();

holder.f_name.setText(rowItem.getF_name());
holder.gender.setText(rowItem.getGender());
holder.age.setText(rowItem.getEmail_id());
holder.addHim.setOnClickListener(new  OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Intent intent = new Intent(mContext, XYZ.class);
// mContext.startActivity(intent);
// ((Activity) mContext).finish();
}
});
return convertView;
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}

}
4.Create a new BaseAdapter File, Layout Folder In Project. (activity_display.xml).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DisplayActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_alignParentLeft="true"
        android:background="@drawable/valkesh" />

    <Button
        android:id="@+id/f_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:background="@drawable/bginterest"
        />

    <Button
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/f_name"
        android:background="@drawable/bginterest"
        />

    <Button
        android:id="@+id/gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/age"
        android:background="@drawable/bginterest"
       />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/gender"
        android:layout_centerHorizontal="true" >

        <Button
            android:id="@+id/trackhim"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@drawable/button_bg"
            android:text="Like Me" />
    </LinearLayout>

</RelativeLayout>
5.Create a new Model Class (POGO) Project. (FriendDetailsListModel.java).

package com.valkesh.model;

public class FriendDetailsListModel {

private String friend_id;
private String email_id;
private String f_name;
private String dob;
private String gender;
/**
* @return the friend_id
*/
public String getFriend_id() {
return friend_id;
}
/**
* @param friend_id the friend_id to set
*/
public void setFriend_id(String friend_id) {
this.friend_id = friend_id;
}
/**
* @return the email_id
*/
public String getEmail_id() {
return email_id;
}
/**
* @param email_id the email_id to set
*/
public void setEmail_id(String email_id) {
this.email_id = email_id;
}
/**
* @return the f_name
*/
public String getF_name() {
return f_name;
}
/**
* @param f_name the f_name to set
*/
public void setF_name(String f_name) {
this.f_name = f_name;
}
/**
* @return the dob
*/
public String getDob() {
return dob;
}
/**
* @param dob the dob to set
*/
public void setDob(String dob) {
this.dob = dob;
}
/**
* @return the gender
*/
public String getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(String gender) {
this.gender = gender;
}
}