I have the design set up, I need to know how to populate the listview with the URLs RSS feed the user enters in the first activity. Then onClick it opens the 2nd activity which opens a list I need to populate with the user Inputted URL RSS feed. After that I need to click an item from a list an open it in another activity or fragment that displays the RSS news. Below is the working base code for the program, I haven't added the 3rd activity or fragment yet, but am confident I can do that once I figure this part out.
public class MainActivity extends AppCompatActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, ListActivity.class);
startActivity(myIntent);
}
});
}
}
2nd Activity
public class ListActivity extends MainActivity {
ListView rssList;
private RssData[] listData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
this.generateData();
rssList = (ListView) this.findViewById(R.id.RssListView);
RssAdapter listAdapter = new RssAdapter(this, android.R.layout.simple_list_item_1, listData);
rssList.setAdapter(listAdapter);
}
//rssNews = (ListView) findViewById(R.id.rssListView);
//rssNews.setAdapter(new RssAdapter(this,rd));
private void generateData() {
RssData data = null;
listData = new RssData[10];
for (int i = 0; i < 10; i++) {
data = new RssData();
data.postTitle = "Post " + (i + 1) + " Title: This is the Post Title from RSS Feed";
listData[i] = data;
}
}
Here is a class I tried to implement, this goes in onCreate in 2nd Activity
Downloader d = new Downloader();
d.execute("https://www.google.com");
try {
src.setText(d.get());
}
catch(Exception e)
{
Log.e("ERROR IN THREAD", e.toString());
}
}
Another class I tried, this would be at end of 2nd activity
class Downloader extends AsyncTask<String, Void, String> {
public String doInBackground(String... urls) {
String result = null;
try
{
URL url = new URL(urls[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
result = result + line;
}
conn.disconnect();
} catch (Exception e) {
Log.e("ERROR RETRIEVING", e.toString());
}
return result;
}
}
//on item selected from listView open new fragment/activity/layout
Adapter
public class RssAdapter extends ArrayAdapter <RssData>
{
private Activity myContext;
private RssData[] datas;
public RssAdapter(Context context, int textViewResourceId,RssData[]objects)
{
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View rowView;
LayoutInflater myInflater;
if(convertView ==null)
{
myInflater = myContext.getLayoutInflater();
convertView = myInflater.inflate(R.layout.list_item_layout, null);
}
rowView = convertView;
TextView postTitleView = (TextView) rowView.findViewById(R.id.readerViewDescription);
postTitleView.setText(datas[position].postTitle);
return rowView;
}
}
Data
public class RssData
{
public String postThumbUrl;
public String postTitle;
public String postDate;
}
XML LAYOUT FILES activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cj.rssreaderfi.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter URL for the desired RSS feed."
android:id ="@+id/URLTxtBox"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id ="@+id/URLEditTxt"
android:hint="www.Example.com"
android:layout_below="@+id/URLTxtBox"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retrieve"
android:id="@+id/button"
android:layout_below="@+id/URLTxtBox"
android:layout_toEndOf="@+id/URLTxtBox"
android:onClick="retrieve"/>
</RelativeLayout>
list_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/readerViewDescription"/>
</LinearLayout>
list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/RssListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
</LinearLayout>