Problem: ListView mit Bildern aus Datenbank

G

Garnet

Ambitioniertes Mitglied
1
Hallo,
ich würde gerne meine ListView mit Bildern füllen, deren Pfad ich in einer Datenbank abgespeichert habe. Also in der Datenbank steht für ein Bild immer "drawable/bildname".

Ich habs mit folgendem Code versucht, bekomme da aber eine NullPointerException. :-(

Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]for[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i=1; i<=anzahl-1; i++) [/LEFT]
{
...
 
[/SIZE]
[SIZE=2]ImageView image = (ImageView) findViewById(R.id.
[/SIZE][LEFT][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]imageViewfoto[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
 
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cur[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = DataBaseHelper.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]appDB2[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2].rawQuery([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SELECT Foto_ID "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]+[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"FROM Sprecher "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]+[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"WHERE _id = ? "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String[] {sprecherID});[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]spaltenindex[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cur[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getColumnIndex([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Foto_ID"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]String fotoID = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cur[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getCount() > 0)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cur[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].moveToFirst();[/SIZE]
[SIZE=2]fotoID = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cur[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getString([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]spaltenindex[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/SIZE][/LEFT]
 
[LEFT][SIZE=2]}[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] bild = getResources().getIdentifier([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + fotoID, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2], getPackageName()); [/SIZE][/LEFT]
[SIZE=2]image.setImageResource(bild);[/SIZE]

Kann mir einer sagen, was ich da falsch mache? Hatte den gleichen Code an anderer Stelle mit einem einzelnen Bild versucht, da hatte es problemlos funktioniert.
[/LEFT]
 
bei welcher Anweisung kommt denn die Exception ?
was ist wenn cur.getCount() nicht > 0 ist ?
 
Zuletzt bearbeitet:
dann ist fotoID = ""
 
Die Exception kommt bei "image.setImageRessource(bild);

Ich hatte es jetzt mal noch anders probiert:

Code:
[SIZE=2]ImageView [U]image[/U] = (ImageView) findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]imageViewfoto[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]String [U]foto[/U] = "[U]drawable[/U]/[U]bildname[/U]";[/COLOR][/SIZE]
[SIZE=2][COLOR=#3f7f5f][U]int[/U] [U]bild[/U] = getResources().getIdentifier(""+ [U]foto[/U], null, getPackageName());[/COLOR][/SIZE][/LEFT]
[SIZE=2][COLOR=#3f7f5f]image.setImageResource([U]bild[/U]);[/COLOR][/SIZE]
[/COLOR][/SIZE]

Selbst hier bekomm ich die NullpointerException und kann einfach nichts finden was falsch sein soll.

Zum Vergleich hier mal noch ein Code der problemlos geht. Hier wird nur ein einzelnes Bild ersetzt. Einziger Unterschied ist hier, dass ich das Bild nicht direkt ersetzt habe, sondern eine neue ImageView erzeugt habe und das in xml definierte ImageView auf unsichtbar gestellt habe wenn es ein Bild in der Datenbank gab.

Code:
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cur
[/COLOR][/SIZE][/COLOR][/SIZE][LEFT][SIZE=2]= DataBaseHelper.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]appDB2[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2].rawQuery([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SELECT Foto_ID "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]+[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"FROM Sprecher "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]+[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"WHERE _id = ? "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String[] {[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sprecherID[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]});[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]spaltenindex[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cur[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getColumnIndex([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Foto_ID"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]String fotoID = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cur[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getCount() > 0)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cur[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].moveToFirst();[/SIZE]
[SIZE=2]fotoID = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cur[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getString([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]spaltenindex[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/SIZE][/LEFT]
 
[LEFT][SIZE=2]}[/SIZE][/LEFT]
 
[LEFT][SIZE=2]RelativeLayout rl = (RelativeLayout) findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]relativeLayout[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]); [/SIZE]
[SIZE=2]ImageView iv = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ImageView([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]); [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] bild = getResources().getIdentifier([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + fotoID, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2], getPackageName()); [/SIZE]
[SIZE=2]iv.setImageResource(bild);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]RelativeLayout.LayoutParams params = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] RelativeLayout.LayoutParams(100, 100); [/SIZE]
[SIZE=2]params.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]leftMargin[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = -2; [/SIZE]
[SIZE=2]params.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]topMargin[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 10; [/SIZE]
[SIZE=2]rl.addView(iv, params);[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//[U]wenn[/U] [U]es[/U] [U]ein[/U] [U]Sprecherbild[/U] [U]gibt[/U], [U]wird[/U] [U]Platzhalterbild[/U] [U]unsichtbar[/U] [U]gemacht[/U][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (fotoID != [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])[/SIZE]
[SIZE=2]{[/SIZE][/LEFT]
[SIZE=2]((ImageView)(findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]imageView1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]))).setVisibility(View.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]INVISIBLE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);}[/SIZE]

Ich verstehe nicht wieso es dort geht, aber in der Liste nicht.


[/LEFT]
 
Wenn image null ist, hat findViewById einfach nix gefunden. Dein funktionierendes Beispiel nutzt das ja nicht.

Gesendet von meinem MB525 mit Tapatalk 2
 
Aber ich hab image doch mit findViewByID ein Bild zugeordnet. Wie kann das dann null sein?
 
Versuchs mal mit image.setImageDrawable(drawable) bzw. image.setImageBitmap(bm).

Wenn du das Bild eh schon mit Pfadangaben laden mußt, dann kannst den Pfad auch gleich für das richtige Bild verwenden anstatt nur für die ID.
 
Ich glaube das geht nicht. Ich muss ja meine Variable angeben, bei setImageDrawable kann ich aber nicht einfach (R.variable) schreiben. Oder wie meinst du das?
 
Na hast doch den Pfad zum Bild in der DB gespeichert, oder sehe ich das falsch? Dann bekommst du ja z.B. sowas in der Art:
Code:
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/dein_project/deine_bilder/bild_0123.jpg";

Und mit dem Pfad kannst du ja das Bild "laden". Also brauchste nicht die ID rauszufinden um es damit in der ImageView anzuzeigen.
 
Ich hab in meiner Datenbank immer nur "drawable/bildname" gespeichert.

Aber ich glaub ich weiß jetzt wo das Problem liegt.

Ich hab ja meine Liste mit setContentView(R.layout.listview_anzeige);
und einen SimpleAdapter:
saList= new SimpleAdapter(
this,
myListing,
R.layout.
listitem_image_textviews,
new String[] {"line1","line2", "line3" },
new int[] {R.id.zeile1ID, R.id.zeile2ID, R.id.zeile3ID } );

setListAdapter(
saList);

Also bekomm ich wahrscheinlich die NullPointer Exception weil ich die ImageView in R.layout.listitem_image_textviews definiert hab und da mit findViewByID gar nicht drauf zugreifen kann?
Nur was kann ich da jetzt machen?
 
Schreib dir nen eigenen Adapter :smile:
 
  • Danke
Reaktionen: Garnet
Ok, danke. Wenn es da keinen anderen Weg gibt, werd ich das wohl machen müssen.

Ich habe mal grade versucht, das Layout mit <include> einzubinden. Da verschwinden dann zwar die Fehlermeldungen, aber die Bilder werden leider immer noch nicht angezeigt, nur das Bild was ich in der xml definiert hab.
 
Zuletzt bearbeitet:

Ähnliche Themen

S
Antworten
33
Aufrufe
2.669
Sempervivum
S
B
Antworten
6
Aufrufe
1.051
jogimuc
J
A
Antworten
10
Aufrufe
1.021
swa00
swa00
Zurück
Oben Unten