Tuesday 28 May 2013

ListViewExample

1) VideoSubCategory
==================================
package com.ovte.sadbhavna;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class VideoSubCategory extends Activity {

    private VideoSubCategoryAdapter mSubCategoryVideoAdapter;
    private ListView lstSubCategory;
    private    SOAPService mSoapService = null;
    private ArrayList<String> alstSubCategoryID=null;
    private ArrayList<String> alstSubCategoryName = null;
    private ArrayList<String> alstDownLoadLink = null;
    private ArrayList<String> alstSubCategoryTitle =null;
    private TextView txt_heading;
    private String strVideoCategoryID=null,strVideoCategoryName=null,strUserID=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xsubcategory);
        GetData();
    }
   
//***************************************************************************************************************************
//This function is used for give memory   
    public void GetData() {
        //For get User ID
        SharedPreferences spGetUSERID = getSharedPreferences(Constant.SHAREDPREFERENCES, MODE_WORLD_READABLE);
        strUserID = spGetUSERID.getString(Constant.USER_ID, "");
            
        //For get category id & category name
        Bundle bundle = getIntent().getExtras();
        strVideoCategoryID=bundle.getString(Constant.KEY_VIDEO_SUBCATEGORY_ID.toString());
        strVideoCategoryName = bundle.getString(Constant.KEY_VIDEO_SUBCATEGORY_NAME.toString());
       
        alstSubCategoryID = new ArrayList<String>();
        alstSubCategoryName = new ArrayList<String>();
        alstDownLoadLink = new ArrayList<String>();
        alstSubCategoryTitle = new ArrayList<String>();
        txt_heading = (TextView)findViewById(R.id.txt_heading);
        txt_heading.setText(strVideoCategoryName);
       
        lstSubCategory =(ListView)findViewById(R.id.lstsubcategory);   
         if(GlobalClass.CheckNetwork(this)){
             new getSubCategoryData().execute("SubcategoryList");
         }
    }
   
//**************************************************************************************************************************
//This Function is used for get subCategory Video data
    private ProcessDialog mProcessDialog = null;
    public class getSubCategoryData extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            mProcessDialog = new ProcessDialog(VideoSubCategory.this, "Get Subcategory video", Constant.LOADING);
            super.onPreExecute();
        }
       
        @Override
        protected String doInBackground(String... params) {
            String SOAP_URL         =     getResources().getString(R.string.Webservice_URL);
            String SOAP_ACTION         =    getResources().getString(R.string.SOAP_Action);
            String SOAP_NAMESPACE     =    getResources().getString(R.string.SOAP_NAMESPACE);
            String SOAP_METHOD        =    "SearchVideoByCategory";
           
            mSoapService = new SOAPService(SOAP_URL, SOAP_ACTION, SOAP_NAMESPACE, SOAP_METHOD);
           
            String communityId=GetCommunity();
           
            String keyName[] = {"user_id","commnunity_id","category_id"};       
           
            Object keyValue[]  = {strUserID.toString(),communityId,strVideoCategoryID.toString() };
            return mSoapService.getSoapData(keyName,keyValue);       
        }
       
        @Override
        protected void onPostExecute(String result) {
            mProcessDialog.dismiss();
            if(result != null){
                String strSuccess,strMessage;
                try {
                        JSONObject json_subCategory = new JSONObject(result);                                   
                        if(json_subCategory != null)
                        {
                            strSuccess = json_subCategory.getString("SUCCESS").toString();
                            if(strSuccess.equals("OK")){
                                JSONArray json_quoteList = json_subCategory.getJSONArray("VIDEO");                       
                                if(json_quoteList !=null){
                                    for(int i=0;i<json_quoteList.length();i++){
                                        JSONObject job = json_quoteList.getJSONObject(i);   
                                        System.out.println("VIDEO=="+job);
                                        alstSubCategoryID.add(job.getString("ID").toString());
                                        alstSubCategoryTitle.add(job.getString("TITLE").toString());
                                        alstSubCategoryName.add(job.getString("MUSICFILE").toString());
                                        alstDownLoadLink.add(job.getString("DOWNLOAD_LINK").toString());                                               
                                    }       
                                    mSubCategoryVideoAdapter = new VideoSubCategoryAdapter(VideoSubCategory.this,alstSubCategoryID,alstSubCategoryTitle,alstSubCategoryName,alstDownLoadLink);
                                    lstSubCategory.setAdapter(mSubCategoryVideoAdapter);   
                                }   
                            }
                            else{
                                strMessage = json_subCategory.getString("MESSAGE").toString();
                                Toast.makeText(VideoSubCategory.this, Html.fromHtml(strMessage), Toast.LENGTH_LONG).show();
                            }
                        }                           
                       
                } catch ( Exception e ) {
                    e.printStackTrace();
                }
            }
            else{
                Toast.makeText(VideoSubCategory.this, Html.fromHtml(Constant.SERVERCONNERROR),Toast.LENGTH_SHORT).show();
            }
                super.onPostExecute(result);
        }               
    }
   
//**************************************************************************************************************************
    private String GetCommunity(){
       
        String communityId="";
        OpenDatabase();
        Cursor c = mDbHelper.GetSelectedCommunityList(mSQLiteDatabase, 1);
        if(c!=null){
            if(c.getCount() > 0){
                c.moveToFirst();
                int i=0;
                while(c.isAfterLast() == false){
                    if(i==0){
                        communityId="" +c.getInt(0);
                    }
                    else{
                        communityId=communityId+","+c.getInt(0);
                    }
                   
                    if(i==c.getCount()-1){
                        communityId=communityId + "";
                    }
                    i++;
                    c.moveToNext();
                };
            }
            c.close();
        }   
        CloseDataBase();
        return communityId;
    }
   
//***********************************************************************************************************************************
    //For open & close database
    private DbHelper mDbHelper=null;
    private SQLiteDatabase mSQLiteDatabase=null;
    private void OpenDatabase()
    {   
        mDbHelper = new DbHelper(this);
        mSQLiteDatabase = mDbHelper.getWritableDatabase();
        mSQLiteDatabase.setLockingEnabled(true);
    }
    private void CloseDataBase()
    {
        if(mDbHelper!=null && mSQLiteDatabase!=null){
            if(mSQLiteDatabase.isOpen()){
                mSQLiteDatabase.close();
                mDbHelper.close();
            }
        }
    }   
}

2)VideoSubCategoryAdapter.java
=========================
package com.ovte.sadbhavna;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class VideoSubCategoryAdapter extends BaseAdapter{

    private LayoutInflater mLayoutInflator;
    private ArrayList<String> alstVideoSubCategoryID=null;
    private ArrayList<String> alstVideoSubcategoryTitle =null;
    private ArrayList<String> alstVideoSubCategoryName=null;
    private ArrayList<String> alstVideoSubCategoryDownLoadLink=null;
    public Context mContext;
    private int mPosition=-1;

    public VideoSubCategoryAdapter(Context mContext,ArrayList<String> alstVideoSubCategoryID,ArrayList<String> alstVideoSubcategoryTitle,ArrayList<String> alstVideoSubCategoryName,ArrayList<String> alstVideoSubCategoryDownLoadLink){   
        this.mContext          = mContext;
        this.mLayoutInflator = LayoutInflater.from(mContext);
        this.alstVideoSubCategoryID    = alstVideoSubCategoryID;
        this.alstVideoSubcategoryTitle = alstVideoSubcategoryTitle;
        this.alstVideoSubCategoryName  = alstVideoSubCategoryName;
        this.alstVideoSubCategoryDownLoadLink = alstVideoSubCategoryDownLoadLink;
    }
   
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return alstVideoSubCategoryName.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
       
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(convertView == null){
            convertView = mLayoutInflator.inflate(R.layout.xsubcategoryadapter, null);
        }
        ImageView imgDownload = (ImageView)convertView.findViewById(R.id.imgDownload);
        imgDownload.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub           
                //DownloadMusic(strFileName,pos);
                boolean DownloadStatus=false;
                OpenDatabase();
                  DownloadStatus=mDbHelper.SearchDuplicateDownloadVideo(mSQLiteDatabase, alstVideoSubCategoryID.get(position).toString());
                CloseDataBase();
               
                if(DownloadStatus){
                    Toast.makeText(mContext, Html.fromHtml(Constant.RECORD_DUPLICATE_MESSAGE),Toast.LENGTH_SHORT).show();
                }
                else{
                    strFileName = alstVideoSubCategoryName.get(position).toString();
                    mPosition=position;
                    DownloadVideo();
                }
            }
           
        });
       
        ImageView imgPlay = (ImageView)convertView.findViewById(R.id.imgPlay);
        imgPlay.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent();
                intent.setAction(android.content.Intent.ACTION_VIEW);
//                File file = new File("/mnt/sdcard/Song/preview.mp3");              
                intent.setDataAndType(Uri.parse(alstVideoSubCategoryDownLoadLink.get(position).toString()), "video/*");
                mContext.startActivity(intent);
            }
        });
   
        TextView txtSubCategoryName = (TextView)convertView.findViewById(R.id.txtSubCategoryName);
        txtSubCategoryName.setText(Html.fromHtml(alstVideoSubcategoryTitle.get(position).toString()));
        return convertView;
    }
   
//******************************************************************************************************************
//    Alert dialog for download
    private void DownloadVideo() {
        AlertDialog mAlertDialog = new AlertDialog.Builder(mContext).create();
        mAlertDialog.setTitle(Constant.DOWNLOAD);
        mAlertDialog.setIcon(R.drawable.btn_download);
        mAlertDialog.setMessage(Constant.DOWNLOAD_MASSAGE);
        mAlertDialog.setCancelable(false);
        mAlertDialog.setButton(Constant.DELETE_DIALOG_BUTTON_YES, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {   
                //pos=position;
       
                if(GlobalClass.CheckNetwork(mContext))           
                   
                    new DownloadVideo().execute(alstVideoSubCategoryDownLoadLink.get(mPosition).toString());
                }
            });
        mAlertDialog.setButton2(Constant.DELETE_DIALOG_BUTTON_NO, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        // Create and show the dialog
        mAlertDialog.show();
    }
   
//******************************************************************************************************************************
//For Download Video file
    private String strFileName;
    private ProcessDialog mProcessDialog=null;
    private class DownloadVideo extends AsyncTask<String,Boolean, Boolean>{       
    protected void onPreExecute() {
               mProcessDialog=new ProcessDialog(mContext, "", "Downloading...");
               super.onPreExecute();
           }
           protected Boolean doInBackground(String... MP3URL) {
           boolean Status=false;
           if(GlobalClass.CheckNetworkNoMessage(mContext))
           {
               if(GlobalClass.MemoryCardCheck())
               {       
                    try{
                        File VideoDirectory = new File(Constant.SDCARDMUSICURL);
                           if(!VideoDirectory.exists()){
                               VideoDirectory.mkdir();
                           }                                                                   
                            InputStream mInputStream=GlobalClass.OpenHttpConnectionPost(MP3URL[0]);                                                                                                                    
                            FileOutputStream fos = new FileOutputStream(VideoDirectory + "/" + strFileName);
                                   byte data[] = new byte[1024];
  
                                    int count = 0;
                                 
                                    int progress = mInputStream.available()/100;
  
                                    while ((count=mInputStream.read(data)) != -1)
                                    {
                                        if(progress <= mInputStream.available()){
                                            progress += progress;
                                               
                                        }
                                        fos.write(data, 0, count);
                                    }
                                    fos.close();
                                    mInputStream.close();
                                                                                                  
                                        Status=true;
                        }
                        catch (Exception e) {
                               e.printStackTrace();
                                Status=false;
                        }
                    }
               }
               else{
                      Status=false;
               }
               return Status;
           }
          
           protected void onPostExecute(Boolean result) {
                    mProcessDialog.dismiss();
                    if(result){
                            Toast.makeText(mContext,Html.fromHtml(Constant.DOWNLOAD), Toast.LENGTH_SHORT).show();
                            OpenDatabase();                                     
                                mDbHelper.InsertDownloadVideo(mSQLiteDatabase, alstVideoSubCategoryID.get(mPosition).toString(), alstVideoSubcategoryTitle.get(mPosition).toString(), alstVideoSubCategoryName.get(mPosition).toString());                                        
                            CloseDataBase();  
                            notifyDataSetChanged();
                    }
                    else{
                            Toast.makeText(mContext,Html.fromHtml(Constant.NOT_DOWNLOAD), Toast.LENGTH_SHORT).show();
                    }        
                   super.onPostExecute(result);                       
           }     
     }
   
//*******************************************************************************************************************************
//For open & close database
    private DbHelper mDbHelper;
    private SQLiteDatabase mSQLiteDatabase;   
    private void OpenDatabase()
    {   
            mDbHelper = new DbHelper(mContext);
            mSQLiteDatabase = mDbHelper.getWritableDatabase();
            mSQLiteDatabase.setLockingEnabled(true);
    }
    private void CloseDataBase()
    {
        mSQLiteDatabase.close();
        mDbHelper.close();
    }   
}
3)xsubcategory.xml
====================================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:gravity="center_vertical"
             android:orientation="horizontal"
             android:background="@drawable/imgtop_tab">          
            <TextView
                android:id="@+id/txt_heading"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="@color/font_color"
                android:textSize="22dip"
                android:textStyle="bold"/>      
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@drawable/imgapp_bg">
        <ListView
            android:id="@+id/lstsubcategory"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="@android:color/transparent"/>
    </LinearLayout>
</LinearLayout>
4) xsubcategoryadapter.xml
===================== =================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip"
        android:layout_marginTop="10dip"
        android:layout_marginBottom="10dip"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/imgPlay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dip"
            android:src="@drawable/btn_play"/>
        <TextView
            android:id="@+id/txtSubCategoryName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:textColor = "@color/Black"
            android:text="Bhajan List"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:textSize="18dip"/>
          <ImageView
            android:id="@+id/imgDownload"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:src="@drawable/btn_download"/>
        <!-- <Button
            android:id="@+id/btnDownload"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:text="@string/btn_Download"/> -->
   
     <!--    <Spinner
            android:id="@+id/spnSubCategory"
            android:layout_width="fill_parent"
            android:layout_height="35dip"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_weight="1.5"
            android:background="@drawable/spinner"/> -->
    </LinearLayout>
</LinearLayout>


   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   

No comments:

Post a Comment