Задать вопрос
  • Как отобразить в RecycleView данные из ArrayList?

    @shevyrinn Автор вопроса
    MainActivity.java
    spoiler
    public class MainActivity extends AppCompatActivity {
    
        URL url;
        List<String> titles;
        ArrayList<String> links;
        ArrayList<String> description;
        ArrayList<String> pubDate;
        ArrayList<String> author;
        ArrayList<String> category;
        String HHMurl= "https://stalkerweb.ru/feed";
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    
            mRecyclerView.setHasFixedSize(false);
    
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
            mRecyclerView.setLayoutManager(mLayoutManager);
       
            RecyclerView.Adapter mAdapter = new RecyclerAdapter(titles);
            mRecyclerView.setAdapter(mAdapter);
            try{
            titles = new ArrayList<>();
            links = new ArrayList<>();
            description = new ArrayList<>();
            pubDate = new ArrayList<>();
            author = new ArrayList<>();
            category = new ArrayList<>();
    
            RecycleClick.addTo(mRecyclerView).setOnItemClickListener(new RecycleClick.OnItemClickListener() {
                @Override
                public void onItemClicked(RecyclerView recyclerView, int position, View v) {
    
                     //to do something
    
                }
    
            });
                new ProcessInBackground().execute();
    
            } catch(Exception e){
                //to do something
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
    
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
    
        public InputStream getInputStream (URL url)
        {
            try {
                return url.openConnection().getInputStream();
            } catch (IOException e) {
                return null;
            }
        }
    
        private class ProcessInBackground extends AsyncTask<Integer, Void, Exception> {
    
            ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
    
            Exception exception = null;
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                progressDialog.setMessage("Загрузка...");
                progressDialog.show();
            }
    
            @Override
            protected Exception doInBackground(Integer... params) {
    
                try {
                    url = new URL(HHMurl);
                    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                    factory.setNamespaceAware(false);
                    XmlPullParser xpp = factory.newPullParser();
                    xpp.setInput(getInputStream(url), "UTF_8");
                    boolean insideItem = false;
                    int eventType = xpp.getEventType();
    
                    while (eventType != XmlPullParser.END_DOCUMENT) {
                        if (eventType == XmlPullParser.START_TAG) {
                            if (xpp.getName().equalsIgnoreCase("item")) {
                                insideItem = true;
                            } else if (xpp.getName().equalsIgnoreCase("title")) {
                                if (insideItem) {
                                    titles.add(xpp.nextText());
                                }
                            } else if (xpp.getName().equalsIgnoreCase("link")) {
                                if (insideItem) {
                                    links.add(xpp.nextText());
                                }
                            } else if (xpp.getName().equalsIgnoreCase("content:encoded")) {
                                if (insideItem) {
                                    description.add(xpp.nextText());
                                }
                            } else if (xpp.getName().equalsIgnoreCase("pubDate")) {
                                if (insideItem) {
                                    pubDate.add(xpp.nextText());
                                }
                            } else if (xpp.getName().equalsIgnoreCase("dc:creator")) {
                                if (insideItem) {
                                    author.add(xpp.nextText());
                                }
                            } else if (xpp.getName().equalsIgnoreCase("category")) {
                                if (insideItem) {
                                    category.add(xpp.nextText());
                                }
                            }
    
                        } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
                            insideItem = false;
                        }
    
                        eventType = xpp.next();
                    }
    
    
                } catch (XmlPullParserException | IOException e) {
                    exception = e;
                }
    
                return exception;
            }
    
        }}

    Ответ написан
    Комментировать