【发布时间】:2018-09-11 08:50:52
【问题描述】:
我在我的 Android 应用程序中创建了一个ViewPager,这是代码
public class TestPagerAdapter extends PagerAdapter {
Context context;
List<PagerModel> pagerArr;
LayoutInflater inflater;
public TestPagerAdapter(Context context, List<PagerModel> pagerArr) {
this.context = context;
this.pagerArr = pagerArr;
inflater = ((Activity)context).getLayoutInflater();
}
@Override
public int getCount() {
return pagerArr.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view= inflater.inflate(R.layout.pop_up,container,false);
TextView text_task=view.findViewById(R.id.textView8);
TextView text_child_parent=view.findViewById(R.id.textView9);
TextView edit_child_parent=view.findViewById(R.id.textView10);
TextView text_deadline=view.findViewById(R.id.textView12);
TextView text_avancement=view.findViewById(R.id.textView14);
Button button_update=view.findViewById(R.id.button3);
Spinner s = view.findViewById(R.id.spinner2);
ImageView rl=view.findViewById(R.id.imm11);
view.setTag(position);
((ViewPager)container).addView(view);
PagerModel model=pagerArr.get(position);
text_task.setText(model.getText_task());
text_avancement.setText(model.getText_avancement());
text_deadline.setText(model.getText_deadline());
text_child_parent.setText(model.getText_child_parent());
edit_child_parent.setText(model.getEdit_child_parent());
button_update.setText(model.get_button_text());
return view;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==((View)object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView((View)object);
}
}
当我单击button_update 时,我想使用Retrofit 来使用Web 服务,如何在我的MainActivity 中访问此按钮?或者我可以从TestPagerAdapter 使用我的网络服务吗?
这是MainActivity
final CaldroidListener listener = new CaldroidListener() {
@Override
public void onSelectDate(Date date, View view) {
final Dialog dialog=new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.pager_layout);
final LayoutInflater factory = getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.pop_up, null);
final Button button_update = textEntryView.findViewById(R.id.button3);
final LayoutInflater factory2 = getLayoutInflater();
final View textEntryView2 = factory.inflate(R.layout.pop_up, null);
final Spinner s = textEntryView.findViewById(R.id.spinner2);
String d= DateFormat.format("dd-MM-yyyy", date.getTime()).toString();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(getString(R.string.BaseUrl))
.addConverterFactory(GsonConverterFactory.create())
.build();
final MyInterface myInterface=retrofit.create(MyInterface.class);
Call<List<Task>> call = myInterface.getTask(d);
call.enqueue(new Callback<List<Task>>() {
@Override
public void onResponse(Call<List<Task>> call, Response<List<Task>> response) {
final List<Task> TaskList = response.body();
if(!TaskList.isEmpty()){
List<PagerModel> pageArr = new ArrayList<>();
for(int i=0;i<TaskList.size();i++){
pageArr.add(new PagerModel(TaskList.get(0).getDescription(),"Parent",TaskList.get(0).getOwner(),TaskList.get(0).getDeadline(),TaskList.get(0).getAvancement(),"Update Task"));
}
TestPagerAdapter adapter44=new TestPagerAdapter(MainActivity.this,pageArr);
AutoScrollViewPager pager= dialog.findViewById(R.id.pager);
pager.setAdapter(adapter44);
CirclePageIndicator indicator=dialog.findViewById(R.id.page_indicator);
indicator.setViewPager(pager);
indicator.setCurrentItem(0);
dialog.show();}
else Toast.makeText(getApplicationContext(),"il y'a pas de task pour cette date",Toast.LENGTH_SHORT).show();
////////////update
button_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"woooow",Toast.LENGTH_SHORT).show();
Call<Success> call22= myInterface.UpdateTask(TaskList.get(0).getId(),s.getSelectedItem().toString());
call22.enqueue(new Callback<Success>() {
@Override
public void onResponse(Call<Success> call, Response<Success> response) {
Success success = response.body();
int s=success.getCode();
if(s==0) Toasty.error(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();
else {
Toasty.success(getApplicationContext(),"Success update",Toast.LENGTH_SHORT).show();
dialog.dismiss(); }
}
@Override
public void onFailure(Call<Success> call, Throwable t) {
Toast.makeText(getApplicationContext(),"failure",Toast.LENGTH_SHORT).show();
}
});
}
});
}
@Override
public void onFailure(Call<List<Task>> call, Throwable t) {
Toasty.error(getApplicationContext(),"failure",Toast.LENGTH_SHORT).show();
}
});
但我无法访问button_update
【问题讨论】:
标签: android android-viewpager retrofit android-pageradapter