如何编写代码以成功加载数组中的下一个数据?

huangapple go评论106阅读模式
英文:

How do you program a button to successfully load the next data in an array

问题

我已经创建了一个按钮,当按下它时,它会加载数组中的下一个链接,但唯一的问题是它不会更新视图页容器。我已经为nextprevious都创建了按钮,但我觉得我可能漏掉了一个小细节,因为这种方法不会将数据加载到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&lt;&gt;();
        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 &lt; 0) {
                    Log.d(&quot;Newest Data Present&quot;,&quot;YUH&quot;);
                } else {
                    URL = list.get(int_of_inital_chapter - 1).getLink();
                    lstData = new ArrayList&lt;&gt;();
                    getData();
                }

            }
        });

        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (int_of_inital_chapter + 1 &gt; list.size()) {
                    Log.d(&quot;Oldest Chapter Enabled&quot;,&quot;YUH&quot;);
                } else {
                    URL = list.get(int_of_inital_chapter + 1).getLink();
                    lstData = new ArrayList&lt;&gt;();
                    getData();
                }
            }
        });

PageViewAdapter Contructor

 public PageViewAdapter(Context context, List&lt;Page&gt; 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);

huangapple
  • 本文由 发表于 2020年8月5日 04:57:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63254934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定