`
carywei
  • 浏览: 191372 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

sdlist

阅读更多

package com.vance.activity;

/*
 * getRootDirectory(); getDownloadCacheDirectory();
 * getExternalStorageDirectory(); getExternalStorageState();
 * 可以得到当前的sdcard 的各个状态。
 */
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SdListActivity extends Activity {
 public static final String TAG = "SdListActivity";
 public static final int ADD_ID = Menu.FIRST;
 public static final int DELETE_ID = Menu.FIRST + 1;
 public static final int EXIT_ID = Menu.FIRST + 2;
 Cursor c;
 Intent intent;
 List<String> COUNTRIES = new ArrayList<String>();
 Button button1;
 Button button2;
 ArrayList<String> lists = new ArrayList<String>();
 //MediaStore.Audio.AudioColumns.TITLE
 String[] from = {  
            MediaStore.Audio.Media.DATA};  
    int[] to = { android.R.id.text1};
 public void onCreate(Bundle b) {
  super.onCreate(b);
  ListView listView = new ListView(this);
//  listView.setAdapter(new ArrayAdapter<String>(this,
//    android.R.layout.simple_list_item_single_choice, COUNTRIES));
  // listView.setItemsCanFocus(true);
  
  // 准备数据
  prepareData();
  SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_multiple_choice, c,from,to);
  listView.setAdapter(adapter);
  OnItemClickListener listener = new OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    TextView t=(TextView)view.findViewById(android.R.id.text1);
//    setTitle(parent.getItemAtPosition(position).toString());
    String s=(String) t.getText().subSequence(8, t.getText().length());
    setTitle(s);
    lists.add(s);
   }
  };
  
  listView.setOnItemClickListener(listener);
  listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  setContentView(listView);
 }
 
 public void prepareData() {
  ContentResolver contentResolver=this.getContentResolver();
  c=contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
  // Environment.getExternalStorageState().equals()
  // File file=new File(Environment.getDataDirectory(), null);
//  File uri = new File("/sdcard/");
//  // 查询sdcard的文件。并装载在list中
//  queryFiles(uri);
 }

 public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  menu.add(0, ADD_ID, 0, "添加到播放列表");
//  menu.add(0, DELETE_ID, 0, "删除");
  menu.add(0, EXIT_ID, 0, "exit");
//  menu.findItem(DELETE_ID);
  return true;
 }

 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case ADD_ID:
   actionClickMenuItem1();
   break;
//  case DELETE_ID:
//   actionClickMenuItem2();
//   break;
  case EXIT_ID:
   actionClickMenuItem3();
   break;
  }
  return super.onOptionsItemSelected(item);
 }

 private void actionClickMenuItem3() {
  finish();
 }

 /*
  * 点击第一个menu的第一个按钮执行的动作
  */
 private void actionClickMenuItem1() {
  intent = new Intent();
  // 这里其实要判断Lists是否为空,如果为空,要toas一下
  if (lists.size() != 0) {
   intent.putStringArrayListExtra("lists", lists);
   setResult(1, intent);
   finish();
  } else {
   Toast.makeText(getApplicationContext(), "你没有选择歌曲", Toast.LENGTH_LONG).show();
  }
 }
 /*
  * 点击第二个个menu的第一个按钮执行的动作
  */
 private void actionClickMenuItem2() {
  button1.setVisibility(View.INVISIBLE);
  button2.setVisibility(View.VISIBLE);
 }

 private void queryFiles(File uri) {
  File[] files = uri.listFiles();
  for (int i = 0; i < files.length; i++) {
   if (files[i].isFile()) {
    if (files[i].getName().endsWith(".mp3")) {
     COUNTRIES.add(files[i].getName());
    }
   } else {
    queryFiles(files[i]);
   }
  }
 }
}

 

==========================

package com.vance.activity;

/* import相关class */
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class SdList extends ListActivity {
 public static final int ADD_ID = Menu.FIRST;
 public static final int DELETE_ID = Menu.FIRST + 1;
 public static final int EXIT_ID = Menu.FIRST + 2;
 Intent intent;
 Button button1;
 Button button2;
 ArrayList<String> lists = new ArrayList<String>();
 /*
  * 对象声明 items:存放显示的名称 paths:存放文件路径 rootPath:起始目录
  */
 private List<String> items = null;
 private List<String> paths = null;
 private String rootPath = "/";
// private TextView mPath;

 /** Called when the activity is first created. */
 @Override
 protected void onCreate(Bundle icicle) {
  super.onCreate(icicle);

  /* 载入main.xml Layout */
//  setContentView(R.layout.main);
//  mPath = (TextView) findViewById(R.id.mPath);
  getFileDir(rootPath);
 }

 /* 取得文件架构的method */
 private void getFileDir(String filePath) {
  /* 设置目前所在路径 */
//  mPath.setText(filePath);
  items = new ArrayList<String>();
  paths = new ArrayList<String>();
  File f = new File(filePath);
  File[] files = f.listFiles();
  if (!filePath.equals(rootPath)) {
   /* 第一笔设置为[回到根目录] */
   items.add("Back to " + rootPath);
   paths.add(rootPath);
   /* 第二笔设置为[回上层] */
   items.add("Back to ../");
   paths.add(f.getParent());
  }
  /* 将所有文件添加ArrayList中 */
  for (int i = 0; i < files.length; i++) {
   File file = files[i];
   items.add(file.getName());
   paths.add(file.getPath());
  }
  /*
   * 声明一ArrayAdapter,使用file_row这个Layout, 并将Adapter设置给此ListActivity
   */
  ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,
   android.R.layout.simple_list_item_multiple_choice, items);
  setListAdapter(fileList);
  this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
 }

 /* 设置ListItem被点击时要做的动作 */
 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  File file = new File(paths.get(position));
  if (file.isDirectory()) {
   /* 如果是文件夹就再进去捞一次 */
   getFileDir(paths.get(position));
  } else {
   if(l.getItemAtPosition(position).toString().endsWith(".mp3")){
    lists.add(l.getItemAtPosition(position).toString());
   }
   /* 如果是文件,则跳出AlertDialog */
//   new AlertDialog.Builder(this).setIcon(com.vance.R.drawable.icon).setTitle(
//     "[" + file.getName() + "] is File!").setPositiveButton(
//     "OK", new DialogInterface.OnClickListener() {
//      public void onClick(DialogInterface dialog,
//        int whichButton) {
//      }
//     }).show();
  }
 }
 
 
 public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  menu.add(0, ADD_ID, 0, "添加到播放列表");
//  menu.add(0, DELETE_ID, 0, "删除");
//  menu.add(0, EXIT_ID, 0, "exit");
//  menu.findItem(DELETE_ID);
  return true;
 }

 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case ADD_ID:
   actionClickMenuItem1();
   break;
//  case DELETE_ID:
//   actionClickMenuItem2();
//   break;
//  case EXIT_ID:
//   actionClickMenuItem3();
//   break;
  }
  return super.onOptionsItemSelected(item);
 }

// private void actionClickMenuItem3() {
//  finish();
// }
 /*
  * 点击第一个menu的第一个按钮执行的动作
  */
 private void actionClickMenuItem1() {
  intent = new Intent();
  // 这里其实要判断Lists是否为空,如果为空,要toas一下
  if (lists.size() != 0) {
   intent.putStringArrayListExtra("lists", lists);
   setResult(2, intent);
   finish();
  } else {
   Toast.makeText(getApplicationContext(), "你没有选择歌曲", Toast.LENGTH_LONG).show();
  }
 }
 /*
  * 点击第二个个menu的第一个按钮执行的动作
  */
 private void actionClickMenuItem2() {
  button1.setVisibility(View.INVISIBLE);
  button2.setVisibility(View.VISIBLE);
 }

}

================================

package com.vance.activity;

import java.util.ArrayList;
import java.util.HashSet;

import com.vance.service.DBService;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.OnTabChangeListener;

public class PlayListActivity extends TabActivity {
 public static final String TAG = "PlayListActivity";
 Intent intent;
 Button button1;
 Button button2;
 DBService db;
 public static final int ADD_ID1 = Menu.FIRST;
 public static final int CLEAR_ID1 = Menu.FIRST+4;
 public static final int EXIT_ID1 = Menu.FIRST + 1;
 public static final int EXIT_ID2 = Menu.FIRST + 2;
 public static final int ADD_ID2 = Menu.FIRST + 3;

 private static final int DIALOG1 = 1;
 private static final int DIALOG2 = 2;
 private static final int DIALOG4 = 4;
 private static final int DIALOG3 = 3;
 
 ArrayList<String> lists=new ArrayList<String>();
 ListView listView1;
 ListView listView2;
 TabHost tabHost;
 protected Menu myMenu;
 protected int myMenuSettingTag = 1;
 String TAB2 = "tab2";
 String TAB1 = "tab1";
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  db = new DBService(this);
  db.openDb();
  setTitle("播放列表");
  tabHost = getTabHost();
  LayoutInflater.from(this).inflate(com.vance.R.layout.tab_list,
    tabHost.getTabContentView(), true);
  // 第一个标签
  listView1 = (ListView) this.findViewById(com.vance.R.id.tab1list);
  listView2 = (ListView) this.findViewById(com.vance.R.id.tab2list);
  

  tabHost.addTab(tabHost.newTabSpec(TAB1).setIndicator("tab1")
    .setContent(com.vance.R.id.tab1));

  tabHost.addTab(tabHost.newTabSpec(TAB2).setIndicator("tab2")
    .setContent(com.vance.R.id.tab2));

  /* 标签变化的监听 */
  tabHost.setOnTabChangedListener(new OnTabChangeListener() {
   public void onTabChanged(String tabId) {
    if (tabId.equals(TAB1)) {
     myMenuSettingTag = 1;
    }
    if (tabId.equals(TAB2)) {
     myMenuSettingTag = 2;
    }
    if (myMenu != null) {
     onCreateOptionsMenu(myMenu);
    }
   }
  });
 }

 @Override
 protected void onStart() {
  super.onStart();
  Cursor c = db.findAlllistName();
  startManagingCursor(c);
  if (c != null) {
   SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
     android.R.layout.simple_list_item_1, c,
     new String[] { "listname" },
     new int[] { android.R.id.text1 });
   listView1.setAdapter(adapter);
   listView1.setOnItemClickListener(listener);
  }
 }
 
 
 
 @Override
 protected void onDestroy() {
  db.closeDb();
  super.onDestroy();
 }

 

 Cursor cc ;
 String listName;
 private OnItemClickListener listener = new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View arg1, int position,
    long arg3) {
   TextView tv=(TextView)arg1.findViewById(android.R.id.text1);
   listName=tv.getText().toString().trim();
   cc= db.findAllMusicName( listName );
   if (cc != null) {
    lists.clear();
    while(cc.moveToNext()){
     lists.add(cc.getString(1));
    }
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(
      PlayListActivity.this,
      android.R.layout.simple_list_item_1, cc,
      new String[] { "musicname" },
      new int[] { android.R.id.text1 });
    listView2.setAdapter(adapter);
   }
   tabHost.setCurrentTabByTag(TAB2);
  }
 };
 
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  myMenu = menu;
  myMenu.clear();// 清空MENU菜单
  switch (myMenuSettingTag) {
  case 1:
   myMenu.add(0, ADD_ID1, 0, "创建新播放列表");
   myMenu.add(0, CLEAR_ID1, 0, "清空播放列表");
   myMenu.add(0, EXIT_ID1, 0, "exit");
   break;
  case 2:
   myMenu.add(0, ADD_ID2, 0, "添加到临时播放列表");
   myMenu.add(0, EXIT_ID2, 0, "exit");
   break;
  }
  return super.onCreateOptionsMenu(menu);
 }

 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case ADD_ID1:
   actionClickMenuItem1();
   break;
  case EXIT_ID1:
   actionClickMenuItem2();
   break;
  case ADD_ID2:
   actionClickMenuItem3();
   break;
  case EXIT_ID2:
   actionClickMenuItem4();
   break;
  case CLEAR_ID1:
   actionClickMenuItem5();
   break;
  }
  return super.onOptionsItemSelected(item);
 }

 private void actionClickMenuItem5() {
  db.deleteAllListName();
  Cursor c = db.findAlllistName();
  startManagingCursor(c);
  if (c != null) {
   SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
     android.R.layout.simple_list_item_1, c,
     new String[] { "listname" },
     new int[] { android.R.id.text1 });
   listView1.setAdapter(adapter);
   listView1.setOnItemClickListener(listener);
  }
 }

 private void actionClickMenuItem1() {
  showDialog(DIALOG1);
 }

 protected Dialog onCreateDialog(int id) {
  switch (id) {
  case DIALOG1:
   return buildDialog1(PlayListActivity.this);

  }
  return null;
 }

 protected void onPrepareDialog(int id, Dialog dialog) {
  if (id == DIALOG1) {
   setTitle("添加播放列表");
  }
 }

 String name;
 EditText edit_text;
 View textEntryView;

 private Dialog buildDialog1(Context context) {
  AlertDialog.Builder builder = new AlertDialog.Builder(context);
  LayoutInflater inflater = LayoutInflater.from(this);
  textEntryView = inflater.inflate(
    com.vance.R.layout.alert_dialog_text_entry, null);
  builder.setView(textEntryView);
  builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
    edit_text = (EditText) textEntryView
      .findViewById(com.vance.R.id.list_edit);
    name = edit_text.getText().toString();
//    mHandle.sendEmptyMessage(0);
    Intent intent = new Intent(PlayListActivity.this,
      SdListActivity.class);
    startActivityForResult(intent, 2);
   }
  });
  builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {

    setTitle("点击了对话框上的取消按钮");
   }
  });
  return builder.create();
 }


 private void actionClickMenuItem2() {
 }
 private void actionClickMenuItem3() {
  Intent intent = new Intent();
  intent.putExtra("listName", listName);
  intent.putStringArrayListExtra("lists", lists);
  setResult(1, intent);
  finish();
 }

 private void actionClickMenuItem4() {
  
 }
 
 
 
// private Handler mHandle = new Handler() {
// public void handleMessage(Message msg) {
//  ArrayAdapter<String> adapter = new ArrayAdapter<String>(
//    PlayListActivity.this, android.R.layout.simple_list_item_1,
//    sta1);
//  listView1.setAdapter(adapter);
// }
//};
ArrayList<String> songs = new ArrayList<String>();
HashSet<String> s = new HashSet<String>();

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 if (resultCode == 1) {
  if (requestCode == 2) {
   ArrayList<String> ls = data.getStringArrayListExtra("lists");
   songs.addAll(ls);
   s = new HashSet<String>(songs);
   songs.clear();
   songs.addAll(s);
   for (String s : songs) {
    db.save(s, name);
   }
   Toast.makeText(getApplicationContext(), "歌曲添加成功",
     Toast.LENGTH_LONG).show();
  }
 }
}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics