I have acquired values from a table from a website for a presidential poll app in android that I'm creating. I store four of the columns in four different arraylists. When I try to get the values using arraylist.get(), and store these into an array, arraylist.get() returns null. I displayed my ArrayList on a ListView, and all the values showed up, so I know the values were stored properly.
if you look at the website i'm using, it has all the polling data from when polling started. The array will contain 8 consecutive polls, which I'll average, then plot on a graph. I will take the starting date of the first of the eight polls, and the ending date of the last of the eight polls, and find the date exactly in between, and use that date as the x coordinate for my polling point. Then I'll do the same for the next eight polls. That's why I first check whether the total number of polls on the website is divisible by eight or not. If the number of polls on the website is not divisible by 8, I go to the else, and subtract out the remainder. After the loop is done, I will individually average the remaining.
Here's my code:
package com.pollapp.presidentialelectionpolls;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.Toast;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
Document doc;
Element table;
Elements rows;
Element row;
Elements cols;
Elements link;
int remainderNum = 0, i = 0;
String startingDate, endingDate;
Date start, end;
DateFormat df = new SimpleDateFormat("MM/dd");
String[][] average = new String[8][3];
ArrayList<String> nameOfSrc = new ArrayList<String>();
ArrayList<String> dateWidth = new ArrayList<String>();
ArrayList<String> Trump = new ArrayList<String>();
ArrayList<String> Clinton = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
ConnectivityManager cm = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
Boolean isConnect = ni == null ? false : ni
.isConnectedOrConnecting();
if (isConnect) {
doc = Jsoup
.connect(
"http://www.realclearpolitics.com/epolls/2016/president/us/general_election_trump_vs_clinton-5491.html")
.get();
table = doc.select("table.data.large").get(1);
rows = table.select("tr");
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
for (int i = 2; i < rows.size(); i++) {
row = rows.get(i);
cols = row.select("td");
link = cols.get(0).select("a");
nameOfSrc.add(link.get(0).text());
dateWidth.add(cols.get(1).text());
Clinton.add(cols.get(4).text());
Trump.add(cols.get(5).text());
}
if ((dateWidth.size() % 8) == 0) {
for (i = (dateWidth.size()-1); i <= 7; i = i-8) {
average[i][0] = dateWidth.get(i);
average[i][1] = Clinton.get(i);
average[i][2] = Trump.get(i);
average[i-1][0] = dateWidth.get(i-1);
average[i-1][1] = Clinton.get(i-1);
average[i-1][2] = Trump.get(i-1);
average[i-2][0] = dateWidth.get(i-2);
average[i-2][1] = Clinton.get(i-2);
average[i-2][2] = Trump.get(i-2);
average[i-3][0] = dateWidth.get(i-3);
average[i-3][1] = Clinton.get(i-3);
average[i-3][2] = Trump.get(i-3);
average[i-4][0] = dateWidth.get(i-4);
average[i-4][1] = Clinton.get(i-4);
average[i-4][2] = Trump.get(i-4);
average[i-5][0] = dateWidth.get(i-5);
average[i-5][1] = Clinton.get(i-5);
average[i-5][2] = Trump.get(i-5);
average[i-6][0] = dateWidth.get(i-6);
average[i-6][1] = Clinton.get(i-6);
average[i-6][2] = Trump.get(i-6);
average[i-7][0] = dateWidth.get(i-7);
average[i-7][1] = Clinton.get(i-7);
average[i-7][2] = Trump.get(i-7);
startingDate = average[i][0].substring(0, average[i][0].indexOf(" "));
endingDate = average[i-7][0].substring(average[i-7][0].lastIndexOf(" ") + 1);
}
} else {
remainderNum = (dateWidth.size() % 8);
for (i = (dateWidth.size() - 1); i <= (remainderNum + 7); i = i-8) {
average[i][0] = dateWidth.get(i);
average[i][1] = Clinton.get(i);
average[i][2] = Trump.get(i);
average[i-1][0] = dateWidth.get(i-1);
average[i-1][1] = Clinton.get(i-1);
average[i-1][2] = Trump.get(i-1);
average[i-2][0] = dateWidth.get(i-2);
average[i-2][1] = Clinton.get(i-2);
average[i-2][2] = Trump.get(i-2);
average[i-3][0] = dateWidth.get(i-3);
average[i-3][1] = Clinton.get(i-3);
average[i-3][2] = Trump.get(i-3);
average[i-4][0] = dateWidth.get(i-4);
average[i-4][1] = Clinton.get(i-4);
average[i-4][2] = Trump.get(i-4);
average[i-5][0] = dateWidth.get(i-5);
average[i-5][1] = Clinton.get(i-5);
average[i-5][2] = Trump.get(i-5);
average[i-6][0] = dateWidth.get(i-6);
average[i-6][1] = Clinton.get(i-6);
average[i-6][2] = Trump.get(i-6);
average[i-7][0] = dateWidth.get(i-7);
average[i-7][1] = Clinton.get(i-7);
average[i-7][2] = Trump.get(i-7);
startingDate = average[i][0].substring(0, average[i][0].indexOf(" "));
endingDate = average[i-7][0].substring(average[i-7][0].lastIndexOf(" ") + 1);
}
}
Toast.makeText(getApplicationContext(), startingDate + ", " + endingDate, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
0%8is also0.