Johannes E
- Forum-Beiträge: 2
23.01.2012, 20:29:49 via Website
23.01.2012 20:29:49 via Website
Hallo liebe androidPit Community,
ich code grade meine erste App und bin an einem Punkt angekommen wo ich nicht mehr weiter weiß und dazu auch noch keine Lösung gefunden habe obwohl das Problem schon bei einigen aufgetreten ist.
Ich habe ein TabLayout in dem ich eine Listview eingebunden habe und diese ListView fülle ich aus einer Online XML datei die bei mir auf dem Server liegt. Nun bis hier hin tut auch alles wunderbar, wenn ich jetzt mit der Maus über ein Item gehe kommt kein Hover effekt und ich kann es auch dem entsprechend nicht anklicken das meine neue Activity startet. Wenn ich jedoch die Tastatur nutze kommt der Hovereffekt aber anklicken lässt es sich immer noch nicht!
Ich hoffe ihr könnt mir weiter helfen hier mal der Code:
Als erstes die Main Activity:
Jetzt meine Germany Class in der sich die ListView befindet:
Und jetzt die main.xml also die Layout xml:
So und hier die singleMenu.class für das Anzeigen eines Items:
ich code grade meine erste App und bin an einem Punkt angekommen wo ich nicht mehr weiter weiß und dazu auch noch keine Lösung gefunden habe obwohl das Problem schon bei einigen aufgetreten ist.
Ich habe ein TabLayout in dem ich eine Listview eingebunden habe und diese ListView fülle ich aus einer Online XML datei die bei mir auf dem Server liegt. Nun bis hier hin tut auch alles wunderbar, wenn ich jetzt mit der Maus über ein Item gehe kommt kein Hover effekt und ich kann es auch dem entsprechend nicht anklicken das meine neue Activity startet. Wenn ich jedoch die Tastatur nutze kommt der Hovereffekt aber anklicken lässt es sich immer noch nicht!
Ich hoffe ihr könnt mir weiter helfen hier mal der Code:
Als erstes die Main Activity:
1import android.app.TabActivity;
2import android.content.Intent;
3import android.content.res.Resources;
4import android.os.Bundle;
5import android.widget.TabHost;
6
7public class HafleActivity extends TabActivity {
8 /** Called when the activity is first created. */
9 public void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.main);
12
13 Resources res = getResources(); // Resource object to get Drawables
14 TabHost tabHost = getTabHost(); // The activity TabHost
15 TabHost.TabSpec spec; // Resusable TabSpec for each tab
16 Intent intent; // Reusable Intent for each tab
17
18 // Create an Intent to launch an Activity for the tab (to be reused)
19 intent = new Intent().setClass(this, Germany.class);
20
21 // Initialize a TabSpec for each tab and add it to the TabHost
22 spec = tabHost.newTabSpec("germany").setIndicator("Germany",
23 res.getDrawable(R.drawable.germany))
24 .setContent(intent);
25 tabHost.addTab(spec);
26
27 // Create an Intent to launch an Activity for the tab (to be reused)
28 intent = new Intent().setClass(this, Sweden.class);
29
30 // Initialize a TabSpec for each tab and add it to the TabHost
31 spec = tabHost.newTabSpec("sweden").setIndicator("Sweden",
32 res.getDrawable(R.drawable.sweden))
33 .setContent(intent);
34 tabHost.addTab(spec);
35
36 // Create an Intent to launch an Activity for the tab (to be reused)
37 intent = new Intent().setClass(this, Netherlands.class);
38
39 // Initialize a TabSpec for each tab and add it to the TabHost
40 spec = tabHost.newTabSpec("netherlands").setIndicator("Netherlands",
41 res.getDrawable(R.drawable.netherlands))
42 .setContent(intent);
43 tabHost.addTab(spec);
44
45 // Create an Intent to launch an Activity for the tab (to be reused)
46 intent = new Intent().setClass(this, Sponsoren.class);
47
48 // Initialize a TabSpec for each tab and add it to the TabHost
49 spec = tabHost.newTabSpec("sponsoren").setIndicator("Sponsoren",
50 res.getDrawable(R.drawable.information
51 ))
52 .setContent(intent);
53 tabHost.addTab(spec);
54 tabHost.setCurrentTab(0);
55
56 }
57}
2import android.content.Intent;
3import android.content.res.Resources;
4import android.os.Bundle;
5import android.widget.TabHost;
6
7public class HafleActivity extends TabActivity {
8 /** Called when the activity is first created. */
9 public void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.main);
12
13 Resources res = getResources(); // Resource object to get Drawables
14 TabHost tabHost = getTabHost(); // The activity TabHost
15 TabHost.TabSpec spec; // Resusable TabSpec for each tab
16 Intent intent; // Reusable Intent for each tab
17
18 // Create an Intent to launch an Activity for the tab (to be reused)
19 intent = new Intent().setClass(this, Germany.class);
20
21 // Initialize a TabSpec for each tab and add it to the TabHost
22 spec = tabHost.newTabSpec("germany").setIndicator("Germany",
23 res.getDrawable(R.drawable.germany))
24 .setContent(intent);
25 tabHost.addTab(spec);
26
27 // Create an Intent to launch an Activity for the tab (to be reused)
28 intent = new Intent().setClass(this, Sweden.class);
29
30 // Initialize a TabSpec for each tab and add it to the TabHost
31 spec = tabHost.newTabSpec("sweden").setIndicator("Sweden",
32 res.getDrawable(R.drawable.sweden))
33 .setContent(intent);
34 tabHost.addTab(spec);
35
36 // Create an Intent to launch an Activity for the tab (to be reused)
37 intent = new Intent().setClass(this, Netherlands.class);
38
39 // Initialize a TabSpec for each tab and add it to the TabHost
40 spec = tabHost.newTabSpec("netherlands").setIndicator("Netherlands",
41 res.getDrawable(R.drawable.netherlands))
42 .setContent(intent);
43 tabHost.addTab(spec);
44
45 // Create an Intent to launch an Activity for the tab (to be reused)
46 intent = new Intent().setClass(this, Sponsoren.class);
47
48 // Initialize a TabSpec for each tab and add it to the TabHost
49 spec = tabHost.newTabSpec("sponsoren").setIndicator("Sponsoren",
50 res.getDrawable(R.drawable.information
51 ))
52 .setContent(intent);
53 tabHost.addTab(spec);
54 tabHost.setCurrentTab(0);
55
56 }
57}
Jetzt meine Germany Class in der sich die ListView befindet:
1import java.util.ArrayList;
2import java.util.HashMap;
3
4import org.w3c.dom.Document;
5import org.w3c.dom.Element;
6import org.w3c.dom.NodeList;
7
8import android.app.ListActivity;
9import android.content.Intent;
10import android.os.Bundle;
11import android.view.View;
12import android.widget.AdapterView;
13import android.widget.AdapterView.OnItemClickListener;
14import android.widget.ListAdapter;
15import android.widget.ListView;
16import android.widget.SimpleAdapter;
17import android.widget.Toast;
18import android.widget.TextView;
19
20
21public class Germany extends ListActivity {
22 public void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24 //setContentView(R.layout.main);
25
26 ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
27 String xml = XMLfunctions.getXML();
28 Document doc = XMLfunctions.XMLfromString(xml);
29
30 int numResults = XMLfunctions.numResults(doc);
31
32 if((numResults <= 0)){
33 Toast.makeText(Germany.this, "Keine Events vorhanden", Toast.LENGTH_LONG).show();
34 Toast.makeText(Germany.this, ""+numResults+"", Toast.LENGTH_LONG).show();
35 finish();
36 }
37
38 NodeList nodes = doc.getElementsByTagName("result");
39
40 for (int i = 0; i < nodes.getLength(); i++) {
41 HashMap<String, String> map = new HashMap<String, String>();
42
43 Element e = (Element)nodes.item(i);
44 map.put("id", XMLfunctions.getValue(e, "id"));
45 map.put("titel", XMLfunctions.getValue(e, "titel"));
46 map.put("city", XMLfunctions.getValue(e, "city"));
47 map.put("date", XMLfunctions.getValue(e, "date"));
48 mylist.add(map);
49 }
50
51 ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
52 new String[] { "titel", "city", "date" },
53 new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_date });
54
55 setListAdapter(adapter);
56
57 ListView view = getListView();
58 view.setTextFilterEnabled(true);
59 view.setOnItemClickListener(new OnItemClickListener() {
60
61 @Override
62 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
63 setContentView(R.id.view);
64
65 // getting values from selected ListItem
66 String titel = ((TextView) view.findViewById(R.id.item_title)).getText().toString();
67 String city = ((TextView) view.findViewById(R.id.item_subtitle)).getText().toString();
68 String date = ((TextView) view.findViewById(R.id.item_date)).getText().toString();
69
70 Intent showEvent = new Intent(getApplicationContext(), singleMenu.class);
71 showEvent.putExtra("titel", titel);
72 showEvent.putExtra("city", city);
73 showEvent.putExtra("date", date);
74
75 startActivity(showEvent);
76 }
77 });
78
79 setContentView(view);
80}}
2import java.util.HashMap;
3
4import org.w3c.dom.Document;
5import org.w3c.dom.Element;
6import org.w3c.dom.NodeList;
7
8import android.app.ListActivity;
9import android.content.Intent;
10import android.os.Bundle;
11import android.view.View;
12import android.widget.AdapterView;
13import android.widget.AdapterView.OnItemClickListener;
14import android.widget.ListAdapter;
15import android.widget.ListView;
16import android.widget.SimpleAdapter;
17import android.widget.Toast;
18import android.widget.TextView;
19
20
21public class Germany extends ListActivity {
22 public void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24 //setContentView(R.layout.main);
25
26 ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
27 String xml = XMLfunctions.getXML();
28 Document doc = XMLfunctions.XMLfromString(xml);
29
30 int numResults = XMLfunctions.numResults(doc);
31
32 if((numResults <= 0)){
33 Toast.makeText(Germany.this, "Keine Events vorhanden", Toast.LENGTH_LONG).show();
34 Toast.makeText(Germany.this, ""+numResults+"", Toast.LENGTH_LONG).show();
35 finish();
36 }
37
38 NodeList nodes = doc.getElementsByTagName("result");
39
40 for (int i = 0; i < nodes.getLength(); i++) {
41 HashMap<String, String> map = new HashMap<String, String>();
42
43 Element e = (Element)nodes.item(i);
44 map.put("id", XMLfunctions.getValue(e, "id"));
45 map.put("titel", XMLfunctions.getValue(e, "titel"));
46 map.put("city", XMLfunctions.getValue(e, "city"));
47 map.put("date", XMLfunctions.getValue(e, "date"));
48 mylist.add(map);
49 }
50
51 ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
52 new String[] { "titel", "city", "date" },
53 new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_date });
54
55 setListAdapter(adapter);
56
57 ListView view = getListView();
58 view.setTextFilterEnabled(true);
59 view.setOnItemClickListener(new OnItemClickListener() {
60
61 @Override
62 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
63 setContentView(R.id.view);
64
65 // getting values from selected ListItem
66 String titel = ((TextView) view.findViewById(R.id.item_title)).getText().toString();
67 String city = ((TextView) view.findViewById(R.id.item_subtitle)).getText().toString();
68 String date = ((TextView) view.findViewById(R.id.item_date)).getText().toString();
69
70 Intent showEvent = new Intent(getApplicationContext(), singleMenu.class);
71 showEvent.putExtra("titel", titel);
72 showEvent.putExtra("city", city);
73 showEvent.putExtra("date", date);
74
75 startActivity(showEvent);
76 }
77 });
78
79 setContentView(view);
80}}
Und jetzt die main.xml also die Layout xml:
1<?xml version="1.0" encoding="utf-8"?>
2<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@android:id/tabhost"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent">
6 <LinearLayout
7 android:orientation="vertical"
8 android:layout_width="fill_parent"
9 android:layout_height="fill_parent"
10 android:padding="5dp">
11 <TabWidget
12 android:id="@android:id/tabs"
13 android:layout_width="fill_parent"
14 android:layout_height="wrap_content" />
15 <FrameLayout
16 android:id="@android:id/tabcontent"
17 android:layout_width="fill_parent"
18 android:layout_height="fill_parent"
19 android:padding="5dp" />
20 <TextView
21 android:id="@+id/item_title"
22 android:layout_width="fill_parent"
23 android:layout_height="wrap_content"
24 android:padding="2dp"
25 android:textAppearance="?android:attr/textAppearanceMedium"
26 android:textSize="20dp" />
27
28 <TextView
29 android:id="@+id/item_subtitle"
30 android:layout_width="fill_parent"
31 android:layout_height="wrap_content"
32 android:padding="2dp"
33 android:textSize="13dp" />
34
35 <TextView
36 android:id="@+id/item_date"
37 android:layout_width="fill_parent"
38 android:layout_height="wrap_content"
39 android:padding="2dp"
40 android:textSize="13dp" />
41 <ListView
42 android:id="@+id/view"
43 android:layout_width="fill_parent"
44 android:layout_height="wrap_content"
45 android:clickable="true"/>
46 </LinearLayout>
47</TabHost>
2<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@android:id/tabhost"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent">
6 <LinearLayout
7 android:orientation="vertical"
8 android:layout_width="fill_parent"
9 android:layout_height="fill_parent"
10 android:padding="5dp">
11 <TabWidget
12 android:id="@android:id/tabs"
13 android:layout_width="fill_parent"
14 android:layout_height="wrap_content" />
15 <FrameLayout
16 android:id="@android:id/tabcontent"
17 android:layout_width="fill_parent"
18 android:layout_height="fill_parent"
19 android:padding="5dp" />
20 <TextView
21 android:id="@+id/item_title"
22 android:layout_width="fill_parent"
23 android:layout_height="wrap_content"
24 android:padding="2dp"
25 android:textAppearance="?android:attr/textAppearanceMedium"
26 android:textSize="20dp" />
27
28 <TextView
29 android:id="@+id/item_subtitle"
30 android:layout_width="fill_parent"
31 android:layout_height="wrap_content"
32 android:padding="2dp"
33 android:textSize="13dp" />
34
35 <TextView
36 android:id="@+id/item_date"
37 android:layout_width="fill_parent"
38 android:layout_height="wrap_content"
39 android:padding="2dp"
40 android:textSize="13dp" />
41 <ListView
42 android:id="@+id/view"
43 android:layout_width="fill_parent"
44 android:layout_height="wrap_content"
45 android:clickable="true"/>
46 </LinearLayout>
47</TabHost>
So und hier die singleMenu.class für das Anzeigen eines Items:
1import android.app.Activity;
2import android.content.Intent;
3import android.os.Bundle;
4import android.widget.TextView;
5
6
7public class singleMenu extends Activity {
8
9 // XML node keys
10 static final String KEY_NAME = "titel";
11 static final String KEY_COST = "city";
12 static final String KEY_DESC = "date";
13 @Override
14 public void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.main);
17
18 // getting intent data
19 Intent showEvent = getIntent();
20
21 // Get XML values from previous intent
22 String titel = showEvent.getStringExtra(KEY_NAME);
23 String city = showEvent.getStringExtra(KEY_COST);
24 String date = showEvent.getStringExtra(KEY_DESC);
25
26 // Displaying all values on the screen
27 TextView lblName = (TextView) findViewById(R.id.item_title);
28 TextView lblCost = (TextView) findViewById(R.id.item_subtitle);
29 TextView lblDesc = (TextView) findViewById(R.id.item_date);
30
31 lblName.setText(titel);
32 lblCost.setText(city);
33 lblDesc.setText(date);
34 }
35
36}
2import android.content.Intent;
3import android.os.Bundle;
4import android.widget.TextView;
5
6
7public class singleMenu extends Activity {
8
9 // XML node keys
10 static final String KEY_NAME = "titel";
11 static final String KEY_COST = "city";
12 static final String KEY_DESC = "date";
13 @Override
14 public void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.main);
17
18 // getting intent data
19 Intent showEvent = getIntent();
20
21 // Get XML values from previous intent
22 String titel = showEvent.getStringExtra(KEY_NAME);
23 String city = showEvent.getStringExtra(KEY_COST);
24 String date = showEvent.getStringExtra(KEY_DESC);
25
26 // Displaying all values on the screen
27 TextView lblName = (TextView) findViewById(R.id.item_title);
28 TextView lblCost = (TextView) findViewById(R.id.item_subtitle);
29 TextView lblDesc = (TextView) findViewById(R.id.item_date);
30
31 lblName.setText(titel);
32 lblCost.setText(city);
33 lblDesc.setText(date);
34 }
35
36}
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.