我需要帮助,以找到一种根据用户选择更改RecyclerView内容的方法。我正在制作一个Android应用,默认情况下显示来自abc新闻的新闻,但我想让用户选择其他来源,例如cnn等。我在微调器中有此选项,并在其中叫onItemSelected()
可以获取用户选择的来源。使用新的源,我调用loadNews(Source)方法,该方法向新闻api发出请求并获取数据。我将发布我的代码,希望有人能提供帮助。截至目前,当我打开新闻活动时,它会从abc加载新闻,但是当我选择另一个来源时,它不会从其他来源重新加载数据,它将保持相同的内容!
活动代码
private String Source ="abc-news";
private Spinner spinner;
private List<Article> articles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
// getting the news service
newsService = Common.getNewsService();
recyclerView = findViewById(R.id.recyclerView);
kbv = findViewById(R.id.kbv);
swipeRefreshLayout = findViewById(R.id.swipeRefresh);
spinner =(Spinner)findViewById(R.id.drop_Down_News_Source);
loadSpinner();
// originally load from abc news and if user wants to change they can select from the list and reload the news
// initialize the recycler
loadNews(this.Source);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@TargetApi(Build.VERSION_CODES.P)
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
public void onRefresh() {
loadNews(Source);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
}
},4000);
}
});
}
private void loadSpinner() {
ArrayAdapter<CharSequence> myAdapter = ArrayAdapter.createFromResource(this,R.array.News_Sources, android.R.layout.simple_spinner_item);
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(myAdapter);
}
public void loadNews(String Source){
newsService.getArticles(Common.getAPIUrl(Source)).enqueue(new Callback<News>() {
@Override
public void onResponse(Call<News> call, Response<News> response) {
Picasso.get()
.load(response.body().getArticles().get(0).getUrlToImage())
.into(kbv);
articles = response.body().getArticles();
articles.remove(0);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(newsActivity.this, articles, newsActivity.this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(newsActivity.this));
}
@Override
public void onFailure(Call<News> call, Throwable t) {
Toast.makeText(newsActivity.this,"Something went wrong!!!", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onClickListener(View view, int position) {
if(articles != null && view != null){
Intent intent = new Intent(newsActivity.this, webviewClass.class);
intent.putExtra("article_url", articles.get(position).getUrl());
startActivity(intent);
}
else{
Toast.makeText(this, "something went wrong in clickListener method!", Toast.LENGTH_SHORT);
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0 :
Source = "cbs-news";
break;
case 1 :
Source = "espn";
break;
case 2:
Source ="cnn";
break;
case 3:
Source = "mtv-news";
break;
case 4:
Source = "nbc-news";
break;
default:
Source ="abc-news";
}
loadNews(Source);
}
答案 0 :(得分:0)
我认为您没有为微调器设置监听器。
spinner.setOnItemSelectedListener(this);
答案 1 :(得分:0)
您的微调器没有用于侦听值更改的侦听器。在将适配器设置为微调器之后,添加这段代码。
spinner.setOnItemSelectedListener(this);
此外,您的活动现在也将实现微调器的itemSelected侦听器。
希望有帮助。 :)