How to list music files in the raw folder - android

I am trying to create a simple Mediaplayer application. It works actually, but I want to do is to show mp3 files on a Textview. I got the list like this way below (I think so). How can I set these filenames to a Textview
List<String>ListOfMusic=new ArrayList<String>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Field[]fields=R.raw.class.getFields();
for (int i = 0; i < fields.length; i++) {
ListOfMusic.add(fields[i].getName());
}
initComp();
textShown.setText(ListOfMusic[0]);

Loop through the ListOfMusic and add the items to the TextView (preceding every song name with the "newline" character if you want do display the song names on separate lines).
Something like this:
String songs="";
for(String songName: ListOfMusic){
songs+=songName+"\n";
}
textShown.setText(songs);

try this
String text = "";
for(String s : ListOfMusic) {
text+=s+"\n";
}
textShown.setText(text);

Related

Removing Stopwords from String

I need to remove stopwords from a string. I use the following code to remove stopwords and setting the final output in a textView. But when i run the code it always give the output "bugs". In other words it always give me the last string word as output. Please Check my code and Help!
public class Testing extends Activity {
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.testing);
t1= (TextView)findViewById(R.id.textView1);
String s="I love this phone, its super fast and there's so" +
" much new and cool things with jelly bean....but of recently I've seen some bugs.";
String[] words = s.split(" ");
ArrayList<String> wordsList = new ArrayList<String>();
Set<String> stopWordsSet = new HashSet<String>();
stopWordsSet.add("I");
stopWordsSet.add("THIS");
stopWordsSet.add("AND");
stopWordsSet.add("THERE'S");
for(String word : words)
{
String wordCompare = word.toUpperCase();
if(!stopWordsSet.contains(wordCompare))
{
wordsList.add(word);
}
}
for (String str : wordsList){
System.out.print(str+" ");
t1.setText(str);
}
}
t1.setText(str); means it doesn't care what the previous text was. It puts the last one in loop. So use append instead.
t1.append(str);
OR Append every single str to a single String and set that in TextView after the loop.
The output is "bugs." because of this line of code:
t1.setText(str);
which will re-write the textview everytime inside the loop. Because the last iteration the word is "bugs.", the textview will display bugs.
If you want to append the string instead of re-writting it use:
t1.append(str);
Hope it helps.

Get files and directory listings in android

I am working on an android project and I am trying to get a list of files and directories from the SD Card. It seems to be more a less working except the file name is outputting a load of nonsense and I can't see why.
Below is the code I am using to get the file listing.
public ArrayList getFileDirectoryListing()
{
ArrayList fileAndDirectories = new ArrayList();
final String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{
File[] files = Environment.getExternalStorageDirectory().listFiles();
for (int i = 0; i < files.length; i++)
{
FileDirectoryDetails fileDirectoryDetails = new FileDirectoryDetails();
fileDirectoryDetails.path = files[i].getName();
if (files[i].isDirectory())
{
fileDirectoryDetails.fileOrDirectory = FileOrDirectory.Directory;
}
else
{
fileDirectoryDetails.fileOrDirectory = FileOrDirectory.File;
}
fileAndDirectories.add(fileDirectoryDetails);
}
}
return fileAndDirectories;
}
Below is the code I am using to set the list adapter
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
listView = getListView();
ArrayList<FileDirectoryDetails> filesAndDirectories = getFileDirectoryListing();
fileDirectoryDetailsArrayAdapter = new
ArrayAdapter<FileDirectoryDetails>(this, android.R.layout.simple_list_item_1, filesAndDirectories);
setListAdapter(fileDirectoryDetailsArrayAdapter);
}
Below is a screenshot of what I am getting back in the list view instead of the actual file names.
Make sure you override toString() in your FileDirectoryDetails returning meaningful details. Currently you're using the default toString()
Or just fill your array with paths strings instead of the whole FileDirectoryDetails
Alternatively, override getView() of the adapter setting the text of the TextView to details.path

Is there a way to change a reference pathway using a variable? [duplicate]

This question already has answers here:
How to get a resource id with a known resource name?
(10 answers)
Closed 8 years ago.
Is there a way to change a reference to an ID in the Android manifest using a variable?
As in:
for(int counter6=1;counter6 <= 12; counter6++)
value = bundle.getString("value"+counter6);
TextView text1 = (TextView) findViewById(R.id.textView+counter6);
text1.setText(value);
Is it possible to have the counter6 variable used in the ID directory, so the for loop can loops through all the different text view making each one text1 respectively then setting their text to the string value?
Its not really a problem if it cant work this way it just means more lines of code to write.
You can't really make a loop on the id and increment it as it is generated but you can make an array of references and by getting that array find each TextView and update the text:
<array name="array_text_views">
<item>#id/text_view_1</item>
<item>#id/text_view_2</item>
<item>#id/text_view_3</item>
<array>
In your code, something like that:
ArrayList<TextView> myTextViews = new ArrayList<TextView>();
TypedArray ar = context.getResources().obtainTypedArray(R.array.array_text_views);
int len = ar.length();
for (int i = 0; i < len; i++){
myTextViews.add(findById(ar[i]));
}
ar.recycle();
I would usually just put a small int[] array of Ids into the code somewhere. If you have a lot of them, consider creating them programmatically (layout.addView(new TextView(..).
For example if you want to start an Activity and tell it what strings to display via the Extras Bundle you can put them directly as an array.
void startOther(String[] texts) {
Intent i = new Intent( /* ... */);
i.putExtra("texts", texts);
// start via intent
}
Now inside that Activity I would put the ids as a "constant".
// hardcoded array of R.ids
private static final int[] TEXT_IDS = {
R.id.text1,
R.id.text2,
// ...
};
And then use both the Bundle and the id Array for example like this:
// a List of TextViews used within this Activity instance
private List<TextView> mTextViews = new ArrayList<TextView>(TEXT_IDS.length);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.something);
// find all TextViews & add them to the List
for (int id : TEXT_IDS) {
mTextViews.add((TextView)findViewById(id));
}
// set their values based on Bundle
String[] stringArray = savedInstanceState.getStringArray("texts");
for (int i = 0; i < mTextViews.size() && i < stringArray.length; i++) {
mTextViews.get(i).setText(stringArray[i]);
}
}

Android how to print a array in text view or anything

I have a array of words and I would like to print the array of words onto the screen in a text view after a button is clicked. i was using a for loop to go through the whole list and then set text, but i keep getting a error with that plus it will just replace the last value, so it wont print the whole array. If anybody could explain to me how to do like a g.drawSting() but android version that would be great. my code rt now is not with me but it something like:
-I'm a beginner to android btw, probably could tell by this question tho.
public void onCreate(Bundle savedInstanceState)
{
//code for a button just being pressed{
//goes to two methods to fix the private array{
for(int y=0; y<=array.size()-1; y++){
textArea.setText(aarray.get(y)); //prints all strings in the array
}
}
}
int arraySize = myArray.size();
for(int i = 0; i < arraySize; i++) {
myTextView.append(myArray[i]);
}
if you want to print one by one, then use \n
myTextView.append(myArray[i]);
myTextView.append("\n");
PS:
Whoever suggesting to change .size() to .length(), thanks for you suggestion.
FYI,
The questioner mentioned the variable name is array.size() in question, so the answer also having the same variable name, to make it easier for the questioner.
if your variable (myArray) is an Array use myArray.length(), if it is ArrayList use myArray.size()
You have to combine all text into a String before you can give it the TextView. Otherwise you overwrite the text all the time.
public void onCreate(Bundle savedInstanceState)
{
StringBuilder sb = new StringBuilder();
int size = array.size();
boolean appendSeparator = false;
for(int y=0; y < size; y++){
if (appendSeparator)
sb.append(','); // a comma
appendSeparator = true;
sb.append(array.get(y));
}
textArea.setText(sb.toString());
}
I use this no-index solution, just an easy to remember one liner:
for(File file:list) Log.d(TAG, "list: " + file.getPath());

Reference control using string instead ID

Typical way to reference android control is something like this:
TextView tv = (TextView)findViewById(R.id.tv);
Where R.id.tv is integer referencing my xml control.
The thing is I would like to make reference using string "R.id.tv". Is that possible?
Let's say I have multiple controls:
tv1,
tv2,
tv3,
tv4,
tv5,
How would I put this into some sort of loop and interate through controls. I am thinking I would use loop counter to reference different controls. How's that to be done? Thanks.
One approach is to put the ids into an array and reference by subscript.
int[] ids = { R.id.tv1, R.id.tv2 /* etc. */ };
for (int i = 0; i < ids.length; ++i) {
TextView tv = (TextView)findViewById(ids[i]);
}
Try next
private int getIdResourceByName(String aString)
{
String packageName = "com.myProject.myPackage"; // set your package name here
int resId = getResources().getIdentifier(aString, "id", packageName);
return resId;
}
...
for (int i = 1; i<=5; i++) {
TextView tv = (TextView) findViewById(getIdResourceByName("tv" + Integer.toString(i)));
...
}
Have a look at this question:
Using findviewbyid with a string in a loop
I don't understand why you'd want to do this, it's pretty ugly, inefficient, and likely to cause maintenance issues and bugs.
Why not use a collection (e.g. ArrayList) to store references to all the controls?

Categories

Resources