0

확인, 이것이 내 문제입니다. 나는 인터넷 전체를 검색하여 많은 답변을 찾았지만 아무도 나를 위해 일하지 않았습니다.어떻게 단추 ListView (리터럴 사운드 응용 프로그램처럼)를 만들 수 있습니까

그래서 저는 50 개 이상의 ImageButton이 각각 다른 Sound를 재생할 수있는 ListView로 Activity를 만드는 방법을 알고 싶습니다.

이 주요 활동 (즉, 버튼을 것이다)입니다 :

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" > 
 

 
    <TextView 
 
     android:id="@+id/list_item_string" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_centerVertical="true" 
 
     android:layout_alignParentLeft="true" 
 
     android:paddingLeft="8dp" 
 
     android:paddingRight="8dp" 
 
     android:textSize="18sp" 
 
     android:textStyle="bold" /> 
 

 
    <ImageButton 
 
     android:id="@+id/play_btn" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignParentRight="true" 
 
     android:background="#00000000" 
 
     android:src="@drawable/f_button_s" 
 
     android:layout_centerVertical="true" 
 
     android:layout_marginRight="5dp" /> 
 

 
</RelativeLayout>
:이 목록보기의 각 라인에 대한 내 사용자 지정 레이아웃

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
 
    android:paddingRight="@dimen/activity_horizontal_margin" 
 
    android:paddingTop="@dimen/activity_vertical_margin" 
 
    android:paddingBottom="@dimen/activity_vertical_margin" 
 

 
    tools:context="com.werewolve.freaksound.sounds" 
 
    android:background="@drawable/f_background_fit"> 
 

 
    <ImageView 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:id="@+id/imageView" 
 
     android:layout_alignParentTop="true" 
 
     android:layout_centerHorizontal="true" 
 
     android:layout_marginTop="5dp" 
 
     android:src="@drawable/f_logo" /> 
 

 
    <ListView 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:id="@+id/soundList" 
 
     android:layout_below="@+id/imageView" 
 
     android:layout_centerHorizontal="true" 
 
     android:layout_marginTop="15dp" /> 
 
</RelativeLayout>

입니다

각 버튼은 onClick "play_btn"과 다른 사운드를 재생하고 문자열 "list_item_string"이있는 사운드에 따라 텍스트를 재생합니다. 이 같은

뭔가 :


은 * (웃음 소리) * (재생 버튼) ***는


당신이 좀 도와 주시겠습니까?

+0

맞춤 어댑터를 사용하고 있습니까? – Leigh

답변

2

당신은과 같이 사용자 정의 어댑터를 만들어야합니다

public class PlaySoundsAdapter extends BaseAdapter implements View.OnClickListener { 

    SoundExample[] sounds; 
    Activity context; 
    PlaySoundAlert soundPlayerAlert; 

    public PlaySoundsAdapter(Activity context, SoundExample[] soundsArray) { 
     this.context = context; 
     this.sounds = soundsArray; 

     // Hooks up the PlaySoundAlert.PlaySound in MainActivity 
     this.soundPlayerAlert = (PlaySoundAlert)context; 
    } 

    @Override 
    public int getCount() { 
     return sounds == null ? 0 : sounds.length; 
    } 

    @Override 
    public Object getItem(int i) { 
     return sounds[i]; 
    } 

    @Override 
    public long getItemId(int i) { 
     return 0; 
    } 

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 

     SoundExample item = (SoundExample)getItem(i); 

     if (view == null) // reuse existing view 
      view = context.getLayoutInflater().inflate(R.layout.custom_sound_layout, 
        viewGroup, false); 

     // Set the TextView to the name of the sound 
     TextView t = (TextView)view.findViewById(R.id.txtName); 
     t.setText(item.getSoundName()); 

     // Set the tag of the button to the sound resource id (uri) 
     Button b = (Button)view.findViewById(R.id.play_btn); 
     b.setTag(item.getSoundUri()); 

     // When the button is clicked, play the associated sound 
     b.setOnClickListener(this); 

     return view; 
    } 

    @Override 
    public void onClick(View view) { 
     Button b = (Button) view; 
     if (b != null) { 
      int soundUri = (int)b.getTag(); 

      // Notify listener (MainActivity) to play the required sound 
      if (soundPlayerAlert != null) { 
       soundPlayerAlert.playSound(soundUri); 
      } 
     } 
    } 
} 

다음이를 만들 당신이 당신의 활동에 구현할 수있는 인터페이스는 다음과 같이 사운드를 재생하기 :

public interface PlaySoundAlert { 
    public void playSound(int uri); 
} 

당신은 내가 위에서 만든 Adapter가 이벤트를 해고하려면이 인터페이스를 사용하여 볼 수 있듯이 필요한 사운드를 연주하십시오. 다음

public class SoundExample { 

    private int soundUri; 
    private String soundName; 

    public String getSoundName() { 
     return soundName; 
    } 

    public void setSoundName(String soundName) { 
     this.soundName = soundName; 
    } 

    public int getSoundUri() { 
     return soundUri; 
    } 

    public void setSoundUri(int soundUri) { 
     this.soundUri = soundUri; 
    } 
} 

그리고이 사용하는 내부 사용자의 Activity 또는 Fragment 사용 :

public class MainActivity extends Activity implements PlaySoundAlert { 

    ListView lstSounds; 
    PlaySoundsAdapter soundsAdapter; 
    SoundExample[] mySounds; 

    // Media player for playing sounds 
    MediaPlayer mp; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     lstSounds = (ListView) findViewById(R.id.soundList); 

     // Create sound list & add two SoundExample objects 
     mySounds = new SoundExample[2]; 
     SoundExample s1 = new SoundExample(); 
     SoundExample s2 = new SoundExample(); 

     // Set sound one to a beep 
     s1.setSoundName("Beep Sound"); 
     s1.setSoundUri(R.raw.beep); 

     // Set sound two to an applause sound 
     s2.setSoundName("Applause Sound"); 
     // NOTE: I am using a sound titled applause.mp3 inside a folder called "raw" 
     s2.setSoundUri(R.raw.applause); 

     // Add sounds to the list 
     mySounds[0] = s1; 
     mySounds[1] = s2; 

     // Instantiate the adapter and apply to the ListView 
     soundsAdapter = new PlaySoundsAdapter(this, mySounds); 
     lstSounds.setAdapter(soundsAdapter); 
    } 

    @Override 
    public void playSound(int uri) { 
     // Play sound 
     mp = MediaPlayer.create(this, uri); 
     if (!mp.isPlaying()) 
      mp.start(); 
    } 
} 

을 그리고 그 모든 당신이 필요로해야한다

귀하의 SoundExample 클래스는이 같은 것을 좋아할 것!

+0

내 앱에 코딩 한 코드를 모두 추가했습니다.응용 프로그램을 컴파일하지 못하게 한 몇 가지 사항을 변경하고 finnally compiled. 그러나 텍스트 및 사운드 경로가있는 새 행을 목록에 어떻게 추가합니까? – ElAlonnso

+0

@ElAlonnso 내 업데이트를 참조하십시오. }'SoundExample soundUri'를 int로 변경했습니다. 사운드 또는 미디어 파일을'raw' 디렉토리에 추가해야합니다. – Leigh

+0

여기 : 확인을 클릭하면 다음 오류가 표시됩니다. (13, 37) 오류 : PlaySoundAlert 기호 클래스를 찾을 수 없습니다. 오류 : (47, 5) 오류 : 메서드가 수퍼 유형의 메서드를 재정의하거나 구현하지 않습니다. 일부 개인 수업이 공개로 변경 되었기 때문에 그 사람들이 저에게 "오류"를 보내 왔기 때문일 수 있습니다 ... .. AND : Button b = view.findViewById (R.id.play_btn); (호환되지 않는 유형 Android.Widget.Button 및 Android.View.View) – ElAlonnso

0

해결 방법은 TextViewImageButton을 모두 포함하는 사용자 지정 ListView 개체를 만드는 것입니다. 다음 사용자 지정 Adapter을 사용하여 이러한 개체를 ListView에 추가합니다. ImageButton의 대안은 단순히 일반 이미지를 사용하여 Clickable으로 만드는 것입니다. (사용자 정의 Adapter을 만드는 방법 포함) 사용자 정의 객체를 표시 할 수 ListView 만들기위한 좋은 예는 여기에서 찾을 수 있습니다 : http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/