[英]Android Custom ListView OnClick even on an image from custom adapter
But the problem is while playing the audio if anybody click on other activity the sound does not stop. 但是问题是在播放音频时,如果有人单击其他活动,声音不会停止。
here is my activity class 这是我的活动课
public class WordActivity extends ActionBarActivity {
categories category_obj;
word word_obj;
wordDB wordDb;
ViewFlipper viewFlipper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word);
TextView txt = (TextView) findViewById(R.id.phraseListHeading);
Typeface font = Typeface.createFromAsset(WordActivity.this.getAssets(), "fonts/NotoSans-Regular.ttf");
String categoryName = getIntent().getExtras().getString("categoryName").toUpperCase();
category_obj = (categories) getIntent().getSerializableExtra("category_obj");
txt.setTypeface(font);
txt.setText(categoryName);
ListView wordListView;
wordListView = (ListView)findViewById(R.id.list_view_word);
wordListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = parent.getItemAtPosition(position);
word_obj = (word) o;
if(Integer.valueOf(word_obj.getCategoryId())>=0) {
Intent myIntent = new Intent(WordActivity.this, WordDetailsActivity.class);
myIntent.putExtra("word_obj", word_obj);
myIntent.putExtra("position",position);
myIntent.putExtra("currentClickedId", word_obj.getCsvWordId().toString());
myIntent.putExtra("favouriteFlag",0);
myIntent.putExtra("searchFlag",0);
myIntent.putExtra("searchString", "");
WordActivity.this.startActivity(myIntent);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_word, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
protected void onResume(){
super.onResume();
GlobalState state = ((GlobalState) getApplicationContext());
state.doAction();
wordDb = new wordDB(WordActivity.this);
getAllWords();
}
public void getAllWords(){
ArrayList<word> words = new ArrayList<word>();
category_obj = (categories) getIntent().getSerializableExtra("category_obj");
Cursor row = wordDb.selectWordList(category_obj.getCsvCategoryId().toString());
words.add(new word("-1","-1","","","","","","","x.mp3",""));
words.add(new word("-2","-2","","","","","","","x.mp3",""));
row.moveToFirst();
while (!row.isAfterLast()) {
//Log.d("Data id: ", row.getString(2));
words.add( new word(row.getString(0),row.getString(1),row.getString(2),row.getString(3),row.getString(4),row.getString(5),row.getString(6),row.getString(7),row.getString(8),row.getString(9)));
row.moveToNext();
}
row.close();
WordAdapter adapter = new WordAdapter(WordActivity.this, words);
ListView listView = (ListView) findViewById(R.id.list_view_word);
listView.setAdapter(adapter);
}
}
here is my adapter class 这是我的适配器类
class WordAdapter extends ArrayAdapter {
ArrayList<word> words = new ArrayList<word>();
private Context mContext;
MediaPlayer mediaPlayer;
public WordAdapter(Context context, ArrayList<word> list) {
super(context, R.layout.words_list, list);
mContext = context;
words = list;
}
public View getView(int position, View convertView, final ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.words_list, null);
TextView txt = (TextView) view.findViewById(R.id.list_view_words);
TextView txt2 = (TextView) view.findViewById(R.id.translated_text);
TextView txt3 = (TextView) view.findViewById(R.id.pronounce);
Typeface font = Typeface.createFromAsset(mContext.getAssets(), "fonts/NotoSans-Regular.ttf");
txt.setTypeface(font);
txt2.setTypeface(font);
txt3.setTypeface(font);
}
else {
view = convertView;
}
if(Integer.valueOf(words.get(position).getCsvWordId())>=0) {
View divbottom = (View) view.findViewById(R.id.phrase_list_bottom_divider);
divbottom.setVisibility(View.VISIBLE);
ImageView playAudio = (ImageView) view.findViewById(R.id.phraselist_play_audio);
playAudio.setVisibility(View.VISIBLE);
} else {
View divbottom = (View) view.findViewById(R.id.phrase_list_bottom_divider);
divbottom.setVisibility(View.GONE);
ImageView playAudio = (ImageView) view.findViewById(R.id.phraselist_play_audio);
playAudio.setVisibility(View.GONE);
}
TextView contactNameView = (TextView) view.findViewById(R.id.list_view_words);
Integer mainTextlen = words.get(position).getMainText().length();
contactNameView.setText( words.get(position).getMainText() );
if(mainTextlen > 40)
{
TextView txt = (TextView) view.findViewById(R.id.list_view_words);
txt.setTextSize(14);
}
TextView translatedText = (TextView) view.findViewById(R.id.translated_text);
translatedText.setText(words.get(position).getTranslationText());
Integer transTextlen = words.get(position).getTranslationText().length();
if(transTextlen > 40)
{
TextView txt2 = (TextView) view.findViewById(R.id.translated_text);
txt2.setTextSize(14);
}
TextView pronounceText = (TextView) view.findViewById(R.id.pronounce);
pronounceText.setText(words.get(position).getPronunciation_in_english());
Integer pronounceTextlen = words.get(position).getPronunciation_in_english().length();
if(pronounceTextlen > 40)
{
TextView txt3 = (TextView) view.findViewById(R.id.pronounce);
txt3.setTextSize(12);
}
/*get full sound file name*/
Integer fileNameLength = words.get(position).getAudio().toString().length();
String fileName = words.get(position).getAudio().toString();
final String soundFile = fileName.substring(0, fileNameLength - 4);
final ImageView imgPlay = (ImageView) view.findViewById(R.id.phraselist_play_audio);
imgPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
if(mediaPlayer == null) {
final int position = listView.getPositionForView(parentRow);
try {
Uri mp3 = Uri.parse("android.resource://" + mContext.getPackageName() + "/raw/" + soundFile);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(mContext, mp3);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
imgPlay.setImageResource(R.drawable.playactive);
// mediaPlayer.setOnCompletionListener(onCompletionListener);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.release();
mediaPlayer = null;
imgPlay.setImageResource(R.drawable.playinactive);
}
});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
return view;
}
}
But the problem is while playing the audio if anybody click on other activity the sound does not stop
但是问题是在播放音频时,如果有人单击其他活动,声音不会停止
Create a getter method in WordAdapter
which return current playing MediaPlayer
instance and call it inside onItemClick
before starting next Activity like: 在
WordAdapter
创建一个getter方法,该方法返回当前正在播放的MediaPlayer
实例,并在启动下一个Activity之前在onItemClick
调用它,例如:
public MediaPlayer getMPlayerInstace(){
return this.mediaPlayer;
}
Now call stop
method in onItemClick
: 现在在
onItemClick
调用stop
方法:
MediaPlayer mPlayer;
mPlayer=adapter.getMPlayerInstace();
if(mPlayer!=null){
mPlayer.stop();
mPlayer.release();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.