package com.example.com.scxh.mediaplayer;
import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements Button.OnClickListener {
private String info = "搜索结果 :";
private String path;
private EditText meEditText;
private Button mbButton;
private ListView mlistView;
private String mInputKeyWord;
private ArrayList<String> mArrayList = new ArrayList<String>();
private ListAdapters listAdapters;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meEditText = (EditText) findViewById(R.id.edittext);
mbButton = (Button) findViewById(R.id.button);
mlistView = (ListView) findViewById(R.id.list);
mbButton.setOnClickListener((OnClickListener) this);
listAdapters = new ListAdapters(this);
mlistView.setAdapter(listAdapters);
// listAdapters.setData(mArrayList);
mlistView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ListAdapters listAdapters;
listAdapters = (ListAdapters) parent.getAdapter();
String path = (String) listAdapters.getItem(position);
// 发送数据源
Intent intent = new Intent(MainActivity.this,
MidiaPlayer3.class);
intent.putExtra("key", path);
// 发送列表数据源
intent.putStringArrayListExtra("song", mArrayList);
startActivity(intent);
}
});
}
public void onClick(View v) {
mInputKeyWord = meEditText.getText().toString();
BrowserFile(Environment.getExternalStorageDirectory(), mInputKeyWord);
}
private void BrowserFile(File file, String KeyWord) {
if (KeyWord.equals("")) {
Toast.makeText(this, "如果输入框没有输入点击搜索按钮", Toast.LENGTH_SHORT).show();
} else {
ToSearchFiles(file);
listAdapters.setData(mArrayList);
}
}
private void ToSearchFiles(File files) {
File[] fileList = files.listFiles();
if (null != fileList) {
//遍历文件
for (File file : fileList) {
if (file.isDirectory()) {
ToSearchFiles(file);
} else {
try {
/* 是文件,进行比较,如果文件名称中包含输入搜索Key,则返回大于-1的值 */
if (file.getName().indexOf(mInputKeyWord) > -1) {
/* 获取符合条件文件的路径,进行累加 */
mArrayList.add(file.getAbsolutePath());
}
} catch (Exception e) {
Toast.makeText(this, "出错", Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
-------------------------------------------------------------------分割线------------------------------------------------------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/edittext"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="240dp"
android:text="搜索"
android:textSize="16dp" />
</RelativeLayout>
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/edittext"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="240dp"
android:text="搜索"
android:textSize="16dp" />
</RelativeLayout>
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" />
</RelativeLayout>