Java Frage Variable übergeben

  • Antworten:13
Thomas Schlagkamp
  • Forum-Beiträge: 14

29.03.2013, 15:06:44 via Website

Hallo zusammen.
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
1public class ListListener implements OnItemClickListener {
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
1public class ContenViewActivity extends Activity {
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}

Antworten
impjor
  • Forum-Beiträge: 1.793

29.03.2013, 15:37:32 via App

Hab jetzt nicht deinen Code gelesen, aber wo ist das Problem?
Du musst nur public static .... schreiben, um das aus anderen Klassen lesen zu können.
Gruß

Liebe Grüße impjor.

Für ein gutes Miteinander: Unsere Regeln
Apps für jeden Einsatzzweck
Stellt eure App vor!

Antworten
Appsoluts
  • Forum-Beiträge: 304

29.03.2013, 15:38:23 via Website

Klasse 1 Zeile 23.

i.setExtra("DATA",variable)


Klasse 2 Zeile 17

source = getIntent().getStringExtra("DATA");

Antworten
Thomas Schlagkamp
  • Forum-Beiträge: 14

29.03.2013, 15:50:28 via Website

Stehe Irgendwie auf dem Schlauch

1public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
2
3 Intent i = new Intent(activity, ContenViewActivity.class);
4 //String at=new String(at.setData(Uri.parse(listItems.get(pos).getLink())));
5
6 //Link in Variable packen und zur Verfügung stellen.
7 //Variable in ConteViewActivity als Url aufrufen
8
9 i.setExtra("DATA",(Uri.parse(listItems.get(pos).getLink())));
und
1//////////////////////////
2 private static String source ;
3
4
5 @Override
6 public void onCreate(Bundle savedInstanceState) {
7 super.onCreate(savedInstanceState);
8 setContentView(R.layout.content);
9 source = getIntent().getStringExtra("DATA");}

So gehts nicht
Sorry wenn ich euch nerve

Antworten
Appsoluts
  • Forum-Beiträge: 304

29.03.2013, 17:02:14 via Website

i.setExtra("DATA",at);

Antworten
Appsoluts
  • Forum-Beiträge: 304

29.03.2013, 17:06:44 via Website

ach sorry es heisst auch i.putExtra("DATA",at)

Antworten
Thomas Schlagkamp
  • Forum-Beiträge: 14

29.03.2013, 17:21:20 via Website

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

Intent i = new Intent(activity, ContenViewActivity.class);
i.putExtra("DATA",at);


at wird angemeckert.

Die müsste ja auch noch irgendwo deklariert werden oder?

Antworten
Appsoluts
  • Forum-Beiträge: 304

29.03.2013, 17:23:17 via Website

achso du hattest es auskommentiert...
ja hier:

String at=new String(at.setData(Uri.parse(listItems.get(pos).getLink())));

Antworten
Thomas Schlagkamp
  • Forum-Beiträge: 14

29.03.2013, 17:43:49 via Website

Juhuu vielen Dank.
So kommen zumindest keine Fehler.

Leider bleibt aber auch das gewünschte Ergebnis aus. Nämlich der Inhalt des jeweiligen Feeds.

Hat jemand eine Idee dazu?

Antworten
Thomas Schlagkamp
  • Forum-Beiträge: 14

29.03.2013, 17:47:07 via Website

Fehler laut LogCat java.net.MalformedURLExepion

Antworten
impjor
  • Forum-Beiträge: 1.793

29.03.2013, 19:58:35 via App

CBsol Android
achso du hattest es auskommentiert...
ja hier:

String at=new String(at.setData(Uri.parse(listItems.get(pos).getLink())));
Da ist irgendwas faul ;)

Probier mal
String at = Uri.parse(listItems.get(pos).getLink());

Gruß

Liebe Grüße impjor.

Für ein gutes Miteinander: Unsere Regeln
Apps für jeden Einsatzzweck
Stellt eure App vor!

Antworten
Thomas Schlagkamp
  • Forum-Beiträge: 14

30.03.2013, 09:59:19 via Website

Vielen Dank euch allen. soweit so gut.

Ein Problem habe ich noch.
Der Inhalt des Feed wird noch nicht dargestellt.
Der Link zu den einzelnen Feeds passt.
aber ausgegeben wird nur:
div class="feed-description"
das kann ja eigentlich nur ein kleiner Fehler sein.

Vielen Dank und frohe Ostern

Antworten