I'm following some online tutorial and it is about how to play an audio in the listview with onItemCLickLIstener, but the problem is i have done the exact work but it is not working properly. The audio plays when i'm clicking on image but it doesn't do anything when clicking on rest of the screen.
public class NmbersActivity extends AppCompatActivity{
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
final ArrayList<Word> word = new ArrayList<Word>();
word.add(new Word("One", "Lutti",R.drawable.number_one,R.raw.number_one));
word.add(new Word("Two", "Otlika",R.drawable.number_two,R.raw.number_two));
word.add(new Word("Three", "Tolookosu",R.drawable.number_three,R.raw.number_three));
word.add(new Word("Four", "Oyyisa",R.drawable.number_four,R.raw.number_four));
word.add(new Word("Five", "Massokka",R.drawable.number_five,R.raw.number_five));
word.add(new Word("Six", "Temmoka",R.drawable.number_six,R.raw.number_six));
word.add(new Word("Seven", "Kenekaku",R.drawable.number_seven,R.raw.number_seven));
word.add(new Word("Eight", "Kawinta",R.drawable.number_eight,R.raw.number_eight));
word.add(new Word("Nine", "Wo'e",R.drawable.number_nine,R.raw.number_nine));
word.add(new Word("Ten", "Na'aacha",R.drawable.number_ten,R.raw.number_ten));
WordAdapter Adapter = new WordAdapter(this,word,R.color.category_number);
ListView activityText = findViewById(R.id.commanIntent);
activityText.setAdapter(Adapter);
activityText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Word words = word.get(position);
mediaPlayer = MediaPlayer.create(NmbersActivity.this,words.getmAudioResource());
mediaPlayer.start();
}
});
}
}
Here is the LIstView File
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Here is the custom layout for single list item .
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/tan_background"
android:minHeight="@dimen/list_item_height"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="@dimen/list_item_height"
android:layout_height="@dimen/list_item_height" />
<LinearLayout
android:id="@+id/text_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="@+id/miwok_text_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:textStyle="bold"
tools:text="lutti" />
<TextView
android:id="@+id/default_text_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
tools:text="one" />
</LinearLayout>
Here is the wordAdapter file.
public class WordAdapter extends ArrayAdapter<Word> {
private int mColorPrimary;
public WordAdapter(Context ActivityName, ArrayList<Word> word, int
background) {
super(ActivityName,0,word);
mColorPrimary = background;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null)
{
listItemView = LayoutInflater.from(getContext()).inflate
(R.layout.list_item, parent, false);
}
Word defaultView = getItem(position);
final TextView miwokTextView =
listItemView.findViewById(R.id.mivokTrans);
miwokTextView.setText(defaultView.getMovik());
final TextView defaultTextView =
listItemView.findViewById(R.id.defaultTrans);
defaultTextView.setText(defaultView.getDefault());
ImageView imageView = listItemView.findViewById(R.id.image);
if(defaultView.hasImage()) {
imageView.setImageResource(defaultView.getmImageResourseId());
}
else{
imageView.setVisibility(View.GONE);
}
View colorView = listItemView.findViewById(R.id.linear_item);
colorView.setBackgroundResource(mColorPrimary);
return listItemView;
}
Here is the Word class.
public class Word{
private String mDeafaultLanguage;
private String mMovikLanguage;
private int mImageResourseId = NO_IMAGE;
public static final int NO_IMAGE = -1;
public Word(String defaultLanguage,String movikLanguage){
mDeafaultLanguage = defaultLanguage;
mMovikLanguage = movikLanguage;
}
public Word(String defaultLanguage,String movikLanguage, int
imageResource){
mDeafaultLanguage = defaultLanguage;
mMovikLanguage = movikLanguage;
mImageResourseId = imageResource;
}
public String getDefault(){
return mDeafaultLanguage;
}
public String getMovik() {
return mMovikLanguage;
}
public int getmImageResourseId() {
return mImageResourseId;
}
public boolean hasImage(){
return NO_IMAGE != mImageResourseId;
}
}
If you want to know more details there is the link of the exact app https://github.com/udacity/ud839_Miwok/tree/43a1596a48521beee160f937988a5c564519dbb5