2017-07-06 46 views
0

我創建保存玩家列表中選擇一個MainActivity names.When我點擊到LIST_ITEM需要setTag()方法爲每個點擊的項目,那應該叫SubActivity.In SubActivity如何實現MediaPlayer以及如何獲取這些Tag。如何設置setTag()方法爲每個的listItem ListView中

MainActivity:

public class MainActivity extends AppCompatActivity { 

    String[] audioTitels; 
    ListView listView; 

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

     Resources res = getResources(); 
     audioTitels = res.getStringArray(R.array.audio_list); 

     listView = (ListView) findViewById(R.id.listView); 


     AudioAdapter audioAdapter = new AudioAdapter(this,audioTitels); 
     listView.setAdapter(audioAdapter); 
     listView.setOnItemClickListener(new 
AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

       switch (i){ 

        case 0 : 

         System.out.println("Value of song0 :"+ audioTitels[i]); 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 1: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 2: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 3: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 4: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 5: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 6: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 7: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 8: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 9: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 10: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 11: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 12: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 13: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 14: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 15: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 16: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 17: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 18: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 19: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 20: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 21: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 22: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 23: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 24: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 25: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 26: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 27: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 28: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 29: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 30: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 31: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 32: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 33: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 34: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 35: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 36: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 37: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 38: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 39: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 40: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 

        case 41: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 42: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 43: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 44: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 45: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 46: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 47: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 48: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 49: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 50: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 51: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 52: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 53: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 54: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 55: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 56: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 57: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 58: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 59: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 60: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 61: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 62: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 63: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 64: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 65: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 66: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 67: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 68: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 69: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 70: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 71: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 72: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 73: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 74: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 75: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 76: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 77: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 78: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 79: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 80: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 81: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 82: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 83: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 84: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 85: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 86: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 87: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 88: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 89: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 90: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 91: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 92: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 93: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 94: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 95: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 96: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 97: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 98: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 99: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 100: 
         System.out.println("Value of song100 :"+ audioTitels[i]); 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 101: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 102: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 103: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 104: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 105: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 106: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 107: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 108: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 109: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 110: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 111: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 112: 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 
        case 113: 
         System.out.println("Value of song113 :"+ audioTitels[i]); 

         Toast.makeText(MainActivity.this,"Song to Play",Toast.LENGTH_LONG).show(); 
         break; 

    } 
} 
      }); 
    } 

    class AudioAdapter extends ArrayAdapter<String>{ 

     Context context; 
     String[] myTitles; 

     AudioAdapter(Context c,String[] titles){ 


     super(c,R.layout.single_audio,R.id.audioText,titles); 


      this.context = c; 
      this.myTitles = titles; 

     } 



     @Override 
     public View getView(int position, View convertView, ViewGroup 
parent) { 

      View row = convertView; 
      if(row == null){ 

       LayoutInflater layoutInflater = (LayoutInflater) 
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       row = 
layoutInflater.inflate(R.layout.single_audio,parent,false); 


      } 

      //getting refernce to the textview 
      TextView myTitle = (TextView) 
row.findViewById(R.id.audioText); 


      myTitle.setText(myTitles[position]); 

myTitle.setAnimation(AnimationUtils.loadAnimation(context,R.anim.list_animation)); 

      return row; 
     } 
    } 

} 

activity_main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.xyz.listviewaudio.MainActivity" 
    android:weightSum="1"> 

    <ListView 
     android:id="@+id/listView" 
     android:layout_width="wrap_content" 
     android:layout_height="430dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_weight="0.88"> 

    </ListView> 
</LinearLayout> 

single_audio.xml:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="16dp" 
     android:layout_marginTop="16dp" 
     android:textAlignment="gravity" 
     android:layout_marginBottom="16dp" 
     android:id="@+id/audioText"/> 

</RelativeLayout> 
+0

你能解釋一下爲什麼你在你的交換機需要113'cases'? – pskink

+0

@pskink我有這樣的url_path http://xyzt.com/plugins/quran/001.mp3。我需要設置標籤這樣setTag(「001.mp3」)到(「114.mp3」),並讓這些標籤並連接這些標籤的URL的子活動發揮祈禱。 –

+0

只需使用'setTag(String.format(「%03d」,i)+「.mp3」)'。它會給你你的電話號碼的3位數字符串 –

回答

0

您可以在getView這樣執行的單擊事件,

 @Override 
       public View getView(int position, View convertView, ViewGroup 
     parent) { 

        View row = convertView; 
        if(row == null){ 

         LayoutInflater layoutInflater = (LayoutInflater) 
     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         row = 
     layoutInflater.inflate(R.layout.single_audio,parent,false); 


        } 

        //getting refernce to the textview 
        TextView myTitle = (TextView) 
     row.findViewById(R.id.audioText); 


        myTitle.setText(myTitles[position]); 

     myTitle.setAnimation(AnimationUtils.loadAnimation(context,R.anim.list_animation)); 

// ========= Permform Click Event =================== 
    row.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        v.setTag((position+1)+".mp3"); 
        // You can do your code here .... 
       } 
      }); 

        return row; 
       } 
+0

,如何在Subactivity中獲取該標籤? –

+0

@noushadmohammed v.setTag(position);像這樣,您可以輕鬆設置每個項目的標籤。您可以根據需要設置任意位置的位置。 –

+0

我需要將setTag(「001.mp3」)這樣的標籤設置爲(「114.mp3」)。 –

相關問題