I'm trying to implement a Newsreader app following Udemy "Complete Android N Developer Course".List view is used.
As per the instruction I have correctly followed but when executing the below main activity though it is required to update the list items with titles, this shows nothing in the list view. No errors even in the Android Monitor.
Any suggestion to find the issue, please.
Thank you!
public class MainActivity extends AppCompatActivity {
ArrayList<String > titles = new ArrayList<>();
ArrayList<String> content = new ArrayList<>();
ArrayAdapter arrayAdapter;
SQLiteDatabase articleDB ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView );
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,titles);
listView.setAdapter(arrayAdapter);
articleDB = this.openOrCreateDatabase("articles",MODE_PRIVATE,null);
articleDB.execSQL("CREATE TABLE IF NOT EXISTS articles (id INTEGER PRIMARY KEY, articleID INTEGER,title VARCHAR,content VARCHAR)");
updateListView();
DownloadTask task = new DownloadTask();
try {
task.execute("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty");
}catch(Exception e){
e.printStackTrace();
}
}
//update table
public void updateListView(){
Cursor c = articleDB.rawQuery("SELECT * FROM articles", null);
int contentIndex = c.getColumnIndex("content");
int titleIndex = c.getColumnIndex("title");
if(c.moveToFirst()){
titles.clear();
content.clear();
do{
titles.add(c.getString(titleIndex));
content.add(c.getString(contentIndex));
}while (c.moveToNext());
arrayAdapter.notifyDataSetChanged();
}
}
public class DownloadTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... strings) {
String result = "";
URL url;
HttpsURLConnection urlConnection = null;
try {
url = new URL (strings[0]);
urlConnection = (HttpsURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1){
char current = (char) data;
result += current;
data = reader.read();
}
//Log.i("URLContent",result);
JSONArray jsonArray = new JSONArray(result);
int numberOfItems = 20;
if(jsonArray.length() <20){
numberOfItems = jsonArray.length();
}
//to clear the table before add data
articleDB.execSQL("DELETE FROM articles"); //will clear everything and add a new data
for (int i=0;i<numberOfItems;i++ ){
//Log.i("JSONItem",jsonArray.getString(i));
String articleId = jsonArray.getString(i);
url = new URL("https://hacker-news.firebaseio.com/v0/item/"+articleId+".json?print=pretty");
urlConnection = (HttpsURLConnection) url.openConnection();
in = urlConnection.getInputStream();
reader = new InputStreamReader(in);
data = reader.read();
String articleInfo = "";
while (data!= -1){
char current = (char) data;
articleInfo += current;
data = reader.read();
}
//Log.i("ArticleInfo",articleInfo);
//separate title and URL
JSONObject jsonObject = new JSONObject(articleInfo);
if (!jsonObject.isNull("title") && !jsonObject.isNull("url")){
String articleTitle = jsonObject.getString("title");
String articleURL = jsonObject.getString("url");
//Log.i("info",articleTitle + articleURL);
url = new URL(articleURL);
urlConnection = (HttpsURLConnection) url.openConnection();
in = urlConnection.getInputStream();
reader = new InputStreamReader(in);
data = reader.read();
String articleContent = "";
while (data!= -1){
char current = (char) data;
articleContent += current;
data = reader.read();
}
//Log.i("articleContent",articleContent);
String sql = "INSERT INTO articles(articleID,title,content) VALUES(? , ? , ?)";
SQLiteStatement statement = articleDB.compileStatement(sql);
statement.bindString(1,articleId);
statement.bindString(2,articleTitle);
statement.bindString(3,articleContent);
statement.execute();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//run when the download task is completed
updateListView();
}
}
}