Пишу приложение на android, парсер новостей, с помощью библиотеки Jsoup.
В одном классе получаю ссылки и записываю в массив Arraylist<String> linkList
, как мне потом получить значения linkList
в другом классе (файле), методом getLinkList()
не получается?
**MainActivity.java**
public class MainActivity extends ActionBarActivity {
private static MainActivity activity;
public static MainActivity getActivity() {
return activity;
}
public RecyclerAdapter recyclerAdapter;
public RecyclerView recyclerView;
public ArrayList<String> getLinkList() {
return linkList;
}
public void setLinkList(ArrayList<String> linkList) {
this.linkList = linkList;
}
public ArrayList<String> linkList;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
try {
setLinkList(new Article().execute().get());
Log.d("myLogs", "linkList: " + getLinkList());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
class Article extends AsyncTask<Void, Void, ArrayList<String>> {
ArrayList<String> link = new ArrayList<>();
Elements content;
Document doc = null;
@Override
protected ArrayList<String> doInBackground(Void... voids) {
try {
doc = Jsoup.connect("https://dev.by").get();
content = doc.select(".article-preview__title__link");
link.clear();
for (Element links : content) {
link.add(links.absUrl("href"));
}
} catch (IOException e) {
e.printStackTrace();
}
return link;
}
@Override
protected void onPostExecute(ArrayList<String> stringArrayList) {
super.onPostExecute(stringArrayList);
//Log.d(LOG_TAG, "link_page: " + stringArrayList);
recyclerAdapter = new RecyclerAdapter(title, image);
recyclerView.setAdapter(recyclerAdapter);
}
}
}
класс RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private ArrayList<String> title = new ArrayList<>();
private ArrayList<String> image = new ArrayList<>();
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cardView;
ImageView imageView;
TextView tvTitle;
public ViewHolder(View view) {
super(view);
cardView = (CardView) view.findViewById(R.id.card_view);
imageView = (ImageView) view.findViewById(R.id.imageView);
tvTitle = (TextView) view.findViewById(R.id.tvTitle);
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("myLogs", "onClickLink: " + new MainActivity().getLinkList());
Intent intent = new Intent(view.getContext(), ArticleView.class);
intent.putStringArrayListExtra("link", new MainActivity().getLinkList());
view.getContext().startActivity(intent);
Toast.makeText(MainActivity.getActivity(), "Yes", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onClick(View view) {
}
}
public RecyclerAdapter(ArrayList<String> title, ArrayList<String> imageViews) {
this.title = title;
this.image = imageViews;
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.tvTitle.setText(title.get(position));
Picasso.with(MainActivity.getActivity())
.load(image.get(position))
.error(R.mipmap.ic_launcher)
.into(holder.imageView);
}
@Override
public int getItemCount() {
return title.size();
}
}
класс ArticleView.java public class ArticleView extends ActionBarActivity {
ArrayList<String> linkList = new ArrayList<>();
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.article_view);
textView = (TextView) findViewById(R.id.textView2);
linkList = getIntent().getStringArrayListExtra("link");
Log.d("myLogs", "getLinkList in ArticleView: " + linkList);
}
}