public
Created

  • Download Gist
MyArrayAdapter.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
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 };
 
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);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String s = mClasses[position];
holder.textView.setText(s);
// this if/else block should be improved but I don't know your exact code
if (s.startsWith("First")) {
holder.bt1.setBackgroundResource(R.drawable.ic_launcher);
holder.bt2.setBackgroundResource(R.drawable.ic_launcher);
holder.bt3.setBackgroundResource(R.drawable.ic_launcher);
holder.bt4.setBackgroundResource(R.drawable.ic_launcher);
holder.bt5.setBackgroundResource(R.drawable.ic_launcher);
} else if (s.startsWith("Second")) {
holder.bt1.setBackgroundResource(R.drawable.ic_launcher);
holder.bt2.setBackgroundResource(R.drawable.ic_launcher);
holder.bt3.setBackgroundResource(R.drawable.ic_launcher);
holder.bt4.setBackgroundResource(R.drawable.ic_launcher);
holder.bt5.setBackgroundResource(R.drawable.ic_launcher);
} else if (s.startsWith("Third")) {
holder.bt1.setBackgroundResource(R.drawable.ic_launcher);
holder.bt2.setBackgroundResource(R.drawable.ic_launcher);
holder.bt3.setBackgroundResource(R.drawable.ic_launcher);
holder.bt4.setBackgroundResource(R.drawable.ic_launcher);
holder.bt5.setBackgroundResource(R.drawable.ic_launcher);
} else if (s.startsWith("Fourth")) {
holder.bt1.setBackgroundResource(R.drawable.ic_launcher);
holder.bt2.setBackgroundResource(R.drawable.ic_launcher);
holder.bt3.setBackgroundResource(R.drawable.ic_launcher);
holder.bt4.setBackgroundResource(R.drawable.ic_launcher);
holder.bt5.setBackgroundResource(R.drawable.ic_launcher);
} else if (s.startsWith("Fifth")) {
holder.bt1.setBackgroundResource(R.drawable.ic_launcher);
holder.bt2.setBackgroundResource(R.drawable.ic_launcher);
holder.bt3.setBackgroundResource(R.drawable.ic_launcher);
holder.bt4.setBackgroundResource(R.drawable.ic_launcher);
holder.bt5.setBackgroundResource(R.drawable.ic_launcher);
}
holder.textView.setTag(Integer.valueOf(position));
holder.textView.setOnClickListener(new OnClickListener() {
 
@Override
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.setTag(Integer.valueOf(position));
holder.bt1.setOnClickListener(mListener);
holder.bt2.setTag(Integer.valueOf(position));
holder.bt2.setOnClickListener(mListener);
holder.bt3.setTag(Integer.valueOf(position));
holder.bt3.setOnClickListener(mListener);
holder.bt4.setTag(Integer.valueOf(position));
holder.bt4.setOnClickListener(mListener);
holder.bt5.setTag(Integer.valueOf(position));
holder.bt5.setOnClickListener(mListener);
return convertView;
}
 
static class ViewHolder {
TextView textView;
Button bt1, bt2, bt3, bt4, bt5;
 
}
 
private OnClickListener mListener = new OnClickListener() {
 
@Override
public void onClick(View v) {
Integer realPos = (Integer) v.getTag();
int video = 0;
String night = null;
for (int i = 0; i < ids.length; i++) {
if (v.getId() == ids[i]) {
video = i;
break;
}
}
Intent i = new Intent(mContext, My_videos.class);
i.putExtra("video", video);
i.putExtra("row", realPos);
mContext.startActivity(i);
}
};
 
}
My_videos.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
public class My_videos extends Activity {
 
private VideoView vid;
// the multidimensional array of videos
private int[][] rowVideos = {
{ R.raw.row1Video1, R.raw.row1Video2, R.raw.row1Video3,
R.raw.row1Video4, R.raw.row1Video5 },
{ R.raw.row2Video1, R.raw.row2Video2, R.raw.row2Video3,
R.raw.row2Video4, R.raw.row2Video5 },
{ R.raw.row3Video1, R.raw.row3Video2, R.raw.row3Video3,
R.raw.row3Video4, R.raw.row3Video5 },
{ R.raw.row4Video1, R.raw.row4Video2, R.raw.row4Video3,
R.raw.row4Video4, R.raw.row4Video5 },
{ R.raw.row5Video1, R.raw.row5Video2, R.raw.row5Video3,
R.raw.row5Video4, R.raw.row5Video5 } };
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.misc_video);
Bundle bdl = getIntent().getExtras();
// we know the button that was clicked in a row
int video = bdl.getInt("video");
// now we also know the row of the list where the Video was clicked
int row = bdl.getInt("row");
vid = (VideoView) findViewById(R.id.videoView1);
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + rowVideos[row][video]));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start();
}
}
extra stuff.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
// I don't know what you're trying to do, right now you set the
// same drawable to the all Buttons no matter what row you're in
 
// I see several possibilities:
// 1. you are trying to set the same image for all buttons but the image should be different for each row, in this case
// create an array to hold the drawables for each row in the adapter class:
int[] rowDrawables = {R.drawable.row1, R.drawable.row2, R.drawable.row3, R.drawable.row4, R.drawable.row5};
 
//then in the getView method replace the if/else clause with something like this:
 
int rowDr = rowDrawables[position];
holder.bt1.setBackgroundResource(rowDr);
holder.bt2.setBackgroundResource(rowDr);
holder.bt3.setBackgroundResource(rowDr);
holder.bt4.setBackgroundResource(rowDr);
holder.bt5.setBackgroundResource(rowDr);
 
// 2. the second possiblity is that you have different images for each button for each row.
// in this case you would create a multidimensional array like this:
 
int[][] rowDrawables = { {R.drawable.row1Button1, R.drawable.row1Button2, R.drawable.row1Button3, R.drawable.row1Button4, R.drawable.row1Button5},
{R.drawable.row2Button1, R.drawable.row2Button2, R.drawable.row2Button3, R.drawable.row2Button4, R.drawable.row2Button5},
{R.drawable.row3Button1, R.drawable.row3Button2, R.drawable.row3Button3, R.drawable.row3Button4, R.drawable.row3Button5},
{R.drawable.row4Button1, R.drawable.row4Button2, R.drawable.row4Button3, R.drawable.row4Button4, R.drawable.row4Button5},
{R.drawable.row5Button1, R.drawable.row5Button2, R.drawable.row5Button3, R.drawable.row5Button4, R.drawable.row5Button5}};
 
// then in the getView method you'll replace the if/else with
 
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]);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.