我正在尝试制作日常计划的应用程序,我每天都会使用片段。他们每个人都有CustomAdapter,而且重复性太强。我尝试了一切,但我找不到从不同阶层使用它的方法。
public class Day1Fragment extends Fragment {
final String FILE_NAME="day1fragment.txt";
FileOutputStream fos=null;
private ArrayList<String> exerciseName=new ArrayList<String>();
private ArrayList<String> exerciseDesc=new ArrayList<String>();
private ArrayList<Integer> exerciseSet=new ArrayList<Integer>();
private ArrayList<Integer> exerciseRepeat=new ArrayList<Integer>();
CustomAdapter customAdapter=new CustomAdapter();
ListView day1List;
ImageButton addButton;
Dialog addDialog,fitnessDialog;
public static Day1Fragment newInstance() {
return new Day1Fragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
addDialog=new Dialog(view.getContext());
fitnessDialog=new Dialog(view.getContext());
day1List=(ListView)view.findViewById(R.id.day1list);
addButton=(ImageButton)view.findViewById(R.id.addButton);
FitnessClass.fragmentInit(view,day1List,customAdapter,addButton,addDialog,fitnessDialog,exerciseName,exerciseDesc,exerciseSet,exerciseRepeat,FILE_NAME,this.getContext(),fos);
}
//TODO CHANGE LISTS
class CustomAdapter extends BaseAdapter{
@Override
public int getCount() {
return exerciseName.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position,View convertView, ViewGroup parent) {
convertView=getLayoutInflater().inflate(R.layout.fitness_layout,null);
TextView exerciseNameText=(TextView)convertView.findViewById(R.id.exerciseLayoutText);
TextView setsText=(TextView)convertView.findViewById(R.id.setsText);
TextView repeatsText=(TextView)convertView.findViewById(R.id.repeatsText);
exerciseNameText.setText(exerciseName.get(position));
setsText.setText(exerciseSet.get(position).toString());
repeatsText.setText(exerciseRepeat.get(position).toString());
return convertView;
}
}
那么我如何使用来自不同类的CustomAdapter类呢?当我试图在不同的类中使用它时,我在这部分中遇到错误:
convertView=getLayoutInflater().inflate(R.layout.fitness_layout,null);
答案 0 :(得分:0)
您可以在程序包中创建名为Java Class
的新CustomAdapter
,并将CustomAdapter
代码放在那里而不是Day1Fragment.java
内。这样,您就可以在任何地方拨打CustomAdapter
,Day1Fragment.java
看起来更干净,代码更少。