Hi zusammen,
ich habe das jetzt mit einer HashMap realisiert. Funktioniert soweit, aber ich möchte durch klicken auf ein Element in dem ListView eine neue Activity aufrufen und da soll dann der richtige Text zum Lied erscheinen.
Die neue Acitvity names Texte geht auch auf, aber es erscheint nur der String aus dem TextView des Layouts, der ersetzt werden soll.
Ich glaube, der String wird nicht richtig an die zweite Activity übergeben. Hatte vorher ein List<String> Array, wo nur Strings drinstehen und jetzt ja diese ArrayList<HashMap...> mit String und einem Object drin.
Da ist doch bestimmt was falsch, oder?
Hier mal mein Code:
Activity: Liederliste
1public class Liederliste extends Activity {
2 //Create pass string
3 public final static String String_EXTRA = "listview.de._ID";
4 ArrayList<HashMap<String, Object>> mapList;
5
6 @Override
7 protected void onCreate(Bundle savedInstanceState) {
8 super.onCreate(savedInstanceState);
9 setContentView(R.layout.liederliste);
10
11 ListView Text = (ListView) findViewById(R.id.ListView1);
12 mapList = new ArrayList<HashMap<String, Object>>();
13 SimpleAdapter simpleAdapter = new SimpleAdapter(this, mapList, R.layout.listen_eintrag,
14 new String[] { "Title"},
15 new int[] {R.id.zeile});
16
17 HashMap<String, Object> map = new HashMap<String, Object>();
18 map.put("Title","Dem Land Tirol die Treue");
19 mapList.add(map);
20 HashMap<String, Object> map2 = new HashMap<String, Object>();
21 map2.put("Title","Egerländer Straßenmarsch");
22 mapList.add(map2);
23 HashMap<String, Object> map3 = new HashMap<String, Object>();
24 map3.put("Title","Koliner Marsch");
25 mapList.add(map3);
26 HashMap<String, Object> map4 = new HashMap<String, Object>();
27 map4.put("Title","Steigerlied");
28 mapList.add(map4);
29
30 Text.setAdapter(simpleAdapter);
31
32 //Sensor a click on a track
33 Text.setOnItemClickListener(OnListClick);
34 }
35
36 private AdapterView.OnItemClickListener OnListClick = new AdapterView.OnItemClickListener(){
37 //Information of the list item
38 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
39 //Pass the Text by using String_EXTRA
40 Intent i = new Intent(Liederliste.this, Texte.class);
41 i.putExtra(String_EXTRA, String.valueOf(mapList.get(position)));
42 startActivity(i);
43 }
44 };
45
46 @Override
47 public boolean onCreateOptionsMenu(Menu menu) {
48 // Inflate the menu; this adds items to the action bar if it is present.
49 getMenuInflater().inflate(R.menu.main, menu);
50 return true;
51 }
52}
Activity: Texte
1public class Texte extends Activity{
2 //Variable declaration
3 String passedVar=null;
4 private TextView passedView=null;
5
6 @Override
7 public void onCreate(Bundle savedInstanceState) {
8 super.onCreate(savedInstanceState);
9 setContentView(R.layout.texte);
10
11 //Get our passed variable from our intent's EXTRAS
12 passedVar=getIntent().getStringExtra(Liederliste.String_EXTRA);
13 //Find out text view
14 passedView=(TextView)findViewById(R.id.text);
15 //Display the right Text to the passed Track
16 if (passedVar.equals("Dem Land Tirol die Treue")){
17 passedView.setText(R.string.tirol);
18 }
19 else if(passedVar.equals("Egerländer Straßenmarsch")){
20 passedView.setText(R.string.egerlaender);
21 }
22 else if(passedVar.equals("Koliner Marsch")){
23 passedView.setText(R.string.koliner);
24 }
25 else if(passedVar.equals("Steigerlied")){
26 passedView.setText(R.string.steiger);
27 }
28 }
29}
Layout: liederliste
1<ListView xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:id="@+id/ListView1"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:drawSelectorOnTop="true"
7 android:paddingBottom="2dp"
8 android:paddingLeft="5dp"
9 android:paddingRight="5dp"
10 android:paddingTop="2dp"
11 tools:context=".liederliste" >
12
13</ListView>
Layout: listen_einträge
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/myListViewEntry"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical" >
7
8 <TextView
9 android:id="@+id/zeile"
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content"
12 android:paddingBottom="2dp"
13 android:paddingLeft="5dp"
14 android:paddingRight="5dp"
15 android:paddingTop="2dp"
16 android:text="DIESE ZEILE MUSS VERGRÖ?ERT WERDEN"
17 android:textSize="28sp"/>
18
19</LinearLayout>
Layout: texte
1<?xml version="1.0" encoding="utf-8"?>
2<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/ScrollView1"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent">
6
7 <TextView
8 android:id="@+id/text"
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:paddingBottom="2dp"
12 android:paddingLeft="5dp"
13 android:paddingRight="5dp"
14 android:paddingTop="2dp"
15 android:text="@string/text"
16 android:textSize="24sp" />
17
18</ScrollView>
Gruß,
Johannes
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.