0

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

Shoaib Mirza
  • 153
  • 3
  • 12

1 Answers1

0

Please, try to set clickable true for the textview.

android:clickable="true"
minhazur
  • 4,928
  • 3
  • 25
  • 27
  • I have noticed you have added the click listener to the listview, not to the textview. Shouldn't it be the textview? @ShoaibMirza – minhazur Oct 26 '18 at 06:20
  • sir if i set it on textView it won't work....it is set onItemClicklistener which works only on listView. – Shoaib Mirza Oct 28 '18 at 06:58
  • textView is not clickable by default, you have to make it clickable first then set onClickListener to it. @ShoaibMirza – minhazur Oct 28 '18 at 07:10
  • but i have to take the position of the listview when it is clicked. I want to play different sound at every position,i will add the full code above,do check it once. – Shoaib Mirza Oct 28 '18 at 07:19
  • you can get the position of that item from the adapter, please do check RecyclerView and RecyclerView.Adapter. @ShoaibMirza – minhazur Oct 28 '18 at 09:53
  • so you are trying to say that the problem is with the listView and not my code?? – Shoaib Mirza Nov 01 '18 at 14:43
  • Please, check this answer, Especially how button click listener is handled. https://stackoverflow.com/a/31555765/2797847 – minhazur Nov 01 '18 at 18:07