Xin chào tất cả mọi người, cho em hỏi là : em đã thêm phần tử vào arrayListTitle trong class ReadRSS, nhưng tại sao khi kiểm tra lại số phần tử của arrayListTitle trong class MainActivity thì size lại = 0 vậy ạ ? Em cảm ơn !
package com.hoanglinhsama.readrss;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView listViewNews;
public static AdapterNews adapterNews;
public static ArrayList<String> arrayListTitle;
public static ArrayList<String> arrayListLink;
private ArrayList<News> arrayListNews;
private void mapping() {
this.listViewNews = findViewById(R.id.listViewNews);
}
private void initialization() {
arrayListTitle = new ArrayList<String>();
arrayListLink = new ArrayList<String>();
this.arrayListNews = new ArrayList<News>();
}
private void addElementArrayListNews() {
for (int i = 0; i < arrayListTitle.size(); i++)
this.arrayListNews.add(new News(arrayListTitle.get(i), "", "")); // bo sung them
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mapping();
this.initialization();
new ReadRSS().execute("https://vnexpress.net/rss/so-hoa.rss");
this.addElementArrayListNews();
Toast.makeText(this, arrayListTitle.size() + "", Toast.LENGTH_LONG).show(); // dong nay kiem tra thi size =0
adapterNews = new AdapterNews(MainActivity.this, R.layout.row_news, this.arrayListNews);
this.listViewNews.setAdapter(adapterNews);
this.listViewNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("link", arrayListLink.get(position));
startActivity(intent);
}
});
}
}
package com.hoanglinhsama.readrss;
import android.os.AsyncTask;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class ReadRSS extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
StringBuilder stringBuilderContent = new StringBuilder();
try {
URL url = new URL(strings[0]);
URLConnection urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null)
stringBuilderContent.append(line);
bufferedReader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilderContent.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
XMLDOMParser xmldomParser = new XMLDOMParser();
Document document = xmldomParser.getDocument(s);
NodeList nodeList = document.getElementsByTagName("item");
for (int i = 0; i < nodeList.getLength(); i++) {
Element element = (Element) nodeList.item(i);
MainActivity.arrayListTitle.add(xmldomParser.getValue(element, "title")); // da them phan tu vao arrayListTitle
MainActivity.arrayListLink.add(xmldomParser.getValue(element, "link"));
}
}
}