Passing data properly in android?
I have this in my first activity:
private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
// Now we want to actually get the data location of the file
String [] proj={MEDIA_DATA};
// We request our cursor again
_cursor = managedQuery(_contentUri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
// We want to get the column index for the data uri
int count = _cursor.getCount();
//
_cursor.moveToFirst();
//
_columnIndex = _cursor.getColumnIndex(MEDIA_DATA);
// Lets move to the selected item in the cursor
_cursor.moveToPosition(position);
// And here we get the filename
String filename = _cursor.getString(_columnIndex);
//*********** You can do anything when you know the file path :-)
showToast(filename);
Intent i = new Intent("com.ave.EDITORSCREEN");
i.putExtra("mnt/sdcard-ext", _ID);
startActivity(i);
}
This is not the full code, but as the full code gathers all the video thumbnails from the sd card and displays them along with their path's (in toasts). I want to be able to click a thumbnail and have the data passed on to the next activity to be played, stopped, paused, etc. You can see where I have passed the data in the first activity:
public class Editor extends Activity {
ImageButton video1;
int isClicked = 0;
ImageButton audio;
int isClicked1 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.editor);
video1 = (ImageButton) findVi开发者_高级运维ewById(R.id.video1);
video1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (isClicked == 0) {
video1.setImageResource(R.drawable.video_pressed);
isClicked = 1;
} else {
video1.setImageResource(R.drawable.video1);
isClicked = 0;
}
}
});
audio = (ImageButton) findViewById(R.id.audio);
audio.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (isClicked1 == 0) {
audio.setImageResource(R.drawable.audio_pressed);
isClicked1 = 1;
} else {
audio.setImageResource(R.drawable.audio);
isClicked1 = 0;
}
}
});
}
}
I imagine to get the data I will need to put
String data = getIntent().getStringExtra("mnt/sdcard-ext");
But where the onCreate method? Or is this even the right way to get the passed data? And lastly how can I can I play the video? Is there some sort of code for say a video player? If so where would I place this in my final activity?
You do not have to do it in the onCreate()
method, but why not? Where else would you put it?
If you are looking for any sort of documentation, I would recommend checking out the Android Developers Java(Android)Doc. The Android Developers have a great site in general, all about Android. I think a good place to start with Android Audio/Video is here.
Generally, you would set up the layout of your MediaPlayer interface (Play/Pause buttons and views) in onCreate()
and also setup your click/touch listeners to be executed when input is received.
For a first programming course, it looks like you are doing pretty well. Good Luck!
精彩评论