Saturday 9 November 2013

Create folder's in sdcard and get folder's list and display in listview example

1) create class ViewFoldersActivity.java

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.rws.ezyparkingalaram.R;
import com.rws.ezyparkingalaram.TopBarActivity;
import com.rws.viewgrid.ViewGridImagesActivity;

public class ViewFoldersActivity extends TopBarActivity implements
OnItemClickListener, OnItemSelectedListener {
private ImageView img_btn_back;
private ListView mFolderList;
private List fileNameList;
private FolderAdapter aadpFolderAdapter;
private File file;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_list_folders);

img_btn_back = (ImageView) findViewById(R.id.img_btn_bck);
img_btn_back.setOnClickListener(this);

// img_btn_ok = (ImageView) findViewById(R.id.img_btn_ok);
// img_btn_ok.setOnClickListener(this);

mFolderList = (ListView) findViewById(R.id.mnth_n_year_listview);
// this.getExternalFilesDir(null);
file = new File(Environment.getExternalStorageDirectory(),
"ParkingAlarm");
fileNameList = getFileListfromSDCard();
aadpFolderAdapter = new FolderAdapter(this, fileNameList);
mFolderList.setAdapter(aadpFolderAdapter);
mFolderList.setOnItemSelectedListener(this);
mFolderList.setOnItemClickListener(this);
}

private List getFileListfromSDCard() {
String state = Environment.getExternalStorageState();
List flLst = new ArrayList();
if (Environment.MEDIA_MOUNTED.equals(state) && file.isDirectory()) {
File[] fileArr = file.listFiles();
int length = fileArr.length;
for (int i = 0; i < length; i++) {
File f = fileArr[i];
flLst.add(f.getName());
System.out.println("flLst==" + flLst.toString());
}
}
return flLst;
}

@Override
public void onClick(View v) {
switch (v.getId()) {

case R.id.img_btn_bck:
finish();
break;

default:
break;
}
}

@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
//Toast.makeText(this, "Position==" + arg2, Toast.LENGTH_LONG).show();
}

@Override
public void onItemSelected(AdapterView arg0, View arg1, int position,
long arg3) {
// Toast.makeText(this, "Position==" + position, Toast.LENGTH_LONG).show();
Intent minIntent = new Intent(this, ViewGridImagesActivity.class);
startActivity(minIntent);
}

@Override
public void onNothingSelected(AdapterView arg0) {

}
}
2)create class FolderAdapter.java
=======================

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rws.ezyparkingalaram.R;
import com.rws.viewgrid.ViewGridImagesActivity;

public class FolderAdapter extends BaseAdapter {

private LayoutInflater mLayoutInflator;
private List lstFolderList;
private Context mContext;
// private String SHAREDPREFERENCE = "ParkingAlarm";
public FolderAdapter(Context mContext,ListlstFolderList){    
       this.mContext          = mContext;
       this.mLayoutInflator = LayoutInflater.from(mContext);
       this.lstFolderList    = lstFolderList;
   }

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

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

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

public class ViewHolder {
TextView txtFolderName;
LinearLayout lytFolderView;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

ViewHolder mHolder;
if (convertView == null) {
convertView = mLayoutInflator.inflate(R.layout.list_cell_folder_date, null);
mHolder = new ViewHolder();
mHolder.txtFolderName = (TextView) convertView.findViewById(R.id.txt_mtn_year);
mHolder.lytFolderView = (LinearLayout)convertView.findViewById(R.id.lytFolderView);
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.txtFolderName.setText("  " + lstFolderList.get(position));
mHolder.lytFolderView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent(mContext,ViewGridImagesActivity.class);
mIntent.putExtra("month_name", lstFolderList.get(position));
mContext.startActivity(mIntent);
}
});
return convertView;
}
}
3)list_cell_folder_date.xml
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/lytFolderView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dra_cell"
    android:gravity="center"
    android:layout_marginBottom="@dimen/main_lyt_margin_top_bottom"
    android:layout_marginTop="@dimen/main_lyt_margin_top_bottom"
    android:layout_marginLeft="@dimen/main_lyt_margin_left_right"
    android:layout_marginRight="@dimen/main_lyt_margin_left_right">

   
        android:id="@+id/txt_mtn_year"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dip"
        android:paddingRight="20dip"
        android:gravity="center|left"
        android:layout_gravity="center"
        android:drawableLeft="@drawable/month_icon"
        android:drawableRight="@drawable/arrow"
        android:text="@string/january_2013"
        android:textColor="@android:color/black"
        android:textSize="@dimen/medium_text" />

4)view_list_folders.xml
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg" >

   
        android:id="@+id/action_settings"
        android:layout_marginBottom="10dp"
        layout="@layout/topbar_share" />

   
        android:id="@+id/btton_line"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:drawablePadding="10dip"
        android:layout_marginBottom="10dip"
        android:drawableLeft="@drawable/bell_icon"
        android:text="@string/parking_permit" />

   
        android:id="@+id/midlle_contect"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/btton_line"
        android:layout_below="@id/action_settings"
        android:text="@string/app_name" >

       
            android:id="@+id/txt_parking_mnths"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="@string/parking_months"
            android:textSize="@dimen/title_text_size"
            android:textStyle="bold" />

       
            android:id="@+id/layout_back"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            layout="@layout/back_button" />

       
            android:id="@+id/mnth_n_year_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/layout_back"
            android:layout_below="@id/txt_parking_mnths"
            android:layout_margin="20dp"
            android:cacheColorHint="@android:color/transparent"
            android:divider="@null"
            android:dividerHeight="10dp"
            android:listSelector="@android:color/transparent" >
       
   

No comments:

Post a Comment