作业三之FileSelector,jxl&&AddContacts(Android)

        上个周末写的创新班作业,主要可复用地方有FileSelector的实现,通过重写返回键事件来响应用户返回上一级的命令,而且储存了用户上一级目录的selected position,更加人性化,有兴趣的朋友可以看看,另外就是jxl库的调用,还有插入联系人,多线程(伪)更新UI等,更新ui这里以前一直以为handler是用于多线程的,但前晚才发现还有Thread这个类,果然多线程不可能没有互斥体,原子操作等这些东西,有兴趣修改的朋友修改下用Thread和handler来实现真多线程吧...(源码下载在最后)


        其中主要的fileselector代码放出:

package com.fp.homework3;


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

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.KeyEvent;

import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class FileSelectAcitvity extends Activity{
	public ListView fileslistview;
	public TextView nowpathtextview;
	
	private String rootPath;
	private class FileData{
		public String name;
		public String path;
		public int type;	//文件类型:0.目录   1.普通文件   2.XLS文件
		public FileData(String _name,String _path,int _type){
			name = _name; path = _path; type = _type;
		}
	};
	private List<FileData> items;
	
	private List<Integer> list_fatherPathPositions;
	public boolean doexit;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fileselect);
        rootPath = getString(R.string.rootpath);
        fileslistview = (ListView)findViewById(R.id.listView1);
        nowpathtextview = (TextView)findViewById(R.id.textView1);
        items = new ArrayList<FileData>();
        list_fatherPathPositions = new ArrayList<Integer>();
        
        listFiles(rootPath);
        Toast.makeText(this,"请选择要导入的excel文档.",Toast.LENGTH_SHORT).show();
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 如果有上一级目录则返回上一级目录,否则调用父类默认函数(退出程序)
		if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
			if(items.get(0).name.toString().equals("..")){
				listFiles(items.get(0).path.toString());
				int lastitem = list_fatherPathPositions.size()-1;
            	fileslistview.setSelection(list_fatherPathPositions.get(lastitem));
            	list_fatherPathPositions.remove(lastitem);
				return false;
			}
			
			new AlertDialog.Builder(this)
	        .setIcon(R.drawable.exit)
	        .setTitle("提示")
	        .setMessage("你确定要退出本程序吗?")
	        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	            	finish();
	            }
	        })
	        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	            }
	        })
	        .create().show();
			
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_fileselect, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	finish();
        return false;
    }
	
	public void openExcel(String filepath) {
		Bundle bd = new Bundle();
        bd.putString("path", filepath);
        
        Intent intent = new Intent(FileSelectAcitvity.this, MainActivity.class);
        intent.putExtra("data", bd);
		startActivity(intent);
	}

	public void listFiles(String filePath){
		items.clear();
		
		File f=new File(filePath);
		nowpathtextview.setText(filePath);
        if(f.exists() && f.canRead()){
            File[] files=f.listFiles();
            
            if((!filePath.equals(rootPath))&&(!filePath.equals("/"))) {
                items.add(new FileData("..",f.getParent(),0));
            }
            
            List<FileData> tmp_items = new ArrayList<FileData>();
            for(int i=0;i<files.length;i++){
                File file=files[i];
                String name = file.getName(), path = file.getPath();
                if(file.isDirectory()){
                    items.add(new FileData(name,path,0));
                }
                else{
                	if( !name.substring(name.length()-4,name.length()).equals(".xls") )
                		tmp_items.add(new FileData(name,path,1));
                	else
                		tmp_items.add(new FileData(name,path,2));
                }
            }
            items.addAll(tmp_items);

            fileslistview.setAdapter(new FileRowAdapter());
            
            fileslistview.setOnItemClickListener(new OnItemClickListener(){
                @Override  
                public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                	FileData fd = items.get(position);
                    if(fd.name.toString().equals("..")){ 
                    	//打开父目录,并返回储存入链表中的前一次打开目录前ListView中第一个item的位置
                    	listFiles(fd.path.toString()); 
                    	int lastitem = list_fatherPathPositions.size()-1;
                    	fileslistview.setSelection(list_fatherPathPositions.get(lastitem));
                    	list_fatherPathPositions.remove(lastitem);
                    }else{  
                        File file=new File(fd.path.toString());
                        if(file.canRead()){  
                            if (file.isDirectory()){
                            	//将当前ListView中第一个item的位置保存,以便返回父目录时可以定位到之前的地方
                            	list_fatherPathPositions.add(fileslistview.getFirstVisiblePosition());
                            	listFiles(fd.path.toString());
                            }
                            else{
                            	if(fd.type==2)
                            		openExcel(fd.path.toString());
                            }
                        }else{
                        	Toast.makeText(FileSelectAcitvity.this, "此文件不可读",Toast.LENGTH_SHORT).show(); 
                        }  
                    }  
                }  
            });
        }
	}
	
	//自定义配适器
    public class FileRowAdapter extends BaseAdapter {
    	public FileRowAdapter() {}
    	
        public int getCount() {
            return items.size();
        }

        public Object getItem(int position) {
            return items.get(position);
        }

        public long getItemId(int position) {
            return position;
        }
        
        public View getView(int position, View convertView, ViewGroup parent) {
        	ViewHolder holder;
        	FileData fd = items.get(position);
        	
        	if(convertView==null) {  
                holder = new ViewHolder();
                holder.icon = new ImageView(FileSelectAcitvity.this);
                holder.text = new TextView(FileSelectAcitvity.this);
                holder.text.setTextColor(0xFFFFFFFF);
                
                LinearLayout ll = new LinearLayout(FileSelectAcitvity.this);
        		ll.setOrientation(LinearLayout.HORIZONTAL);
        		ll.setVerticalGravity(Gravity.CENTER);

        		ll.addView(holder.icon);
        		ll.addView(holder.text);
        		ll.setTag(holder);
        		convertView = ll;
        		
            }else {
                holder = (ViewHolder) convertView.getTag();  
            }
        	
            holder.text.setText(fd.name);
            
            if(fd.type==0)
            	holder.icon.setImageResource(R.drawable.folder);
            else if(fd.type==1)
            	holder.icon.setImageResource(R.drawable.default2);
            else
            	holder.icon.setImageResource(R.drawable.notes);
            return convertView;
        }
        
        public final class ViewHolder {  
        	public ImageView icon;
            public TextView text;
        }//*/
        
        View item[];
    }
}

 

代码下载:http://dl.vmall.com/c0bgzvn7ym

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值