目前,我有3个阵列。我想要几个小时。我可以以任何动态方式实现此功能。对我来说For循环不起作用。有没有办法动态实现这个。如果你们帮忙解决这个问题,我将很高兴。
switch(pageNumber){
case 0:
final ListView list1 = (ListView)rootView.findViewById(R.id.list);
BinderData bindingData = new BinderData(this.getActivity(), d[0]);
list1.setAdapter(bindingData);
list1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (d[0].get(position).getItemType() == 0
|| d[0].get(position).getItemType() == 3
|| d[0].get(position).getItemType() == 2)
return;
Intent intent = new Intent(Schedule.this.getActivity(),
ContentExtended.class);
intent.putExtra("title", d[0].get(position).getTitle());
intent.putExtra("content", d[0].get(position).getContent());
startActivity(intent);
}
});
break;
case 1:
final ListView list2 = (ListView)rootView.findViewById(R.id.list);
BinderData bindingData1 = new BinderData(this.getActivity(), d[1]);
list2.setAdapter(bindingData1);
list2.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (d[1].get(position).getItemType() == 0
|| d[1].get(position).getItemType() == 3
|| d[1].get(position).getItemType() == 2)
return;
Intent intent = new Intent(Schedule.this.getActivity(),
ContentExtended.class);
intent.putExtra("title", d[1].get(position).getTitle());
intent.putExtra("content", d[1].get(position).getContent());
startActivity(intent);
}
});
break;
case 2:
final ListView list3 = (ListView)rootView.findViewById(R.id.list);
BinderData bindingData2 = new BinderData(this.getActivity(), d[2]);
list3.setAdapter(bindingData2);
list3.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (d[2].get(position).getItemType() == 0
|| d[2].get(position).getItemType() == 3
|| d[2].get(position).getItemType() == 2)
return;
Intent intent = new Intent(Schedule.this.getActivity(),
ContentExtended.class);
intent.putExtra("title", d[2].get(position).getTitle());
intent.putExtra("content", d[2].get(position).getContent());
startActivity(intent);
}
});
break;
}
答案 0 :(得分:2)
在我看来,除了d
数组的索引之外,整个代码都被复制了。所以,它会是这样的:
public void method(int pageNumber) {
final ListView list = (ListView)rootView.findViewById(R.id.list);
BinderData bindingData = new BinderData(this.getActivity(), d[pageNumber]);
list.setAdapter(bindingData);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (d[pageNumber].get(position).getItemType() == 0
|| d[pageNumber].get(position).getItemType() == 3
|| d[pageNumber].get(position).getItemType() == 2)
return;
Intent intent = new Intent(Schedule.this.getActivity(),
ContentExtended.class);
intent.putExtra("title", d[pageNumber].get(position).getTitle());
intent.putExtra("content", d[pageNumber].get(position).getContent());
startActivity(intent);
}
});
}
答案 1 :(得分:0)
on onClick check on pageNumber我看到了,在每种情况下,我看到的唯一区别是数组d的索引。在onClick方法中尝试这个:
final ListView list1 = (ListView)rootView.findViewById(R.id.list);
BinderData bindingData = new BinderData(this.getActivity(), d[pageNumber]);
list1.setAdapter(bindingData);
list1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (d[0].get(position).getItemType() == 0
|| d[pageNumber].get(position).getItemType() == 3
|| d[pageNumber].get(position).getItemType() == 2)
return;
Intent intent = new Intent(Schedule.this.getActivity(),
ContentExtended.class);
intent.putExtra("title", d[pageNumber].get(position).getTitle());
intent.putExtra("content", d[pageNumber].get(position).getContent());
startActivity(intent);
}
});
答案 2 :(得分:0)
您无法使用loops
进行切换。你可以使用if
。因为我看到你的代码只在数组d索引中的每种情况下只有区别。所以你不想切换或者也不想。
public void onClick(int pageNumber) {
//your implementaion
}
现在您可以将页码传递给此方法