Skip to content

Instantly share code, notes, and snippets.

@nikunjparadva
Forked from kaecy/OpenLinksListener.java
Created July 27, 2018 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikunjparadva/944a714ad1cb2acbbf8db235f07e140c to your computer and use it in GitHub Desktop.
Save nikunjparadva/944a714ad1cb2acbbf8db235f07e140c to your computer and use it in GitHub Desktop.
List All Audio Files in Android - modifications to this https://github.com/kaecy/android-demos/tree/master/adding%20search
package org.cheat.search;
import android.content.Intent;
import android.content.Context;
import android.net.Uri;
import android.widget.TextView;
import android.widget.ListView;
import android.provider.MediaStore.Audio;
public class OpenLinksListener implements ListView.OnItemClickListener {
Context context;
public OpenLinksListener(Context context)
{
this.context = context;
}
@Override
public void onItemClick(android.widget.AdapterView<?> parent, android.view.View view, int position, long id)
{
Uri openable = Uri.parse(Audio.Media.getContentUri("external").toString() + "/" + (((TextView)view).getText().toString().split(": ")[0]));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(openable, "audio/*");
context.startActivity(intent);
}
}
package org.cheat.search;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SearchView;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.database.Cursor;
public class Search extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.search_result_layout);
activity = this;
adapter = new ArrayAdapter(this, R.layout.textview);
ListView listview = (ListView)findViewById(R.id.listview);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OpenLinksListener(activity));
}
public void performSearch(String query)
{
Cursor music = getContentResolver().query(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{ "_id", "_data" },
"is_music != 0",
null, null);
adapter.clear(); // clear all the items in the listview
query = query.toLowerCase(); // we want the match to be case-insensitive
String path;
while ( music.moveToNext() )
{
path = music.getString(1).toLowerCase();
if ( path.contains(query) )
adapter.add(music.getString(0) + ": " + music.getString(1));
}
adapter.notifyDataSetChanged(); // now we tell the adapter to update the changes
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu activityMenu)
{
getMenuInflater().inflate(R.menu.actions, activityMenu);
SearchView search = (SearchView) activityMenu.findItem(R.id.search).getActionView();
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
activity.performSearch(text);
return true;
}
@Override
public boolean onQueryTextChange(String text) {
return false;
}
});
return true;
}
Search activity;
ArrayAdapter adapter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment