Use listView to change songs in android mediaplayer - android

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.

Related

ListView example program which switches to new activity

Could anyone possibly give me an entire example for demonstrating a ListView.
Each item in listview needs to fire a new activity.
For ex, if I use 'Subjects' as content of ListView then after pressing particular a subject from listview , a new screen with its title and some content of that subject gets displayed.
I know this is too much to ask, but i have been looking for ListView Ex. since quite long and nothing has helped me so far. PLease Help
Thank you
yourlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
then use bundle to send some content to your next activity.
then getActionBar.setTitle(sometext); this text is come from your bundle.
This contains:
http://www.androidbegin.com/tutorial/android-search-listview-using-filter/
you can refer following link for listview demo code.
http://www.androidhive.info/2011/10/android-listview-tutorial/
and for opening new activity on selecting any item, you can use,
listCoupon.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// here you can compare selected item by its position and then start a new activity
// arg2 is selected item position in your arraylist
}
});

How to navigate to another screen in android using onListItemClick

I am kind of new to Android development. Could you please tell me, how to navigate to another screen using onListItemClick. I have 200 items in a ListView. If i click an item from the ListView, it has to navigate to another screen which should show the details of the clicked item.
To get what you want you can do something like this...
1>create another activity with certains controls in which you can place your values...
2>on itemclick() of listview...
a>create intent and set values to pass to new activity
b>start new activity with this intent.
3>in oncreate() of new activity
a>retrive the values from intent
b>populate your controls using the values...
I am assuming you are getting the list and detail of actor from database.
I have used the same walk through to display recipe detail of selected recipe in my app.
public class ActorList extends Activity {
ListView myActorList=null;
public final static String selectedActor_ID="will Keep Actor ID";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_actor_list);
//initialize controls
myActorList.setAdapter(adapter);
myActorList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v,
int position, long id) {
// TODO Auto-generated method stub
Intent i= new Intent(ActorList.this,ActorDetail.class);
// you have to pass the actor id to next activity
// you can get this actor id from argument of type "long"
i.putExtra(selectedActor_ID, String.valueOf(id));
startActivity(i);
}
});
}
}
//Other Activity To show Detail..
//get the ID of Selected Actor on other activity say "ActorDetail"
public class ActorDetail extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_actor_detail);
ActorID=getIntent().getStringExtra(ActorList.selectedActor_ID);
// now as you have the id here for that particular actor
//fetch the detail of that selected actor thru id and bind it to your layout.
}
}
You need to register the other activity in Manifest file also like this
<activity
android:name=".otherscreenActivity"
</activity>

One-to-many in a listview loads another listview in Android

The goal of the simple app is to have a one-to-many relationship so that when a user clicks on one item in a list, they are taken to another list with the items associated with the clicked object.
Simplified Example: Top list would have two items: Idioms, Phrasal Verbs. When the user clicks on Idioms, they are taken to a list of idioms and when user clicks on Phrasal Verbs they are taken to a list of phrasal verbs. After reaching the list of Idioms, they can choose an idiom and it takes them to a text view which displays a definition and example.
Currently, the topic list (Idioms and Phrasal Verbs) displays properly in the ListView, but when I click on one of them the app quits and I get many errors, this is one (E/AndroidRuntime(276): Caused by: android.content.res.Resources$NotFoundException: String array resource ID #0x0)
How can I get the one-to-many associated with the list item I click on? Are my intents correct?
<string-array name="topics">
<item>Idioms</item>
<item>Phrasal Verbs</item>
<string-array name="idioms">
<item>Cash Chow</item>
<item>In the Black</item>
</string-array>
<string-array name="pverbs">
<item>Back up</item>
<item>Back down</item>
</string-array>
The main activity file looks like this, it is displaying properly in the emulator.
public class TestActivity extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] topics = getResources().getStringArray(R.array.topics);
setListAdapter(new ArrayAdapter<String>(this, R.layout.topic_list, topics));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Intent i = new Intent(TestActivity.this, Sublist.class);
i.putExtra("position", position);
startActivity(i);
The second class I created is called Sublist and this is the code I have in it.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
int arrayB = extras.getInt("position");
String[] subtopics = getResources().getStringArray(arrayB);
setListAdapter(new ArrayAdapter<String>(this, R.layout.topic_list, subtopics));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
first I would suggest you put anything which is related, together in one array like this:
<string-array name="idioms">
<item>CashCow</item>
<item>In the Black</item>
</string-array>
after that you make your List displaying like normal. in the OnClick you can then use the same Adapter, but update his data.
public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) {
int textId = R.id.YOUR_TEXTVIEW_ID;
String[] data;
switch (position) {
case 0:
data = getResources().getStringArray(R.id.idioms);
break;
case 1:
data = getResources().getStringArray(R.id.phrasal_verbs);
break;
default:
data = someFallback;
break;
}
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, textId, data);
mListView.setAdapter(aa);
mListView.postInvalidate();
}

Android select in ListView and push button to select

I have a ListView populated with the names of US states from a database. To transition to a new screen, I want to select a state in that ListView and then hit a button that transitions to a screen of cities in that state.
I think I know how to populate the ListView, but I do not know what the inner button workings should look like. Plus, I do not know how to use to carry the state info through the transition to use it to list cities.
Does this code have anything to do with this?
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
blah.setCurrentListID(arg3);
Let me know. Thanks.
First, assuming you use an adapter class that extends ArrayAdapter with states, like:
public class StatesItemAdapter extends ArrayAdapter<UsState>
Then you have a List like ArrayList as the data source for the ArrayAdapter right? Then,
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
UsState state = statesList.get(position); // on ArrayList
// or try statesList.get(listView.getSelectedItemPosition());
Intent citiesIntent = new Intent(getApplicationContext(), CitiesActivity.class);
citiesIntent.putExtra("state_name", state.getName()); // Name string originating from database record
startActivity(citiesIntent);
}
And then in the Cities Activity, in onCreate you must get the name of the state and use it as a criteria in your database query to get a Cursor of city results:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle bundleExtras = intent.getExtras();
String selectedState = bundleExtras.getString("state_name");
// Do database query on cities relating to state
.......

Android: get listview onClick to new intent

i hava got the code below. i want to start an activity when i click on a single item on the list. however when i do nothing happens. I alsow want every item to refer to the same intent calld "com.whiskey.app.view" and send a id variable that was given by the sql query. I browsed trough the code several times but i cant seem t get it to work.
public class MainScreen extends Activity implements OnItemClickListener{
public ListView whiskeylist;
public String[] DataArryWhiskey;
....
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Start db view of whiskey
DBConfig whiskeyrows = new DBConfig(this);
whiskeyrows.open();
DataArryWhiskey = whiskeyrows.getDataInArray();
whiskeyrows.close();
whiskeylist = (ListView)findViewById(R.id.listofWhiskey);
whiskeylist.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , DataArryWhiskey));
// End db view of whiskey
}// end onCreate
// catch itemclick event from the main list.
public void onItemClick(AdapterView av, View v, int position, long l)
{
// TODO Auto-generated method stub
String[] listitem_data = DataArryWhiskey[position].split(","); // break passed sting into a array comma seperated
Bundle passingitems = new Bundle();
passingitems.putString("whiskey_id", listitem_data[0]);
Intent currentintent = new Intent("com.wiskey.app.view");
currentintent.putExtras(passingitems);
startActivity(currentintent);
}
Although the above answers work, I think that the problem in your current implementation is that you don't call:
whiskeylist.setOnItemClickListener(this);
I think this should do the work!
If your activity only cotains this ListView, you should use a ListActivity.
These are made specifically for activities that only contain lists.
One of the methods for ListActivities is onListItemClick. That one is specifically for clicking on items in lists, as the name says.
The reason your code doesn't work is because onItemClick isn't generally used for clicking in Lists, but for clicking other objects in Activities.
Try changing your code based on the samples here: ListActivity
Derive your class from ListActivity and remove the implements OnItemClickListener
Put below code in onCreate
setListAdapter(whiskeylist);
And then have this as your onItemClick
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String[] listitem_data = DataArryWhiskey[position].split(","); // break passed sting into a array comma seperated
...your code....
startActivity(currentintent);
}
Also refer to:
ListActivity
ListView
You have not added listener for click actions, try adding:
whiskeylist.setOnItemClickListener(this);
at the end of onCreate
You might also write anonymous OnItemClickListener as in here:
http://developer.android.com/resources/tutorials/views/hello-listview.html

Categories

Resources