Wednesday, January 18, 2012

custom girdview on top and custom listview below which iterates itself.

package com.inception.smui;
/**
 * Class Name : ProfilesList
 *
 * Parent Class :Activity
 *
 * Interfaces: None
 *
 * Description:It will display the profiles related each and every sub-category
 */
import java.util.ArrayList;
import java.util.List;

import com.ServiceMessenger.R;
import com.inception.dataparser.Category;
import com.inception.dataparser.Service1;
import com.inception.dataparser.ServiceProvider;
import com.inception.dataparser.UserComments;
import com.inception.dataprovider.DatabaseHelper;
import com.inception.sm.ApplicationManager;
import com.inception.sm.Globals;
import com.inception.sm.MyAppContext;


import android.R.color;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class SubCategoryMenu extends BaseClass{
    String TAG="SubCategoryMenu";

    private DatabaseHelper mDBHelper;
    private static final int INDETERMINATE = 0;
    private ProgressDialog mProgDlg;
    private static ApplicationManager mAppMgr;
    TextView tv;

    private ArrayList<ServiceProvider> mProfileList = new ArrayList<ServiceProvider>();
    //private ArrayList<ServiceProvider> mProfileList = new ArrayList<ServiceProvider>();
    public ArrayList<String> mList = new ArrayList<String>();
    public ArrayList<String> mList1 = new ArrayList<String>();
    public ArrayList<String> mList2 = new ArrayList<String>();
    public List<String> mList3 = null;
    public List<String> mCatUrls=new ArrayList<String>();
    private List<Bitmap> mCatImg=new ArrayList<Bitmap>();
    private List<String> mProfileId = new ArrayList<String>();
    private ArrayList<String> mCatid = new ArrayList<String>();
    private ArrayList<Category> mSubCatList = new ArrayList<Category>();
    ArrayList<Service1> serviceList=null;
    ArrayList<String> mProfileIds=null;
    ArrayList<String> mProfileCities=null;
    ArrayList<String> mProfileCountries=null;
    ArrayList<String> mProfileRatings=null;
    ArrayList<String> mProfileDes=null;
    ArrayList<String> mProfileNames=null;
    ArrayList<String> mProfileOnlinestatus=null;
    ArrayList<String> mProfile_pic_url=null;
    ArrayList<UserComments> usercomments=null;
    ArrayList<Messenger> mClients = new ArrayList<Messenger>();
    private Context mContext;
    boolean mIsBound;
    private int mCurrentView;
    //widgets

    public ListView mListView;
    private Gallery mGallery;
    public Button mBtn1,mBtn2;
    private LayoutInflater mInflater;
    MyAppContext mAppctx;

    public static String mCategory_id;
    public static String mSubcat;
    private static CharSequence pid;
    //public static String mCat;
    public static int mView;
    public static CharSequence cat;
    private Category mCat = new Category();
    BaseClass b = new BaseClass();

    /**
     * Callback method to launch the Acitivity
     *
     */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDBHelper = new DatabaseHelper(this);
        mAppMgr = ApplicationManager.getApplicationMgrInstance(this.getApplicationContext());
        setContentView(R.layout.profile);
        mListView=(ListView)findViewById(R.id.lv_user1);
        mGallery = (Gallery) findViewById(R.id.gallery_catlog);
        mList3= new ArrayList<String>();
        mList3=mAppMgr.getCategories();
        mCatUrls=mAppMgr.getCategoriesUrls();
        Log.e(TAG,mCatUrls.get(0));
        mCatImg=mAppMgr.getCategoryImgs();
        mContext=this;
        mBtn1=(Button)findViewById(R.id.button_MyAcct1);
        mBtn2=(Button)findViewById(R.id.button_MySP1);
        /**
         *Button click listener for MyAccpunt button
         */
        mBtn1.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Log.e(TAG,"button My Account clicked");
                if(CategoryMenu.mStatus){
                    Log.e("am in","login status true" );
                    MyAccount.mCurrentView1=Globals.SENT_FROM_SPLIST;
                    Intent i=new Intent(SubCategoryMenu.this,MyAccount.class);
                    startActivity(i);
                }
                else
                {
                    LoginInfo.mCurrentView1=Globals.LOGIN_IN_MYACCOUNT;
                    Intent i=new Intent(SubCategoryMenu.this,LoginInfo.class);
                    startActivity(i);

                }
            }

        });


        /**
         *Button click listener for MySPs button
         */
        mBtn2.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Log.e(TAG,"button 1 clicked");
                if(CategoryMenu.mStatus){
                    Log.e("am in","login status true" );
                    Intent i=new Intent(mContext,MyAccount.class);
                    startActivity(i);
                }
                else
                {
                    LoginInfo.mCurrentView1=Globals.LOGIN_IN_MYACCOUNT;
                    Intent i=new Intent(mContext,LoginInfo.class);
                    startActivity(i);

                }
            }

        });
       
        mAppctx = (MyAppContext)getApplicationContext();
        mGallery.setAdapter(new ImageAdapter(mContext));
        //mGallery.setFadingEdgeLength(20); 
        Intent ip = getIntent();
        //int view= ip.getIntExtra("view", Globals.CONST_SUBCATEGORY);

        cat=(String) ip.getCharSequenceExtra("cat");
        Log.d("cat",""+cat);
        int view=(int) ip.getIntExtra("view", 0);
        Log.d("view",""+view);

        //if(view  == Globals.CONST_SUBCATEGORY)
        //{
        //mList = null;
        cat = ip.getCharSequenceExtra("cat");
        Log.d("subcatarory",""+cat);

        mCurrentView = Globals.CONST_SUBCATEGORY;

        mSubCatList=mAppMgr.getSubCategories(cat);
        /*if(mSubCatList.size()==0)
            {
                finish();
                //mCurrentView = Globals.CONST_PROFILE;
            }
            else{*/
        System.out.println(mSubCatList);

        Log.d("u r in ",cat.toString());
        mList2 = prepareStringList1();
        Log.d("mList2",""+mList2.size());

        mListView.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item3,R.id.subCatlog_text,mList2));
        //}

        //}








        //mSubcat = (String) ip.getCharSequenceExtra("subcat");
        //Log.e(TAG,mSubcat);
        //mList1= null;
        /*    SQLiteDatabase database = mDBHelper.getWritableDatabase();

        Cursor data = database.query("catalog2", new String[] {"category_id"},new String("category_name"+"=?"),new String[]{mSubcat.toString()}, null, null, null);
        if (data != null)
        {
            Log.d("database",database.toString());
            if (data.moveToFirst()){
                mCategory_id= data.getString(data.getColumnIndex("category_id"));


            }
        }*/

        /**
         *List item click listener for profiles
         */

        mListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Log.d("SM","position "+position);
                mCat = new Category();
                if(mCurrentView == Globals.CONST_SUBCATEGORY){



                    CharSequence category = mList2.get(position);
                    CharSequence catSelectId = mCatid.get(position);

                    Log.e("subcategory clicked",""+ catSelectId);

                    mCat=mAppMgr.getCategoryObjectFromId(catSelectId);
                    String isleaf = mCat.getIs_leaf();
                    String cat_id=mCat.getCategory_id();
                    String cat_name=mCat.getCategory_name();
                    //String parent_id=mCat.getParent_id();

                    Log.e("isleaf",isleaf);
                    if(isleaf.equals("false"))
                    {
                        Log.d("false executed","Thanks");
                        mSubCatList=mAppMgr.getSubCategories(cat_id);
                        System.out.println(mSubCatList);
                        mCurrentView = Globals.CONST_SUBCATEGORY;
                        mList2 = prepareStringList1();
                        mListView.setAdapter(new ArrayAdapter<String>(mContext,R.layout.list_item3,R.id.subCatlog_text,mList2));

                    }
                    else
                    {
                        Intent i=new Intent(SubCategoryMenu.this,ProfilesList.class);
                        Log.d("true executed","Thanks");
                        mAppctx.setCatId(cat_id);
                        mAppctx.setCatName(cat_name);
                        //i.putExtra("cat",cat_id);
                        //i.putExtra("view", Globals.CONST_PROFILE);
                        startActivity(i);

                    }
                    //Log.e("am in","sub categories");
                    //startActivity(i);


                }




            }
        });

        /**
         *Gallery item click listener for categories
         */

        mGallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                mCat = new Category();
                //v.setFocusable(true);
                //tv.setText("");
                CharSequence cat = mList3.get(position);
                Log.e("category clicked",""+ cat);
                CharSequence category = cat;
                mCat=mAppMgr.getCategoryObjectFromName(category);
                String isleaf = mCat.getIs_leaf();
                String parent_id=mCat.getParent_id();
                String cat_id=mCat.getCategory_id();
                Log.e("cat_id",cat_id);

                Log.e("isleaf",isleaf);
                //Log.e("parent_id",parent_id);
                if(isleaf.equals("false"))
                {
                    callsubcatlog(cat_id);
                }
                else
                {
                    Intent i=new Intent(mContext,ProfilesList.class);
                    Log.d("true executed","Thanks");

                    //i.putExtra("cat",cat_id);
                    mAppctx.setCatId(cat_id);

                    startActivity(i);

                }


            }
           
        });
       
       
    }



    public ArrayList<String> prepareStringList1()
    {

        int i =0;
        Log.e("am in","prepare string list");
        //    Log.e(TAG+"mList1.size()",""+mList1.size());
        mCatid=new ArrayList<String>();
        ArrayList<String> List2=new ArrayList<String>();
        //misleaf=new ArrayList<String>();
        for(;i< mSubCatList.size();i++){
            String profileStr = mSubCatList.get(i).category_id;
            String profileStr1 = mSubCatList.get(i).category_name;
            //String isleaf=mList1.get(i).is_leaf;

            Log.e("profileStr1",profileStr);
            mCatid.add(profileStr);

            List2.add(profileStr1);

            //misleaf.add(isleaf);



        }
        Log.e("TAG+mList1.size()",""+mSubCatList.size());


        return List2;
    }



    void callsubcatlog(String cat_id)
    {
        mSubCatList=mAppMgr.getSubCategories(cat_id);
        if(mSubCatList.size()==0)
        {
            finish();
            mCurrentView = Globals.CONST_PROFILE;
        }
        else{
            System.out.println(mSubCatList);

            Log.d("u r in ",cat.toString());
            mList2 = prepareStringList1();
            Log.d("mList2",""+mList2.size());

            mListView.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item3,R.id.subCatlog_text,mList2));
        }
    }

    /**
     * To set image in profile list
     */
    class IconicAdapter extends ArrayAdapter {
        IconicAdapter() {
            super(SubCategoryMenu.this, R.layout.list_item,R.id.text,mProfileNames);
        }

        public View getView(int position, View convertView,
                ViewGroup parent) {
            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.list_item, parent,    false);


            ImageView icon=(ImageView)row.findViewById(R.id.icon);
            TextView label=(TextView)row.findViewById(R.id.text);
            label.setText(mProfileNames.get(position)+","+mProfileCountries.get(position));

            /*    if (mList1.get(0).startsWith("abc")) {
                Log.e("am in","abc profile");
                icon.setImageResource(R.drawable.icon);
            }
            if (mList1.get(0).startsWith("xyz")) {
                icon.setImageResource(R.drawable.d1);
                Log.e("am in","xyz profile");
            }*/

            return(row);
        }
    }
    /**
     * To papulate images , data in catalog gallery
     */

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;
        ViewHolder holder;

        public ImageAdapter(Context c) {
            mContext = c;
            mInflater = LayoutInflater.from(c);

            TypedArray a = obtainStyledAttributes(R.styleable.Theme);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.Theme_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            Log.i(TAG+"i am taking images from local DB","");
            //mCatImg=mAppMgr.getCategoryImgs();
            return mCatUrls.size();
            //return mCatImg.size();
        }

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

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

        /**
         * To papulate each view of gallery with textview and imageview
         */

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
               
               
                //convertView.setLayoutParams(new Gallery.LayoutParams(100, 150));
                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);
                convertView.setTag(holder);
               
               
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            holder.text.setText(mList3.get(position));
            holder.icon.setImageBitmap(mCatImg.get(position));

            holder.icon.setBackgroundResource(mGalleryItemBackground);
            return convertView;


        }
       


    }
    /**
     * To Hold TextView and ImageView
     */

    static class ViewHolder {
        TextView text;
        ImageView icon;
    }


}



list_item_icon_text.xml layout for custom gallery


<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView android:id="@+id/icon" android:layout_width="100dip"
        android:layout_height="100dip" />

    <TextView android:id="@+id/text" android:layout_width="100dip"

        android:layout_height="wrap_content" />

</LinearLayout>

profile.xml

<?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="fill_parent"
    android:background="@drawable/bk_gnd">
    <TextView android:id="@+id/empty" android:layout_width="fill_parent"
        android:layout_height="15dip" />
    <TextView android:id="@+id/catlogs_gallery_text" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Categories"
        android:textColor="@color/menu_buttons" android:textSize="20sp"
        android:gravity="center" android:layout_below="@id/empty" />
    <TextView android:id="@+id/empty1" android:layout_width="fill_parent"
        android:layout_height="15dip" android:layout_below="@id/catlogs_gallery_text" />
    <Button android:id="@+id/button_MyAcct1" android:text="MyAccount"
        android:textSize="@dimen/menu_buttons_text_size" android:textColor="@color/menu_buttons"
        android:textStyle="bold" android:gravity="center"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" android:background="@drawable/button_menu" />

    <Button android:id="@+id/button_MySP1" android:text="MySPs"
        android:textSize="@dimen/menu_buttons_text_size" android:textColor="@color/menu_buttons"
        android:textStyle="bold" android:gravity="center"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"

        android:layout_alignParentRight="true" android:background="@drawable/button_menu" />
    <Gallery android:id="@+id/gallery_catlog" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_below="@id/catlogs_gallery_text" />
    <TextView android:id="@+id/empty2" android:layout_width="fill_parent"
        android:layout_height="15dip" android:layout_below="@id/gallery_catlog" />
    <TextView android:id="@+id/txt_subCat" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Sub Categories"
        android:textColor="@color/menu_buttons" android:textSize="20sp"
        android:gravity="center" android:layout_below="@id/empty2" />
    <TextView android:id="@+id/txt_selectsubCat" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Select any category to get subcategories"
        android:textColor="@color/menu_buttons" android:layout_below="@id/txt_subCat" />
    <ListView android:textSize="150sp" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/lv_user1"
        android:layout_below="@id/txt_selectsubCat" />

</RelativeLayout>

list_item3.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content" android:layout_height="wrap_content">

   
    <TextView android:id="@+id/subCatlog_text" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_toRightOf="@id/icon"
        android:padding="5dp" android:textSize="@dimen/menu_buttons_text_size"
        android:textColor="@color/menu_buttons" android:textStyle="bold"


        android:layout_margin="5dp" />

    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_centerVertical="true"
        android:id="@+id/iv_forward" android:src="@drawable/arrow1"

        android:layout_alignParentRight="true" android:layout_marginRight="10dip">
    </ImageView>
</RelativeLayout>

No comments:

Post a Comment