Why Sound in MediaPlayer does not Play? - android

The problem is that the sound doesn't play.
When I run the application this sentence appear in (Android Mointor)" D/AbsListView: unregisterIRListener() is called "
the MainActivity.java code
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
//instantiate custom adapter
List adapter = new List(list, this);
//handle listview and assign adapter
ListView lView = (ListView)findViewById(R.id.listView);
lView.setAdapter(adapter);
}
}
the List.java code:
public class List extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
MediaPlayer mediaPlayer;
public List(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_list, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button addBtn = (Button)view.findViewById(R.id.add_btn);
addBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
MediaPlayer.create(List.this.context, R.raw.people)
mediaPlayer.start();
notifyDataSetChanged();
}
});
return view;
}
}

I think you must be changed this to context
addBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
mediaPlayer.create(context,R.raw.people);
mediaPlayer.start();
notifyDataSetChanged();
}
});

Try this:
#Override
public void onClick(View v) {
mediaPlayer = MediaPlayer.create(context,R.raw.people);
mediaPlayer.start();
notifyDataSetChanged();
}

You can use the below method to play a sound. It can be played from storage or url from Internet.
public void playAudio(String audioPath) {
try {
MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(audioPath);
mp.prepare();
mp.start();
Log.d(TAG, "is playing audio...");
} catch (Exception e) {
Log.d(TAG, "cannot play audio: " + e.toString());
}
}

Related

In listview I click an audio to start but when I scroll I see other audios has stared too

I have a ListView of audios which have seek bar in it . When I click the play button of one of audios, it starts but when I scroll down I see that some other seekbars are also playing and when I scroll up again the first item that I clicked is no paused. I have searched a lot and it seems that there is a problem with position of item in list , the only solution I found was to use viewholder in adapter but it didn't work . does any one have a solution? thanks.
public class AudioAdapter extends ArrayAdapter<News> {
Context context;
boolean isPLaying = false;
public AudioAdapter(Context context, ArrayList<News> array) {
super(context, R.layout.list_item_news, array);
this.context = context;
}
public static class ViewHolder {
TextView txtTitle;
SeekBar seekBar;
TextView audioDuration;
ImageView btnPlayStop;
MediaPlayer mp;
Handler seekHandler;
int audioCurrentPosition;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final News news = getItem(position);
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.list_item_audio, parent, false);
viewHolder.txtTitle = (TextView) convertView.findViewById(R.id.audioTitle);
viewHolder.seekBar = (SeekBar) convertView.findViewById(seek_bar);
viewHolder.audioDuration = (TextView) convertView.findViewById(R.id.txtDuration);
viewHolder.btnPlayStop = (ImageView) convertView.findViewById(R.id.btnPlayStop);
viewHolder.mp = new MediaPlayer();
viewHolder.seekHandler = new Handler();
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txtTitle.setText(news.getTitle());
viewHolder.mp = MediaPlayer.create(context, R.raw.team);
viewHolder.audioCurrentPosition = news.getAudioPosition();
viewHolder.mp.seekTo(viewHolder.audioCurrentPosition * 1000);
viewHolder.audioDuration.setText(ms);
viewHolder.btnPlayStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( !isPLaying) {
viewHolder.mp.seekTo(viewHolder.audioCurrentPosition * 1000);
//play
isPLaying = true;
viewHolder.btnPlayStop.setImageResource(ic_media_pause);
viewHolder.mp.start();
viewHolder.seekBar.setMax( viewHolder.mp.getDuration() / 1000);
((Activity) context).runOnUiThread(new Runnable() {
#Override
public void run() {
if ( viewHolder.mp != null && viewHolder.mp.isPlaying()) {
int mCurrentPosition = viewHolder.mp.getCurrentPosition() / 1000;
viewHolder.seekBar.setProgress(mCurrentPosition);
viewHolder.seekHandler.postDelayed(this, 1000);
news.setAudioPosition( viewHolder.mp.getCurrentPosition() / 1000);
}
}
});
} else {
//pause
isPLaying = false;
viewHolder.btnPlayStop.setImageResource(ic_media_play);
viewHolder.audioCurrentPosition = viewHolder.mp.getCurrentPosition() / 1000;
viewHolder.mp.pause();
}
}
});
viewHolder.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if ( viewHolder.mp != null && fromUser) {
//Change the seekbar with finger
viewHolder.mp.seekTo(progress * 1000);
viewHolder.audioCurrentPosition = progress * 1000;
}
}
});
viewHolder.mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//When mediaplayer finishes
isPLaying = false;
viewHolder.btnPlayStop.setImageResource(ic_media_play);
viewHolder.audioCurrentPosition = 0;
}
});
return convertView;
}
}
Since you are using ViewHolder pattern, your views are being recycled and hence, your playing-indicator gets reused too...
Release your old MediaPlayer when you a new instance is started. For example:
protected void mediaplayerMethod(String filepath) {
Log.d(TAG, "mediaplayerMethod audio file path " + filepath);
if(mp != null){
mp.release();
}
mp = null;
mp = new MediaPlayer();
mp.setOnCompletionListener(AudioAdapter.this); // Important
seekBar.setOnSeekBarChangeListener(AudioAdapter.this);
utils = new Utilities();
playSong(filepath);
}
EDIT 1
You can try the below code
public class SoundTest extends Activity {
private ListView lv1;
private String lv_arr[]={"test 1","test 2","test 3","test 4","test 5"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item, lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
if (lv1.getItemAtPosition(position)=="test 1") {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.sound1);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (lv1.getItemAtPosition(position)=="test 2") {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.sound2);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
//And the rest of the audio 3,4,5.
}
});
}
}
The global variable MediaPlayer needs to be set private static. This has caught me several times.

Play/pause buttons in Listview are not updating

I am new on Android. I have a listview, which have image, text and play/pause button. When I click on a play button in a row and then click on the play button in another row, the background of both buttons remain pause. So I want that when I click on play button in a row and then click on the play button in another row, the background of first button should change to play and background of second should remain pause. Thanks in advance.
public class MyListAdapter extends ArrayAdapter<String> {
private String[] gelenurl;
List<String> gelen_ad;
public static MediaPlayer mPlayer= new MediaPlayer();
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects, String[] arr) {
super(context, resource, objects);
layout=resource;
gelenurl=arr;
gelen_ad=objects;
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder mainViewholder;
if(convertView==null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView=inflater.inflate(layout, parent, false);
mainViewholder= new ViewHolder();
convertView.setTag(mainViewholder);
}
else {
mainViewholder = (ViewHolder) convertView.getTag();
}
mainViewholder.img = (ImageView) convertView.findViewById(R.id.list_item_thumbnail);
mainViewholder.img.setImageResource(R.drawable.ic_radio);
mainViewholder.title = (TextView) convertView.findViewById(R.id.list_item_text);
mainViewholder.title.setText(getItem(position));
mainViewholder.button = (ImageButton) convertView.findViewById(R.id.list_item_btn);
mainViewholder.button.setImageResource(R.drawable.transparent_play);
mainViewholder.button.setBackgroundColor(Color.WHITE);
mainViewholder.button.setVisibility(View.VISIBLE);
mainViewholder.stop_button = (ImageButton) convertView.findViewById(R.id.list_item_stop_btn);
mainViewholder.stop_button.setImageResource(R.drawable.transparent_stop);
mainViewholder.stop_button.setBackgroundColor(Color.WHITE);
final ViewHolder vh = mainViewholder;
vh.stop_button.setVisibility(View.INVISIBLE);
mainViewholder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vh.button.setVisibility(View.INVISIBLE);
vh.stop_button.setVisibility(View.VISIBLE);
try {
radioLinks(gelenurl[position]);
} catch (IOException e) {
e.printStackTrace();
}
}
public void radioLinks(String city) throws IOException {
if(mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset();
}
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(city);
mPlayer.prepareAsync();
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mPlayer.start();
}
});
}
});
mainViewholder.stop_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vh.button.setVisibility(View.VISIBLE);
vh.stop_button.setVisibility(View.INVISIBLE);
mPlayer.stop();
mPlayer.reset();
}
});
return convertView;
}
}
You will want to track the currently playing song with a member variable. Something like private String currentCity = null. Inside the onClick handler for play, you'll set that to the selected item, and call notifyDataSetChanged to tell the adapter to refresh everything. Inside the stop callback, you'll probably need to set currentCity = null after stopping and again call notifyDataSetChanged()
try {
currentCity = gelenurl[position];
radioLinks(currentCity);
notifyDataSetChanged();
} catch (IOException e) {
e.printStackTrace();
}
You want to use that information inside getView() to decide what to show.
mainViewholder.img = (ImageView) convertView.findViewById(R.id.list_item_thumbnail);
mainViewholder.img.setImageResource(R.drawable.ic_radio);
mainViewholder.title = (TextView) convertView.findViewById(R.id.list_item_text);
mainViewholder.title.setText(getItem(position));
mainViewholder.button = (ImageButton) convertView.findViewById(R.id.list_item_btn);
mainViewholder.button.setImageResource(R.drawable.transparent_play);
mainViewholder.button.setBackgroundColor(Color.WHITE);
...
if ( getItem(position) == currentCity ) {
// show stop button
} else {
/// show play button
}

How to set ImageView setOnClickListner which is in custom listview and play/pause song when Imageview clicked?

I am new to android and i want to play and pause song when ImageView clicked.According to my project I have one custom listview and in this listview i have two ImageView.So,when user select play at that time play the song and select pause at that time pause the song.
Note:- Basically I want to set that onClickListner in my activity class not to custom listview adapter.And i am adding that songs from res\raw folder to arraylist.
Here this my adapter
public class SongAdapter extends BaseAdapter{
Context context;
private ArrayList<SongModel> songsListDat = null;
public ArrayList<SongModel> songsList;
private static LayoutInflater inflater;
public SongAdapter(Activity activity, ArrayList<SongModel> songsListDat) {
this.context = activity;
this.songsListDat = songsListDat;
this.songsList = new ArrayList<SongModel>();
this.songsList.addAll(songsListDat);
}
public void setSelectedIndex(int ind)
{
notifyDataSetChanged();
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public int getCount() {
return songsListDat.size();
}
#Override
public Object getItem(int position) {
return songsListDat.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.ringtone_row_layout, null);
TextView title = (TextView) vi.findViewById(R.id.name); // title
ImageView play = (ImageView)vi.findViewById(R.id.imgPlayPause);
ImageView pause = (ImageView)vi.findViewById(R.id.imgPause);
SongModel song = new SongModel();
song = songsListDat.get(position);
title.setText(song.getSongTitle());
return vi;
}
}
This is activity class :- In this activity i want set onclicklistner for play and pause functionality.
public class MainActivity extends ListActivity {
public static final String TAG = "[MainActivity]";
Activity activity;
SongAdapter songAdapter;
SongsManager songsManager = new SongsManager();
MediaPlayer mp;
// Songs list
public ArrayList<SongModel> songsList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = MainActivity.this;
mp = new MediaPlayer();
SongsManager plm = new SongsManager(activity);
final ArrayList<SongModel> songsListData = songsManager.songList;
this.songsList = plm.getAllSongs();
for (int i = 0; i < songsListData.size(); i++) {
SongModel song = songsListData.get(i);
songsListData.add(song);
}
songAdapter = new SongAdapter(this, songsList);
setListAdapter(songAdapter);
ListView lv = getListView();
}
}
and this is my song model class
public class SongModel {
private String songPath, songTitle;
public SongModel(String songPath, String songTitle) {
this.songPath = songPath;
this.songTitle = songTitle;
}
public SongModel() {
}
public void setSongPath(String songPath){
this.songPath = songPath;
}
public String getSongPath() {
return songPath;
}
public void setSongTitle(String songTitle){
this.songTitle = songTitle;
}
public String getSongTitle() {
return songTitle;
}
}
If anyone know then please help me...
First Add a interface in your adapter class like & use it like this ::
public class SongAdapter extends BaseAdapter {
Context context;
private ArrayList<SongModel> songsListDat = null;
public ArrayList<SongModel> songsList;
public OnMusicListener mListener;
private static LayoutInflater inflater;
public SongAdapter(Activity activity, ArrayList<SongModel> songsListDat, OnMusicListener mListener) {
this.context = activity;
this.mListener = mListener;
this.songsListDat = songsListDat;
this.songsList = new ArrayList<SongModel>();
this.songsList.addAll(songsListDat);
}
public void setSelectedIndex(int ind) {
notifyDataSetChanged();
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public int getCount() {
return songsListDat.size();
}
#Override
public Object getItem(int position) {
return songsListDat.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.ringtone_row_layout, null);
TextView title = (TextView) vi.findViewById(R.id.name); // title
ImageView play = (ImageView) vi.findViewById(R.id.imgPlayPause);
ImageView pause = (ImageView) vi.findViewById(R.id.imgPause);
SongModel song = new SongModel();
song = songsListDat.get(position);
title.setText(song.getSongTitle());
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mListener.onPlayClicked(position);
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mListener.onPauseClicked(position);
}
});
return vi;
}
public interface OnMusicListener {
void onPauseClicked(int position);
void onPlayClicked(int position);
}
}
Then from your activity class
songAdapter = new SongAdapter(this, songsList,this);
setListAdapter(songAdapter);
After "this " added you can now implements the method from interface
from that you can have the click event of each play/pause of the list..
Please tell if needed more help..
So this will be your activity should like this ::
public class MainActivity extends ListActivity implements SongAdapter.OnMusicListener{
public static final String TAG = "[MainActivity]";
Activity activity;
SongAdapter songAdapter;
SongsManager songsManager = new SongsManager();
MediaPlayer mp;
// Songs list
public ArrayList<SongModel> songsList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = MainActivity.this;
mp = new MediaPlayer();
SongsManager plm = new SongsManager(activity);
final ArrayList<SongModel> songsListData = songsManager.songList;
this.songsList = plm.getAllSongs();
for (int i = 0; i < songsListData.size(); i++) {
SongModel song = songsListData.get(i);
songsListData.add(song);
}
songAdapter = new SongAdapter(this,songsList,this);
setListAdapter(songAdapter);
ListView lv = getListView();
}
#Override
public void onPauseClicked(int position){
//Todo-- Add code you want to perform on Pause clicked with specific position
}
#Override
public onPlayClicked(int position){
//Todo-- Add code you want to perform on Play clicked with specific position
}
}
Please try this when you want to set the image src from a ListView item to something else when the ListView item is tapped.
Use view parameter of onItemClick method to change ImageView of selected row:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
ImageView selectedImage = (ImageView) view.findViewById(R.id.buckysImage);
selectedImage.setImageResource(R.drawable.selected);
}
You can simply pass onclick listener to your adapter and set click listener to your image view.
And when you set your adapter from your activity just simply create new click listener as you passing params into your methods.

how to send all the data of array list to another activity?

Daily Screen :-
public class DailyScreen extends AppCompatActivity implements AdapterView.OnItemClickListener {
ImageView add, edit;
MyCustomAdapter adapter1;
Button ok;
Button next2;
final Context context = this;
ListView listView;
private ArrayAdapter<String> adapter;
List<Milk> items=new ArrayList();
public List<Milk> getItems() {
return items;
}
public void setItems(List<Milk> items) {
this.items = items;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daily_listview);
add = (ImageView) findViewById(R.id.add);
edit = (ImageView) findViewById(R.id.edit);
listView = (ListView) findViewById(R.id.list);
next2 = (Button) findViewById(R.id.next2);
Milk milkDefault=new Milk("Toned Milk",0);
items.add(milkDefault);
//this.setArrayList((ArrayList<String>) Arrays.asList(items));
adapter1 = new MyCustomAdapter(items, this);
listView.setAdapter(adapter1);
listView.setOnItemClickListener(this);
registerForContextMenu(listView);// to set context menu in list view
}
#Override
protected void onResume() {
super.onResume();
next2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(DailyScreen.this, SupplierActivity.class);
intent.putExtra("LIST_ITEMS",List,items);//
startActivity(intent);
}
});
add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(DailyScreen.this);
dialog.setTitle("Enter new Milk");
dialog.setContentView(R.layout.dialog_box);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
final EditText editText = (EditText) dialog.findViewById(R.id.pro);
String data = editText.getText().toString();
//button initialization
Button ok = (Button) dialog.findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String data = editText.getText().toString();
if (!data.isEmpty()) {
items.add(new Milk(data,0));
adapter1.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "product name is :" + data, Toast.LENGTH_LONG).show();
dialog.cancel();
}
else{
Toast.makeText(DailyScreen.this, "Please enter the data", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
MyCustomAdapter class
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private List<Milk> list = new ArrayList<Milk>();
private Context context;
public MyCustomAdapter(List<Milk> list, Context context) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_layout, null);
}
TextView name = (TextView) view.findViewById(R.id.text);
name.setText(list.get(position).getNsme());
final TextView milkCount = (TextView) view.findViewById(R.id.milkcount);
milkCount.setText(""+list.get(position).getAmount());
ImageView increment = (ImageView) view.findViewById(R.id.add);
ImageView decrement = (ImageView) view.findViewById(R.id.sub_item);
increment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int a = Integer.parseInt(milkCount.getText().toString().trim());
a = a + 1;
milkCount.setText("" + a);
list.get(position).setAmount(a);
}
});
decrement.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int a = Integer.parseInt(milkCount.getText().toString().trim());
if (a == 0) {
a = 0;
} else {
a = a - 1;
}
milkCount.setText("" + a);
list.get(position).setAmount(a);
}
});
return view;
}
Milk class
public class Milk {
private String nsme ;
private int amount ;
public Milk(String nsme, int amount) {
this.nsme = nsme;
this.amount = amount;
}
public String getNsme() {
return nsme;
}
public void setNsme(String nsme) {
this.nsme = nsme;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
i have tried all thing to send all the data of list view to another activty please suggest me how can i do it ??
Make the list static and u can then access it in any activity. Make sure u initialise the list
milk class extends serializable firstly
then u do
make object of milk class in mainactivity class
Milk milk=new Milk(this);
Intent i=new Intent(getApplicationContext(),Update.class);
i.putExtra("emnumber",(Serializable)milk);
startActivity(i);
then u can pick the value in update class
if(getIntent().getSerializableExtra("emnumber")!=null) {
con = (ArrayList<Contact>) getIntent().getSerializableExtra("emnumber");
email_mobile = contactList.get(0).nsme;
pass__word = contactList.get(0).amount;
you can implement as:
public class Milk implements Parcelable{
......
}
Send arraylist as:
intent.putExtra("files", "your arraylist");
obtain arraylist as:
Intent inIntent = getIntent();
list=inIntent.getParcelableArrayListExtra("files");
Try this. May be it help.
Send arraylist to activity through extras.
intent.putStringArrayListExtra("ARRAY_LIST", arrayList);
Retrieve arraylist in activity.
arrayList= i.getStringArrayListExtra("ARRAY_LIST");
If u have to send arraylist other than string then.
intent.putParcelableArrayListExtra("ARRAY_LIST", (ArrayList<Milk>) milkArrayList);
Milk class must be parcellable

Android listview showing only last element

i created custom listview with text and two buttons, i set up arraylist and adapter but my listview is showing every element as last, for ex. if i add 3 elements: "text1","text2","text3" my listview shows "text3", "text3" "text3" and i dont have any idea why.
private ListView lista;
private List<Piosenka> listaPiosenek;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.editText1);
lista = (ListView) findViewById(R.id.listView1);
lista.setClickable(true);
}
public void update_listy() throws MalformedURLException, IOException
{
final List<Piosenka> listaPiosenek = new ArrayList<Piosenka>();
listaPiosenek.add(new Piosenka("text1"));
listaPiosenek.add(new Piosenka("text2"));
listaPiosenek.add(new Piosenka("text3"));
PiosenkaAdapter adapter = new PiosenkaAdapter(this, listaPiosenek);
lista.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long index)
{
System.out.println("sadsfsf");
}
});
lista.setAdapter(adapter);
}
Edit: PiosenkaAdapter code
public class PiosenkaAdapter extends BaseAdapter implements OnClickListener {
private Context context;
private List<Piosenka> listapiosenek;
public PiosenkaAdapter(Context context, List<Piosenka> listapiosenek) {
this.context = context;
this.listapiosenek = listapiosenek;
}
public int getCount() {
return listapiosenek.size();
}
public Object getItem(int position) {
return listapiosenek.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
Piosenka element = listapiosenek.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_element, null);
}
TextView tvTytul = (TextView) convertView.findViewById(R.id.tvTytul);
tvTytul.setText(Piosenka.getTytul());
Button btnPobierz = (Button) convertView.findViewById(R.id.btnPobierz);
btnPobierz.setFocusableInTouchMode(false);
btnPobierz.setFocusable(false);
btnPobierz.setTag(element);
Button btnPlay = (Button) convertView.findViewById(R.id.btnPlay);
btnPlay.setFocusableInTouchMode(false);
btnPlay.setFocusable(false);
btnPlay.setOnClickListener(this);
btnPlay.setTag(element);
// btnRemove.setId(position);
return convertView;
}
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.btnPobierz:
Piosenka entry = (Piosenka) view.getTag();
listapiosenek.remove(entry);
notifyDataSetChanged();
break;
case R.id.btnPlay:
entry = (Piosenka) view.getTag();
listapiosenek.remove(entry);
notifyDataSetChanged();
break;
}
}
}
Try this...
lista.setAdapter(adapter);
adapter.notifyDataSetChanged();
Can you paste you PiosenkaAdapter's code?
I don't know your language, but the Piosenka variable is fetched correctly in getView()
Piosenka element = listapiosenek.get(position);
But this looks strange to me
TextView tvTytul = (TextView) convertView.findViewById(R.id.tvTytul);
tvTytul.setText(Piosenka.getTytul());
Piosenka.getTytul() looks to me as a static method call, where you should do a regular method call to element.getTytul() instead.

Categories

Resources