英文:
How do you program a button to successfully load the next data in an array
问题
我已经创建了一个按钮,当按下它时,它会加载数组中的下一个链接,但唯一的问题是它不会更新视图页容器。我已经为next
和previous
都创建了按钮,但我觉得我可能漏掉了一个小细节,因为这种方法不会将数据加载到viewpager中。
以下是代码;
lstData = new ArrayList<>();
getData();
ViewPager myrv = (ViewPager) findViewById(R.id.view_page);
myViewPager = new PageViewAdapter(this, lstData);
myrv.setAdapter(myViewPager);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (int_of_inital_chapter - 1 < 0) {
Log.d("Newest Data Present","YUH");
} else {
URL = list.get(int_of_inital_chapter - 1).getLink();
lstData = new ArrayList<>();
getData();
}
}
});
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (int_of_inital_chapter + 1 > list.size()) {
Log.d("Oldest Chapter Enabled","YUH");
} else {
URL = list.get(int_of_inital_chapter + 1).getLink();
lstData = new ArrayList<>();
getData();
}
}
});
PageViewAdapter构造函数
public PageViewAdapter(Context context, List<Page> PageList) {
this.context = context;
this.PageList = PageList;
inflater = LayoutInflater.from(context);
}
算法的工作方式是当活动创建时,它会自动加载分配给输入URL的数据。这个部分已经正常工作。但是我想要的是让按钮加载列表数组中URL的前一个或后一个链接。我漏掉了什么?
英文:
I have created a button, so that, when pressed it will load the next link in the array, but the only problem is that it doesn't update the view pager container. I have created a button for both next
and previous
, but I feel like I am missing a small detail, because this method does not load data into the viewerpager
Here is the code;
lstData = new ArrayList<>();
getData();
ViewPager myrv = (ViewPager) findViewById(R.id.view_page);
myViewPager = new PageViewAdapter(this, lstData);
myrv.setAdapter(myViewPager);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (int_of_inital_chapter - 1 < 0) {
Log.d("Newest Data Present","YUH");
} else {
URL = list.get(int_of_inital_chapter - 1).getLink();
lstData = new ArrayList<>();
getData();
}
}
});
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (int_of_inital_chapter + 1 > list.size()) {
Log.d("Oldest Chapter Enabled","YUH");
} else {
URL = list.get(int_of_inital_chapter + 1).getLink();
lstData = new ArrayList<>();
getData();
}
}
});
PageViewAdapter Contructor
public PageViewAdapter(Context context, List<Page> PageList) {
this.context = context;
this.PageList = PageList;
inflater = LayoutInflater.from(context);
}
The algorithm works so that when the activity is created it automatically loads the data assigned to the inputted URL. Which Works. But I want to have the buttons load whatever is next or before the URL in the list array. What am I missing?
答案1
得分: 0
尝试这样做:
在全局声明以下内容:
private ViewPager myrv;
private ViewPagerAdapter myViewPager;
然后初始化它们:
myrv = (ViewPager) findViewById(R.id.view_page);
myViewPager = new PageViewAdapter(this, lstData);
并且在按钮的onClick事件中添加以下内容:
myViewPager = new PageViewAdapter(this, lstData);
myrv.setAdapter(myViewPager);
英文:
Try this:
declare the following globally:
private ViewPager myrv;
private ViewPagerAdapter myViewPager;
and then initialize them:
myrv = (ViewPager) findViewById(R.id.view_page);
myViewPager = new PageViewAdapter(this, lstData);
and also put the following in the buttons onClick
myViewPager = new PageViewAdapter(this, lstData);
myrv.setAdapter(myViewPager);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论