我正在做我的第一个项目。我需要个建议。我的应用程序应向用户显示运动,当用户点击左侧的“播放”图标时,应用程序应按意图移至youtube到视频,以向用户展示如何正确进行运动。这是一些代码。
构造函数
public class Constructor {
private String mExcersiseName;
private String mBodyPart;
private String mYoutubeLink;
public Constructor(String excersiseName, String bodyPart, String youtubeLink){
mExcersiseName = excersiseName;
mBodyPart = bodyPart;
mYoutubeLink = youtubeLink;
}
public String getExcersiseName(){
return mExcersiseName;
}
public String getBodyPart(){
return mBodyPart;
}
public String getYoutubeLink(){return mYoutubeLink;}
}
适配器
public class ExerciseAdapter extends ArrayAdapter<Constructor>{
String mYoutubeLink;
public ExerciseAdapter(Activity context, ArrayList<Constructor> constructorList) {
super(context, 0, constructorList);
mYoutubeLink = youtubeLink;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem = convertView;
if (listItem == null) {
listItem = LayoutInflater.from(getContext()).inflate(R.layout.items_for_list, parent, false);
}
Constructor actualPosition = getItem(position);
TextView exerciseName = (TextView) listItem.findViewById(R.id.list_excersise_name);
exerciseName.setText(actualPosition.getExcersiseName());
TextView bodyPart = (TextView) listItem.findViewById(R.id.list_body_part);
bodyPart.setText(actualPosition.getBodyPart());
return listItem;
}
}
我们在其中放置运动名称,身体部位和youtube链接等数据的Java类
public class BicepsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
ArrayList<Constructor> bicepsExercises = new ArrayList<Constructor>();
bicepsExercises.add(new Constructor("Lorem ipsum","Lorem","link"));
bicepsExercises.add(new Constructor("Lorem ipsum","Lorem","link"));
bicepsExercises.add(new Constructor("Lorem ipsum","Lorem","link"));
bicepsExercises.add(new Constructor("Lorem ipsum","Lorem","link"));
bicepsExercises.add(new Constructor("Lorem ipsum","Lorem","link"));
bicepsExercises.add(new Constructor("Lorem ipsum","Lorem","link"));
bicepsExercises.add(new Constructor("Lorem ipsum","Lorem","link"));
ExerciseAdapter adapter = new ExerciseAdapter(this,bicepsExercises);
ListView list = (ListView) findViewById(R.id.list_item);
list.setAdapter(adapter);
}
}