1.例中数据是读取sdcard中的文件:
package myapplication.com.myappexample; import android.content.Context; import android.graphics.Bitmap; import android.os.Environment; import android.view.View; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2016/12/3. */ public class SDcardHelper { public static void save(String data, String name, Context context){ try { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录,2.2的时候为:/mnt/sdcart 2.1的时候为:/sdcard,所以使用静态方法得到路径会好一点。 File filedir = new File(sdCardDir + File.separator + "MyjizhanFile"); // 这里的为创建的文件夹,在根目录下 if (!filedir.exists()) { filedir.mkdirs(); } File saveFile = new File(filedir, name); // aaaa.txt保存到AA文件夹下 FileOutputStream outStream1 = new FileOutputStream(saveFile); System.out.println(outStream1.toString()); outStream1.write(data.getBytes()); outStream1.close(); Toast.makeText(context,"保存成功",Toast.LENGTH_SHORT).show(); } } catch (IOException e) { e.printStackTrace(); } } /*** * 查找目录下所有的文件,例子中用到的数据及是读取的文件名称,和绝对路径 * */ public static ListgetFileName(File[] files) { List data=new ArrayList<>(); String address=""; if (files != null)// 先判断目录是否为空,否则会报空指针 { for (File file : files) { if (file.isDirectory()) { getFileName(file.listFiles()); } else { String fileName = file.getName(); address=file.getAbsolutePath(); data.add(new WenjianBean(fileName,address)); } } } return data; } }
2.简单的数据info
package myapplication.com.myappexample; /** * Created by Administrator on 2016/12/4. */ public class WenjianBean { String name; String address; public String getName() { return name; } public String getAddress() { return address; } public WenjianBean(String name, String address) { this.name = name; this.address = address; } }
3.ListView的item布局,需要屏蔽item中的checkbox的点击事件,重写了RelativeLayout
xml version="1.0" encoding="utf-8"?> <myapplication.com.myappexample.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_marginRight="10dip" android:layout_weight="1.0" android:layout_marginLeft="10dp" android:orientation="horizontal" android:descendantFocusability="blocksDescendants" android:background="#AAAAAA" > <CheckBox android:id="@+id/isCheakBox" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:layout_alignParentRight="true" /> <ImageView android:id="@+id/dailyPic" android:contentDescription="dailyPic" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginTop="3.0dip" android:src="@mipmap/txt" android:layout_alignParentLeft="true" android:layout_centerVertical="true"/> <TextView android:id="@+id/dailyName" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_toRightOf="@+id/dailyPic" android:text="日报名称" android:gravity="center_vertical" android:layout_centerVertical="true" android:textColor="#000000" android:textSize="18.0sp" /> myapplication.com.myappexample.MyRelativeLayout>
4. RelativeLayout
package myapplication.com.myappexample; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.RelativeLayout; /** * Created by Administrator on 2017/1/16. */ public class MyRelativeLayout extends RelativeLayout { public MyRelativeLayout(Context context) { super(context); } public MyRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { /** * * 这里返回true,事件处理操作返回到上一级操作 * */ return true; } }
5.主布局
xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:background="@color/bai" tools:context="myapplication.com.myappexample.Rizhi_Activity"> <LinearLayout android:layout_height="45dp" android:layout_width="match_parent" android:id="@+id/layout1" android:gravity="center_horizontal|center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/textView_quxiao" android:layout_height="match_parent" android:layout_width="wrap_content" android:text="取消" android:layout_marginLeft="10dp" android:gravity="center_horizontal|center_vertical" android:textColor="@color/hei"/> <TextView android:id="@+id/textView_xuanze" android:layout_height="match_parent" android:layout_width="0dp" android:gravity="center_horizontal|center_vertical" android:layout_weight="1" android:text="已选择0项" android:textColor="@color/hei"/> <TextView android:id="@+id/all_check_btn" android:layout_width="wrap_content" android:text="全选" android:textColor="@color/red" android:gravity="center_vertical" android:layout_height="match_parent" android:layout_alignParentLeft="true"/> LinearLayout> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="30dp" android:textColor="#6e6e6e" android:gravity="center_vertical" android:text="11"/> <ListView android:layout_marginTop="3dp" android:id="@+id/listView" android:layout_height="0dp" android:layout_weight="1" android:layout_width="match_parent"/> <LinearLayout android:layout_height="45dp" android:layout_width="match_parent" android:id="@+id/layout3" android:gravity="center_horizontal|center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/rizhi_textView_shanchu" android:layout_height="match_parent" android:layout_width="0dp" android:gravity="center_horizontal|center_vertical" android:layout_weight="1" android:text="删除" android:textColor="@color/hei"/> LinearLayout> LinearLayout>
6.实现代码
package myapplication.com.myappexample; import android.bluetooth.BluetoothAdapter; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.net.Uri; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.SearchView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Rizhi_Activity extends AppCompatActivity{ private static final String TAG = "Rizhi_Activity"; TextView textView_quxiao,rizhi_textView_shanchu; TextView textView_xuanze; TextView text; private List- itemList; private DraftDailyAdapter adapter; private Map
isCheckedMap; private TextView allCheckBox; int count; int num_xuanze=0; ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rizhi); initView(); } public void initView(){ listView= (ListView) findViewById(R.id.listView); text= (TextView) findViewById(R.id.text); textView_xuanze= (TextView) findViewById(R.id.textView_xuanze); /** * 获取该路径下面的文件名称和路径名称 * */ File file1=new File(Environment.getExternalStorageDirectory()+File.separator+"MyjizhanFile"); File[] files = file1.listFiles(); final List data=SDcardHelper.getFileName(files); Log.i(TAG, "initView:size "+data.size()); textView_xuanze.setText("选中"+num_xuanze+"项"); count=data.size(); text.setText("查找到"+count+"个文件"); allCheckBox = (TextView) findViewById(R.id.all_check_btn); itemList = new ArrayList - (); final ListView list= (ListView) findViewById(R.id.listView); isCheckedMap = new HashMap
(); //初始化数据 for(int i=0;i ){ Item item = new Item(); item.id=i; item.name = data.get(i).getName(); item.address=data.get(i).getAddress(); item.check=false; itemList.add(item); } adapter = new DraftDailyAdapter(this,itemList); list.setAdapter(adapter); textView_quxiao= (TextView) findViewById(R.id.textView_quxiao); rizhi_textView_shanchu= (TextView) findViewById(R.id.rizhi_textView_shanchu); /*** * 文件删除 * **/ rizhi_textView_shanchu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ArrayList - delitem=new ArrayList
- (); for(int i=0;i
){ if(itemList.get(i).check){ delitem.add(itemList.get(i)); } } for(int i=0;i ){ itemList.remove(delitem.get(i)); delete(delitem.get(i).address); } adapter.notifyDataSetChanged(); num_xuanze=0; textView_xuanze.setText("已选中"+num_xuanze+"项"); text.setText("查找到"+count+"个文件"); } }); /** * * 取消 * */ textView_quxiao= (TextView) findViewById(R.id.textView_quxiao); textView_quxiao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for(int i=0;i ){ if(itemList.get(i).check==true){ itemList.get(i).check=false; } } allCheckBox.setTextColor(getResources().getColor(R.color.red)); num_xuanze=0; textView_xuanze.setText(num_xuanze+""); adapter.notifyDataSetChanged(); } }); /**** * 全选功能/ * */ allCheckBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i(TAG, "onClick: +data.size"+data.size()); for(int i=0;i ){ if(itemList.get(i).check==false){ itemList.get(i).check=true; } } allCheckBox.setTextColor(getResources().getColor(R.color.lv)); adapter.notifyDataSetChanged(); num_xuanze=itemList.size(); textView_xuanze.setText(num_xuanze+""); } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { CheckBox isCheakBox= (CheckBox) view.findViewById(R.id.isCheakBox); Item item= (Item) parent.getItemAtPosition(position); if(isCheakBox.isChecked()){ num_xuanze--; item.check=false; }else { num_xuanze++; item.check=true; } textView_xuanze.setText("已选中" + num_xuanze + "项"); adapter.notifyDataSetChanged(); } }); } /*** * adapter * **/ class DraftDailyAdapter extends BaseAdapter { public List - list; private Context context; LayoutInflater inflater; public DraftDailyAdapter(Context context, List
- list) { super(); this.list = list; this.context = context; inflater = LayoutInflater.from(this.context); } @Override public int getCount() { return list == null ? 0 : list.size(); } @Override public Object getItem(int location) { return list.get(location); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; final Item item = list.get(position); //Item的位置 final int listPosition = position; //这个记录item的id用于操作isCheckedMap来更新CheckBox的状态 if(convertView == null){ holder = new ViewHolder(); convertView = inflater.inflate(R.layout.layout_listviewitem, null); holder.tvName = (TextView)convertView.findViewById(R.id.dailyName); holder.dailyPic= (ImageView) convertView.findViewById(R.id.dailyPic); holder.cBox = (CheckBox)convertView.findViewById(R.id.isCheakBox); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.cBox.setChecked(item.check); holder.tvName.setText(item.name); if(item.name.contains("txt")){ holder.dailyPic.setImageResource(R.mipmap.txt); }else{ holder.dailyPic.setImageResource(R.mipmap.txt); } return convertView; } public final class ViewHolder { public TextView tvName; public CheckBox cBox; ImageView dailyPic; } } class Item { private Integer id; private String name; boolean check; String address; } public boolean delete(String address) { File file = new File(address); if (file == null || !file.exists() || file.isDirectory()){ return false; } file.delete(); count--; if(num_xuanze==count){ allCheckBox.setTextColor(getResources().getColor(R.color.lv)); }else { allCheckBox.setTextColor(getResources().getColor(R.color.red)); } return true; } }
7.
https://pan.baidu.com/s/1skZClmX