Just a disclaimer, I'm very new to both android and Java.
The end product that I'm going after in this is a soundboard like application. I want to display a list of sounds that the user can tap on to play the associated sound file.
So far I have setup a couple of string-arrays in my strings.xml file. One array represents the sounds available, and the second array represents the actual R.raw.soundfile resources. Might be easier to just show it.
<string-array name="alex_list">
<item>Sound Title</item>
</string-array>
<string-array name="alex_sounds">
<item>R.raw.soundfile</item>
</string-array>
Here is the class definition I have so far. I really don't want anyone to "do the work for me" as it were, but I'm completely lost as to where to progress from here. I feel like I should pass the string-array position from alex_list (when clicked) to get the right resource from alex_sounds (which should play immediately, doesn't need media controls). But that's where I get stuck.
public class AlexList extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(), R.array.alex_list, R.layout.list_item));
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
// I have zero clue what to do from here
}
});
}
}
If there is a better way to do any of this, please tell me. The last thing I want to do is develop bad habits at an early stage. I'll continue to work on it and post back if I find a solution.
Thanks in advance for your time!
Change your values array this way:
<string-array name="alex_sounds">
<item>soundfile</item>
</string-array>
Then your listener becomes: (packageName is a string representing the package of your R file: e.g. "com.example.main")
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
String selectedFromList = getItemAtPosition(pos);
int resId = getResources().getIdentifier(selectedFromList, "raw", packageName);
MediaPlayer mp = MediaPlayer.create(AlexList.this, resId);
mp.start();
}
This will play the file which name you selected in your list view, from your res/raw folder.
(Coded quickly in StackOverflow, there may be syntax errors)
Related
I have a question and know this has been asked a million times, I am fairly new to Android Programming. So I am kinda boggled.
I have created two parallel arrays - containing phrases in two languages.As the spinner changes it value(from 1st array) - the textview should be updated to reflect the translated phrase (in the second array).
The two arrays are created in string.xml, similar to below and the spinner gets it value from there.
<string-array name="array_name">
<item>phrase1</item>
<item>phrase2</item>
<item>phrase3</item>
<item>phrase4</item>
</string-array>
Im not sure how to go about doing this or if this is a viable way of doing it.... but boggled I remain....
Can someone please help?
You should set OnItemSelectedListener to spinner and change the text
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textView.setText(getResources().getTextArray(R.array.array_name)[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I apologize if this is a repeat, but my research has come up empty in finding stuff pertinent to this specific case.
I'm trying to create an application where the user clicks a button in an activity and is prompted with a listView in another activity with the titles of songs as the content. When the user clicks on a listView item, the corresponding song (saved in the Raw file) will play. Assume the songs are named song1, song2, etc, for now. I have the app working up to choosing the song. They are prompted with the listview which correctly displays the titles, but I'm stuck as to what to do from this point.
So basically, I need help with handling the user click on any given item to play that song in the original activity.
Code for the Main Activity:
public class HomeScreen extends Activity {
//set view to the home screen
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
}
//Show the Activity for the song list
public void showSongList (View clickedButton) {
// the showSongList is defined in the layout xml file.
Intent switchScreen = new Intent(HomeScreen.this, SongList.class);
startActivity(switchScreen);
}
}
Code for the Activity with the listView
public class SongList extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Store the string resources into the array
String[] music_list = getResources().getStringArray(R.array.songs);
//Binding resources array into adapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, music_list));
}
}
This is the XML for the string array which the list is based off of
<resources>
<string-array name="songs">
<!-- Go back will be defined later to return to the original Activity -->
<item>Go Back</item>
<item>First Song</item>
<item>Second Song</item>
<item>Third Song</item>
<item>Fourth Song</item>
<item>Fifth Song</item>
<item>Sixth Song</item>
<item>Seventh Song</item>
<item>Eighth Song</item>
<item>Ninth Song</item>
<item>Tenth Song</item>
<item>Eleventh Song</item>
<item>Twelfth Song</item>
</string-array>
</resources>
I apologize that this is probably an easy question for someone who knows what they're doing.
If there is any extra code that needs posted, Just ask. Thanks in advance.
You can add setonitemClickListener to your listview. In the listener, you will get the song's posision in the listview. So you can know the song selected by user and play the music.As for the code of playing music, I'm sorry that I havent used.By the way, if you need add or remove songs, getting the songs list by file may be better.
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,long arg3) {
//arg2 means the chosen songs posision. You can control your play here with the parameter}
});
In you code, the mListView is this.
To accomplish this task use the onItemClickListener. set it on your list view and use the position argument in onItemClick() to get the position of each item clicked. Then pass it to any method which will resolve the position and play the song by creating its object.
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
i = arg2;
musicPlay(b1);
}
public void musicPlay(View v){
int id=0;
if(i==0){
id=R.raw.abc;
}
if(i==1){
id=R.raw.abcde;
}
if(i==2){
id=R.raw.gghi;
}
if(mp==null){
mp=MediaPlayer.create(this, id);
mp.start();
}
mp=MediaPlayer.create(this, id);
mp.start();
}
Here use i as an global integer variable.
Hope this helps.
Let me explain myself:
As you know, when you've a view which have to be inflated several times, but changing values, you use a GridView or a ListView. Those two Composite views, have some methods like onItemClick. This method is so useful, as it returns the position of the view clicked.
With this position you can perform some concrete tasks, like retreiving from an ArrayList, the information of that object. Here's an example:
ArrayList<DocumentInfo> documents;
And when you set a setOnItemClickListener() you can get the correct values:
gallery.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
getDocumentInfoOf(pos);
}
});
public void getDocumentInfoOf(int position){
DocumentInfo doc = documents.get(position);
}
However, when you aren't using a GridView or a ListView, you're in your own. You don't have a clear way (AFAIK) to know which layout inflated is the one clicked (I mean like the previous example, the "position" value).
What I am currently doing, is the following:
for (int i=0; i<10;i++){
RelativeLayout documentInflated = (RelativeLayout) this.mInflater.inflate(R.layout.open_document_per_inflar, null);
documentInflated.setContentDescription(""+i);
documentInflated.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
openDocument(v);
}
});
container.addView(documentInflated);
}
public void openDocument(View v){
int idDocument = Integer.parseInt(v.getContentDescription());
//idDocument is the view clicked
}
Do you guys think this is a clear way of doing this?
Thank you!!!
If I'm not mistaken you want to get some data from your created Relative Layout when you click on it. The best solution here is to use the method setTag(Object tag). After that you get the informatiom with the method getTag(). This method allows you to add extra information to your view. As it says in the documentation:
Tags
Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.
Also depending on your needs you can seperate every tag with a key -> value pair with the method setTag(int key, Object tag), after that you can retrieve this object with getTag(int key);
So in your case you will have
documentInflated.setTag(i)
in the onClick yo will then have:
int i = (int)v.getTag();
Unless I'm not understanding your desire...
#Override
public void onClick(View v) {
openDocument(v);
}
v IS the view being clicked. Your code looks like it should do what you're hoping it will do. What are you actually seeing happen?
So I've been following some advice around similar questions and I somehow couldn't manage to get into it. I have a spinner called 'e' from which I want to retrieve the selected string when the user clicks a button below it.
I implemented the nested class like this:
class SelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
String selected = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent){
//does nothing
}
}
e.setOnItemSelectedListener(new SelectedListener());
Just following the advice from other users around here and the android developers tutorial, the thing is, after this, I don't know which call should I make to retrieve it if I want to save it into a String variable like so:
String selected = //don't know what to put here
Hope is clear enough. Thanks in advance.
I'm pretty sure:
e.getSelectedItem().toString()
So my situation is this. I have two tables. A main table where data is added using a form, this form has a spinner which is powered from the second table (this part is working correctly). I then want the selected item in the spinner to be stored in a foreignkey field in the main table.
I have been using the following code:
private Spinner mSpinner;
mSpinner = (Spinner) findViewById(R.id.spinCat);
and in my populate fields method I have the following:
mSpinner.getSelectedItem();
but this stores something like "android.database.sqlite.SQLiteCursor#43e5be60" in to the database rather than the name.
If anyone can offer some help it would be great, this has been bothering me for some days now and has halted development somewhat. If you need to see more code, then please don't hesitate to ask.
Any help is much appreciated.
EDIT: new code:
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View vw,
int pos, long id) {
LinearLayout rowll = (LinearLayout) vw;
TextView test = (TextView) rowll.getChildAt(0);
Toast t = Toast.makeText(parent.getContext(), test
.getText().toString(), Toast.LENGTH_SHORT);
t.show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// Do nothing..
}
});
getSelectedItem returns an Object (which is what your output looks a lot like).
Did you try simply calling toString like this?
mSpinner.getSelectedItem().toString();
I personally just set a variable in the onItemSelected listener, but this should work too. What I use is:
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
myIP = parent.getItemAtPosition(position).toString();
}
Variable is then continuously updated and I just add it to the DB when I connect to the IP in question.
Cursor data extraction:
while (cursor.moveToNext()) {
ipList.add(cursor.getString(1));
}
getString(int columnIndex) does: Returns the value of the requested column as a String.
http://developer.android.com/reference/android/database/Cursor.html
Don't use .getChildAt(), use .getItemAtPositon(). See how the code does it at http://developer.android.com/resources/tutorials/views/hello-spinner.html.