- Forum-Beiträge: 14
29.03.2013, 15:06:44 via Website
29.03.2013 15:06:44 via Website
Ich habe folgendes Problem
ich will ich der Klasse 1 eine Variable erstellen mit folgendem Inhalt
.setData(Uri.parse(listItems.get(pos).getLink()));
diese Variable muss in Klasse 2 aufgerufen werden
private static final String source = VARIABLE;
Könnte mir jemand erklären wie ich das anstellen kann?
Im Anschluss noch mal der ganze Code
Über Hilfe würde ich mich sehr freuen
Klasse1
2 // List item's reference
3 List<RssItem> listItems;
4 // Calling activity reference
5 Activity activity;
6
7 public ListListener(List<RssItem> aListItems, Activity anActivity) {
8 listItems = aListItems;
9 activity = anActivity;
10 }
11
12 /**
13 * neue Activity aufrufen.
14 */
15 public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
16
17 Intent i = new Intent(activity, ContenViewActivity.class);
18
19 //Link in Variable packen und zur Verfügung stellen.
20 //Variable in ConteViewActivity als Url aufrufen
21
22 /////////////
23 ???.setData(Uri.parse(listItems.get(pos).getLink()));
24
25 ////////////////////
26
27 activity.startActivity(i);
28
29 }
30
31}
Klasse 2
2 /**
3 * Die URL des RSS Items.
4 //////////////// */
5
6
7
8
9
10 private static final String source = ???????????????;
11
12 /////////////////////////////////
13 @Override
14 public void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.content);
17 }
18
19
20 @Override
21 protected void onStart() {
22 super.onStart();
23
24 // Downloading the RSS feed needs to be done on a separate thread.
25 Thread downloadThread = new Thread(new Runnable() {
26
27 public void run() {
28 try {
29 updateView(getLatestContent(retrieveRssDocument()));
30 } catch (Exception e) {
31 Log.e("Content Retriever", e.getLocalizedMessage(), e);
32 }
33 }
34 });
35
36 downloadThread.start();
37 }
38
39 /**
40 * Updates the {@link WebView} with the provided content.
41 *
42 * @param content
43 * the content to provide to the {@link WebView}.
44 */
45 private void updateView(final String content) {
46 final WebView view = (WebView) findViewById(R.id.content);
47 view.loadData("<html><body>" + content + "</body></html>",
48 "text/html", null);
49 }
50
51 /**
52 * Download the content and parse into a {@link Document}.
53 *
54 * @return a {@link Document} containing the RSS data
55 *
56 * @throws IOException
57 * if an io error was encountered
58 * @throws ParserConfigurationException
59 * an error was encountered while trying to build the document
60 * @throws SAXException
61 * an error was encountered while trying to parse the RSS
62 * content
63 */
64 private Document retrieveRssDocument() throws IOException,
65 ParserConfigurationException, SAXException {
66
67 URL url = new URL(source);
68 URLConnection connection = url.openConnection();
69 InputStream inStream = connection.getInputStream();
70
71 try {
72 DocumentBuilder builder = DocumentBuilderFactory.newInstance()
73 .newDocumentBuilder();
74 return builder.parse(new BufferedInputStream(inStream));
75 } finally {
76 inStream.close();
77 }
78 }
79
80 /**
81 * Obtains the content from the latest RSS entry.
82 *
83 * @return the content from the latest RSS entry
84 *
85 * @throws IOException
86 * if an io error was encountered
87 * @throws ParserConfigurationException
88 * an error was encountered while trying to build the document
89 * @throws SAXException
90 * an error was encountered while trying to parse the RSS
91 * content
92 */
93 private String getLatestContent(Document document) throws IOException,
94 ParserConfigurationException, SAXException {
95
96 NodeList nodeList = document.getElementsByTagName("item");
97 int length = nodeList.getLength();
98 if (length > 0) {
99 return getDescriptionContent(nodeList.item(0));
100 }
101
102 return null;
103 }
104
105 /**
106 * Searches for the first "description" node from the provided root.
107 *
108 * @param root
109 * the root node to start the search from
110 * @return the first "description" node that was found; {@code null} is
111 * returned if no match is found
112 */
113 private String getDescriptionContent(Node root) {
114 if (root instanceof Element) {
115 Element asElement = (Element) root;
116 if (asElement.getTagName().equalsIgnoreCase("description")) {
117 return asElement.getTextContent();
118 }
119 }
120
121 NodeList children = root.getChildNodes();
122 for (int i = 0; i < children.getLength(); i++) {
123 Node node = children.item(i);
124 String result = getDescriptionContent(node);
125 if (result != null) {
126 return result;
127 }
128 }
129 return null;
130 }
131}
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.