From 91aab3f63eafdd4599f3b9fa7e4198b5e85bb293 Mon Sep 17 00:00:00 2001 From: Defend Date: Tue, 14 Feb 2017 01:01:30 +0300 Subject: [PATCH] Added NewsItemList, testing FeedParser. I have found some issues after writing code in that module. --- app/app.iml | 16 ++-- .../defdevteam/tstu/CabinetActivity.java | 6 -- .../defdevteam/tstu/FeedController.java | 55 ++++++++--- .../ru/defend/defdevteam/tstu/FeedParser.java | 32 ++++++- .../defend/defdevteam/tstu/NewsItemList.java | 93 +++++++++++++++++++ app/src/main/res/layout/cabinet_newsfeed.xml | 5 - .../main/res/layout/cabinet_newsfeed_item.xml | 4 +- 7 files changed, 178 insertions(+), 33 deletions(-) create mode 100644 app/src/main/java/ru/defend/defdevteam/tstu/NewsItemList.java diff --git a/app/app.iml b/app/app.iml index a16956c..8b0c423 100644 --- a/app/app.iml +++ b/app/app.iml @@ -66,14 +66,6 @@ - - - - - - - - @@ -82,6 +74,14 @@ + + + + + + + + diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java b/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java index 934e142..1582c65 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java @@ -180,9 +180,6 @@ public class CabinetActivity extends AppCompatActivity AuthURL = "http://web-iais.admin.tstu.ru:7777/zion/f?p=stud_main"; loadSite(AuthURL); - addNewsItem("Hello guys!", "Its my first trying!", null); - addNewsItem("Hello guys!", "Its my second trying!", null); - } @Override @@ -310,7 +307,6 @@ public class CabinetActivity extends AppCompatActivity public void addNewsItem (String title, String text, String link) { LayoutInflater inflater = getLayoutInflater(); LinearLayout newsFeed = (LinearLayout) findViewById(R.id.newsfeed_scroll); - newsFeed.addView(inflater.inflate(R.layout.cabinet_newsfeed_item, null)); View layout = inflater.inflate(R.layout.cabinet_newsfeed_item, null); newsFeed.addView(layout); @@ -322,8 +318,6 @@ public class CabinetActivity extends AppCompatActivity new DownloadImageTask((ImageView) layout.findViewById(R.id.newsfeed_item_image)) .execute(link); - - newsFeed.addView(inflater.inflate(R.layout.cabinet_newsfeed_item, null)); } public void loadSite (String url) { diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java b/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java index 6d53c26..eecab63 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java @@ -1,10 +1,12 @@ package ru.defend.defdevteam.tstu; +import android.util.Log; + /** * Created by thedefend on 29.11.16. */ -public class FeedController implements FeedReader.ReaderCallback{ +public class FeedController implements FeedReader.ReaderCallback, FeedParser.ParserCallback{ FeedReader feedReader; FeedUpdater feedUpdater; @@ -46,15 +48,15 @@ public class FeedController implements FeedReader.ReaderCallback{ throw new Exception("Failed to get rss source string from FeedReader class"); } } - - public void feedUpdateAction(final String result) { - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - //activity.newsTextView.setText(result); - } - }); - } +// +// public void feedUpdateAction(final String result) { +// activity.runOnUiThread(new Runnable() { +// @Override +// public void run() { +// //activity.newsTextView.setText(result); +// } +// }); +// } private class FeedUpdater extends Thread { @Override @@ -81,14 +83,45 @@ public class FeedController implements FeedReader.ReaderCallback{ throw new Exception("Feed Reader returned empty string"); } - public void onSuccess(String result){ + @Override + public void onSuccess(final String result){ this.newsFeed = result; + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + activity.addNewsItem("Test", result, ""); + } + }); + FeedParser parser = new FeedParser(result); + parser.registerCallback(this); + parser.parseXML(); } + @Override public void onFailure(){ //Ill add this code later! } + @Override + public void onSuccessParsing(NewsItemList newsItemList) { + for (NewsItemList.Group item: newsItemList) { + final String title = item.getTitle(); + final String text = item.getText(); + final String link = item.getLink(); + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + activity.addNewsItem(title, text, link); + } + }); + } + } + + @Override + public void onFailureParsing() { + + } + } diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java b/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java index f4945c9..6ad91ad 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java @@ -3,6 +3,9 @@ package ru.defend.defdevteam.tstu; import org.w3c.dom.Document; import org.w3c.dom.Node; +import java.util.ArrayList; +import java.util.Collection; + import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -12,7 +15,27 @@ import javax.xml.parsers.DocumentBuilderFactory; public class FeedParser { + ParserCallback callback; + NewsItemList newsItemList; + String inputXML; + + interface ParserCallback { + void onSuccessParsing(NewsItemList newsItemList); + void onFailureParsing(); + } + + void registerCallback(ParserCallback callback) { + this.callback = callback; + } + FeedParser(String inputXML) { + newsItemList = new NewsItemList(); + this.inputXML = inputXML; + } + + void parseXML() { + + NewsItemList.Group group = new NewsItemList().new Group(); // <---Remove that try { DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); // Создается дерево DOM документа из файла @@ -20,10 +43,17 @@ public class FeedParser { // Получаем корневой элемент Node root = document.getDocumentElement(); + String exp = root.getTextContent(); // <---Remove that + group.set("Node",exp,""); // <---Remove that + newsItemList.add(group);// <---Remove that } catch (Exception e) { e.printStackTrace(); } - + group.set("Node2","end",""); // <---Remove that + newsItemList.add(group);// <---Remove that + group.set("Node3","2end",""); // <---Remove that + newsItemList.add(group);// <---Remove that + callback.onSuccessParsing(newsItemList); } } diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/NewsItemList.java b/app/src/main/java/ru/defend/defdevteam/tstu/NewsItemList.java new file mode 100644 index 0000000..347d051 --- /dev/null +++ b/app/src/main/java/ru/defend/defdevteam/tstu/NewsItemList.java @@ -0,0 +1,93 @@ +package ru.defend.defdevteam.tstu; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.ListIterator; +import java.util.NoSuchElementException; + +/** + * Created by itsmy on 13.02.2017. + */ + +public class NewsItemList implements Iterable { + + private ArrayList list; + + NewsItemList() { + list = new ArrayList<>(); + } + + public int size() { + return list.size(); + } + + public Group get(int index) { + return list.get(index); + } + + public void add(Group group) { + this.list.add(group); + } + + @Override + public Iterator iterator() { + return new NewsIterator(); + } + + private class NewsIterator implements Iterator { + private int cursor; + + public NewsIterator() { + this.cursor = 0; + } + + public boolean hasNext() { + return this.cursor < size(); + } + + public Group next() { + if(this.hasNext()) { + Group current = get(cursor); + cursor ++; + return current; + } + throw new NoSuchElementException(); + } + + public void remove() { + throw new UnsupportedOperationException(); + } + } + + public class Group { + private String title; + private String text; + private String link; + + Group() { + + } + + public void set(String title, String text, String link) { + this.title = title; + this.text = text; + this.link = link; + } + + public String getTitle() { + return this.title; + } + + public String getText() { + return this.text; + } + + public String getLink() { + return this.link; + } + + } + + + +} diff --git a/app/src/main/res/layout/cabinet_newsfeed.xml b/app/src/main/res/layout/cabinet_newsfeed.xml index be25de8..f7b73bc 100644 --- a/app/src/main/res/layout/cabinet_newsfeed.xml +++ b/app/src/main/res/layout/cabinet_newsfeed.xml @@ -16,11 +16,6 @@ android:layout_height="wrap_content" android:id="@+id/newsfeed_scroll"> - - diff --git a/app/src/main/res/layout/cabinet_newsfeed_item.xml b/app/src/main/res/layout/cabinet_newsfeed_item.xml index 0bf4cf0..0f5849c 100644 --- a/app/src/main/res/layout/cabinet_newsfeed_item.xml +++ b/app/src/main/res/layout/cabinet_newsfeed_item.xml @@ -19,7 +19,7 @@ android:paddingTop="10dp">