2016-10-03 37 views
-3

列出從外部存儲讀取的所有歌曲。在點擊一首歌曲時,應該轉到SecondActivity.java如何在單擊列表視圖時調用另一個活動

MainActivity.java 

public class MainActivity extends ListActivity { 
    public final static String EXTRA_MESSAGE = "com.example.shubham.hymnattune"; 
    private List peers = new ArrayList(); 
    private MainActivity.MediaCursorAdapter mediaAdapter = null; 
    private String currentFile = ""; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null); 
     if (null != cursor) { 
      cursor.moveToFirst(); 
     } 
} 

    @Override 
    protected void onListItemClick(ListView list, View view, int position, long id) { 
     super.onListItemClick(list, view, position, id); 

     currentFile = (String) view.getTag(); 
     Intent intent=new Intent(this,SecondActivity.class); 
     intent.putExtra(EXTRA_MESSAGE,currentFile); 
     startActivity(intent); 

    } 

    private class MediaCursorAdapter extends SimpleCursorAdapter { 

     public MediaCursorAdapter(Context context, int layout, Cursor c) { 
      super(context, layout, c, 
        new String[]{MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION}, 
        new int[]{R.id.displayname, R.id.title, R.id.duration}); 
     } 

     @Override 
     public void bindView(View view, Context context, Cursor cursor) { 
      TextView title = (TextView) view.findViewById(R.id.title); 
      TextView name = (TextView) view.findViewById(R.id.displayname); 
      TextView duration = (TextView) view.findViewById(R.id.duration); 

      name.setText(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE))); 

      long duratioInMs = Long.parseLong(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION))); 

      double durationInMin = ((double) duratioInMs/1000.0)/60.0; 

      durationInMin = new BigDecimal(Double.toString(durationInMin)).setScale(2, BigDecimal.ROUND_UP).doubleValue(); 

      duration.setText("" + durationInMin); 

      view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA))); 
     } 

     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent) { 
      LayoutInflater inflator = LayoutInflater.from(context); 

      View v = inflator.inflate(R.layout.listitem, parent, false); 
      bindView(v, context, cursor); 
      return v; 
     } 
    } 
}; 

接收在MainActivity.java中選擇的歌曲並播放它。

SecondActivity.java 

public class SecondActivity extends AppCompatActivity { 

    private static final int UPDATE_FREQUENCY = 500; 
    private static final int STEP_VALUE = 4000; 


    //private MainActivity.MediaCursorAdapter mediaAdapter = null; 
    private TextView selectedFile = null; 
    private SeekBar seekBar = null; 
    private MediaPlayer player = null; 
    private ImageButton playButton = null; 
    private ImageButton prevButton = null; 
    private ImageButton nextButton = null; 

    private boolean isStarted = true; 
    private String currentFile = ""; 
    private boolean isMoveingSeekBar = false; 

    private final Handler handler = new Handler(); 
    // private final IntentFilter intentFilter = new IntentFilter(); 

    private final Runnable updatePositionRunnable = new Runnable() { 
     public void run() { 
      updatePosition(); 
     } 
    }; 
    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.second); 

     selectedFile = (TextView) (findViewById(R.id.selectedfile)); 
     seekBar = (SeekBar) (findViewById(R.id.seekbar)); 
     playButton = (ImageButton) (findViewById(R.id.play)); 
     prevButton = (ImageButton) (findViewById(R.id.prev)); 
     nextButton = (ImageButton) (findViewById(R.id.next)); 

     player = new MediaPlayer(); 

     player.setOnCompletionListener(onCompletion); 
     player.setOnErrorListener(onError); 
     seekBar.setOnSeekBarChangeListener(seekBarChanged); 

     Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null); 

     if (null != cursor) { 
      cursor.moveToFirst(); 

      playButton.setOnClickListener(onButtonClick); 
      prevButton.setOnClickListener(onButtonClick); 
      nextButton.setOnClickListener(onButtonClick); 
     } 

     Intent intent=getIntent(); 
     currentFile=intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 
     startPlay(currentFile); 

    } 


This is my main layout file. containing a list of songs from external storage. 

activity_main.xml 

<?xml version="1.0" encoding="utf-8"?> 
<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="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context="com.example.shubham.hymnattune.MainActivity"> 

    <ListView 
      android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1.0"/> 
</LinearLayout> 

這是我的第二個活動的xml文件,用於播放歌曲。 second.xml

<?xml version="1.0" encoding="utf-8"?> 
<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="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context="com.example.shubham.hymnattune.SecondActivity"> 


<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="10dp" 
     android:background="@android:drawable/screen_background_light" 
     android:id="@+id/linear2" 
     android:layout_alignParentStart="true"> 

     <TextView 
      android:id="@+id/selectedfile" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:ellipsize="middle" 
      android:gravity="center_horizontal" 
      android:singleLine="true" 
      android:text="No File Selected" 
      android:textColor="@android:color/black" /> 

     <SeekBar 
      android:id="@+id/seekbar" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:max="100" 
      android:paddingBottom="10dp"/> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:drawable/screen_background_light" 
      android:gravity="center" 
      android:orientation="horizontal"> 

      <ImageButton 
       android:id="@+id/prev" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@android:drawable/ic_media_previous"/> 

      <ImageButton 
       android:id="@+id/play" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@android:drawable/ic_media_play"/> 

      <ImageButton 
       android:id="@+id/next" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@android:drawable/ic_media_next"/> 
     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 
+2

只需使用Google搜索。 '在列表項目點擊開始新活動',你會得到你的答案。 –

+4

可能重複[如何打開新的活動點擊列表視圖中的項目?](http://stackoverflow.com/questions/19662233/how-open-new-activity-clicking-an-item-in-listview) – IIIIIIIIIIIIIIIIIIIIII

+0

@SHUBHAM DHINGRA檢查我的答案 –

回答

0

試試這個

ListView list = (ListView) findViewById(R.id.list); 
list.setOnItemClickListener(new AdapterView.onItemClickListener() { 
@Override 
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) { 
    Intent intent=new Intent(this,SecondActivity.class); 
    startActivity(intent); 
} 
}); 
0

我用一個代碼我的適配器類的內部對項目的點擊來實現。檢查該代碼

public class LazyAdapter extends BaseAdapter { 

private VideoActivity mainactivity; 
private String[] result,imageId,title; 
private static LayoutInflater inflater=null; 

Context context; 

public LazyAdapter(VideoActivity mainactivity, String[] videourls, String[] imgurls, String [] explist) { 
    context = mainactivity; 
    result = videourls; 
    imageId=imgurls; 
    title = explist; 
    inflater = (LayoutInflater)mainactivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    // imageLoader=new ImageLoader(mainactivity.getApplicationContext()); 
} 

public int getCount() { 
    return title.length; 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 


public class Holder 
{ 
    TextView tv; 
    ImageView img; 
} 

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

    Holder holder=new Holder(); 
    View rowView; 
    rowView = inflater.inflate(R.layout.listview, null); 
    holder.tv=(TextView) rowView.findViewById(R.id.textView1); 
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1); 
    Glide.with(context) 
      .load(imageId[position]) 
      .into(holder.img); 

    holder.tv.setText(title[position]); 
    // imageLoader.DisplayImage(result[position], holder.img); 
    rowView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      // Toast.makeText(context, "You Clicked "+title[position], Toast.LENGTH_LONG).show(); 

      Intent intent = new Intent(context, VideoActivity2.class); 
      intent.putExtra("imgid",title[position]); 
      intent.putExtra("videourl",result[position]); 
      context.startActivity(intent); 

     } 
    }); 
    return rowView; 
} 

}

在這段代碼中,我還添加了一個代碼,在點擊列表視圖中的每個項目時傳遞數據..希望這一個他lps you

相關問題