我有一个listactivity app,由5行组成,每行包含一个TEXT和5个BUTTON, 文本和按钮都是可点击的,文本将打开MyDay活动,这是textview,当你点击任何按钮它必须打开视频视图屏幕,它的工作正常如下图所示:
但我的问题是:
我可以通过每行中的5个按钮分配五个不同的视频。
我试图为每个按钮设置不同的视频,所以:
25按钮打开25个不同的视频。
我的意思是:
第一个行中的按钮(一个)将打开 video_1 第一个行中的按钮(两个)将打开 video_2 第一个行中的按钮(三个)将打开 video_3 第一个行中的按钮(四个)将打开 video_4 第一个行中的按钮(五个)将打开 video_5 第二行中的按钮(一个)将打开 video_6 第二行中的按钮(两个)将打开 video_7等到第五行的最后一个按钮
我无法实现这一点。
任何建议都将受到赞赏
感谢
我的代码:
ListButtons类:
public class ListButtons extends ListActivity {
String classes[] = {"First", "Second", "Third","Fourth", "Fifth" };
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new MyArrayAdapter(this, classes)); }}
MyArrayAdapter类:
public class MyArrayAdapter extends ArrayAdapter<String> {
private final String[] mClasses;
private Context mContext;
private Typeface tf;
private LayoutInflater mInflater;
private String[] mData = { "button1", "button2", "button3", "button4",
"button5" };
private int[] ids = { R.id.button1, R.id.button2, R.id.button3,
R.id.button4, R.id.button5 };
int[][] rowDrawables = {
{R.drawable.row_1, R.drawable.row_2, R.drawable.row_3,
R.drawable.row_4, R.drawable.row_5},
{R.drawable.row_6, R.drawable.row_7, R.drawable.row_8,
R.drawable.row_9, R.drawable.row_10},
{R.drawable.row_11, R.drawable.row_12, R.drawable.row_13,
R.drawable.row_14, R.drawable.row_15},
{R.drawable.row_16, R.drawable.row_17, R.drawable.row_18,
R.drawable.row_19, R.drawable.row_20},
{R.drawable.row_21, R.drawable.row_22, R.drawable.row_23,
R.drawable.row_24, R.drawable.row_25}};
public MyArrayAdapter(Context context, String[] classes) {
super(context, 0, classes);
mClasses = classes;
mContext = context;
tf = Typeface.createFromAsset(context.getAssets(), "BFantezy.ttf");
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null, true);
holder = new ViewHolder();
holder.textView = (TextView) convertView
.findViewById(R.id.row_label);
holder.textView.setTypeface(tf);
holder.bt1 = (Button) convertView.findViewById(R.id.button1);
holder.bt2 = (Button) convertView.findViewById(R.id.button2);
holder.bt3 = (Button) convertView.findViewById(R.id.button3);
holder.bt4 = (Button) convertView.findViewById(R.id.button4);
holder.bt5 = (Button) convertView.findViewById(R.id.button5);
} else {
holder = (ViewHolder) convertView.getTag();
}
String s = mClasses[position];
holder.textView.setText(s);
int[] rowDr = rowDrawables[position];
holder.bt1.setBackgroundResource(rowDr[0]);
holder.bt2.setBackgroundResource(rowDr[1]);
holder.bt3.setBackgroundResource(rowDr[2]);
holder.bt4.setBackgroundResource(rowDr[3]);
holder.bt5.setBackgroundResource(rowDr[4]);
holder.textView.setTag(Integer.valueOf(position));
holder.textView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Integer pos = (Integer) v.getTag();
Intent ourIntent = new Intent(mContext, MyDay.class);
ourIntent.putExtra("cheese", mClasses[pos]);
mContext.startActivity(ourIntent);
}
});
holder.bt1.setOnClickListener(mListener);
holder.bt2.setOnClickListener(mListener);
holder.bt3.setOnClickListener(mListener);
holder.bt4.setOnClickListener(mListener);
holder.bt5.setOnClickListener(mListener);
return convertView;
}
static class ViewHolder {
TextView textView;
Button bt1, bt2, bt3, bt4, bt5;
}
private OnClickListener mListener = new OnClickListener() {
public void onClick(View v) {
String night = null;
for (int i = 0; i < ids.length; i++) {
if (v.getId() == ids[i]) {
night = mData[i];
break;
}
}
Intent i = new Intent(mContext, My_videos.class);
i.putExtra("video", night);
mContext.startActivity(i);
}
};
}
My_videos班级:
public class My_videos extends Activity {
private VideoView vid;
String night;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
Bundle bdl = getIntent().getExtras();
night = bdl.getString("video");
vid = (VideoView) findViewById(R.id.videoView1);
if (night.equalsIgnoreCase("button1")) {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_1));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); }
else if (night.equalsIgnoreCase("button2")) {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_2));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); }
else if (night.equalsIgnoreCase("button3")) {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_3));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); }
else if (night.equalsIgnoreCase("button4")) {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_4));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); }
else if (night.equalsIgnoreCase("button5")) {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_5));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); }
}}
更新
我尝试修改My_videos类,如下面的代码,但结尾为为每个按钮显示相同的视频,即(video_5)。
public class My_videos extends Activity {
private VideoView vid;
String night;
private int position;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
Bundle bdl = getIntent().getExtras();
night = bdl.getString("video");
vid = (VideoView) findViewById(R.id.videoView1);
if(position == 0){
night.equalsIgnoreCase("button1"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_1));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); }
if(position == 0){
night.equalsIgnoreCase("button2"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_2));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 0){
night.equalsIgnoreCase("button3"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_3));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 0){
night.equalsIgnoreCase("button4"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_4));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 0){
night.equalsIgnoreCase("button5"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_5));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); }}
else if(position == 1){
night.equalsIgnoreCase("button1"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_6));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 1){
night.equalsIgnoreCase("button2"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_7));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 1){
night.equalsIgnoreCase("button3"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_8));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 1){
night.equalsIgnoreCase("button4"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_9));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 1){
night.equalsIgnoreCase("button5"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_10));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); }}
else if(position == 2){
night.equalsIgnoreCase("button1"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_11));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 2){
night.equalsIgnoreCase("button2"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_12));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 2){
night.equalsIgnoreCase("button3"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_13));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 2){
night.equalsIgnoreCase("button4"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_14));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 2){
night.equalsIgnoreCase("button5"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_15));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); }}
else if(position == 3){
night.equalsIgnoreCase("button1"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_16));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 3){
night.equalsIgnoreCase("button2"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_17));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 3){
night.equalsIgnoreCase("button3"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_18));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 3){
night.equalsIgnoreCase("button4"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_19));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 3){
night.equalsIgnoreCase("button5"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_20));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); }}
else if(position == 4){
night.equalsIgnoreCase("button1"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_21));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 4){
night.equalsIgnoreCase("button2"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.video_22));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 4){
night.equalsIgnoreCase("button3"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_23));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position ==4){
night.equalsIgnoreCase("button4"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_24));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); } }
if(position == 4){
night.equalsIgnoreCase("button5"); {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.video_25));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start(); }}
}}
答案 0 :(得分:1)
在此链接https://gist.github.com/3463215我发布了一个My_videos
类的版本,可以执行您想要的操作。基本上,在MyArrayAdapter
课程中点击Button
时,您会在Intent
中发送点击该按钮的行+在该行中点击的确切Button
。然后在My_videos
类中,您将获得这些数字,并在数组数组结构数组中轻松找到正确的视频。
答案 1 :(得分:0)
你有5个按钮,25个链接,5行,如果你设置5个监听器(对于每个按钮1)你可以选择这样的uri:
button1,button2,button3,button4,button5;
button1.setOnclickListener(new View.onClickListener{
public void onClick(View v){
Integer pos = (Integer) v.getTag();
Intent ourIntent = new Intent(mContext, MyDay.class);
ourIntent.putExtra("cheese", mClasses[rowPosition*5+buttonPostion]);
mContext.startActivity(ourIntent);
}
});
= getView(position)
中的rowPosition =是简单的转换数字,按钮位置是您的v.getTag()
按钮数,现在为1。
播出白色你可以选择25个链接:当你在你的第2行中点击时,第3个按钮,链接ID将是2 * 5 + 3 = 13。所以在你的secund行中,3 buttom将获得13个链接。
答案 2 :(得分:0)
您可以获得所有视频的相同视频,所有两个视频的视频相同,因为所有行中的所有视频ID都相同,等等。
所以,在onClick中执行此操作:
public void onClick(View v) {
String night = null;
int position = yourListView.getPositionForView((View) v.getParent())
for (int i = 0; i < ids.length; i++) {
if (v.getId() == ids[i]) {
night = mData[i];
break;
}
}
}
现在也将此位置添加为您的意图的额外。这样您就可以知道单击哪一行等等。