ArrayAdapter für Custom ListView

surtic

surtic

Erfahrenes Mitglied
19
Guten Abend Zusammen,

Ich bin gerade dabei meine ListView etwas besser zu gestalten als nur den üblichen String :)

Hab nun ein paar Tutorials angeschaut und auch schon sonst in Foren geschaut, aber irgend wie sehe ich nicht wo mein Fehler ist. Ich hoffe eure geschulten Augen sehen es und können mir den Tipp geben :)

Hier der Aufruf aus meiner MainActivity
Code:
setContentView(R.layout.main);

this.listView = (ListView) findViewById(R.id.list_view);
                this.listView.setAdapter(new RssItemAdapter(this, android.R.layout.simple_list_item_1, this.rssHandler.getRssItems() ));

main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView 
	 	android:id="@+id/list_view"
	 	android:layout_width="fill_parent"
	 	android:layout_height="wrap_content"/>
</LinearLayout>

list_item.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/list_item_table">
        <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list_item_table_row_1">
            <TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15px" android:layout_marginRight="15px" android:id="@+id/list_item_title"></TextView>
        </TableRow>
        <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list_item_table_row_2">
            <TextView android:textAppearance="?android:attr/textAppearanceSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15px" android:layout_marginRight="15px" android:id="@+id/list_item_date"></TextView>
        </TableRow>
        <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list_item_table_row_3">
            <TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15px" android:layout_marginRight="15px" android:id="@+id/list_item_description"></TextView>
        </TableRow>
    </TableLayout>
    
</LinearLayout>

RssItemAdapter
Code:
package ch.nonameweb.android.ogame.rss;

import java.util.ArrayList;
import java.util.List;

import android.R;
import android.content.Context;
import android.net.NetworkInfo.DetailedState;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class RssItemAdapter extends ArrayAdapter<RssItem>{

	protected ArrayList<RssItem> rssItems;
	
	public RssItemAdapter(Context context, int textViewResourceId, ArrayList<RssItem> rssItems) {
        super(context, textViewResourceId, rssItems);
        this.rssItems = rssItems;
    }
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
		View view = convertView;
        
        if (view == null) {
        	
        	LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	view = vi.inflate(R.layout.list_item, null);
        	
    	}
      
        RssItem item = rssItems.get(position);
        
        if ( item != null ) {
        	
        	
        	
        	
        	TextView title = (TextView) view.findViewById(R.layout.list_item_title);
        	TextView date = (TextView) view.findViewById(R.id.list_item_date);
        	TextView description = (TextView) view.findViewById(R.id.list_item_description);

			if ( title != null ) {
				title.setText( item.getTitle() );
			}

			if ( date != null ) {
				date.setText( item.getDate().toString() );
			}
			
			if ( description != null ) {
				description.setText( item.getDescription() );
			}
    	}
        
        return v;
    }
	

}


Ich denke mal das der Knopf irgend wo im Adapter ist, da mir dort beim Code das Layout (list_item) nicht gefunden wird.
Code:
view = vi.inflate(R.layout.list_item, null);


Dadurch finde ich natürlich auch diese dann nicht wo es auch wieder Rot ist :)
Code:
TextView title = (TextView) view.findViewById(R.layout.list_item_title);
        	TextView date = (TextView) view.findViewById(R.id.list_item_date);
        	TextView description = (TextView) view.findViewById(R.id.list_item_description);


Ich freue mich über jeden schupser in die richtige Richtung.

Danke für eure Zeit.
 
Gibt es eine Exception? Dann bitte mal den Stacktrace angeben ;)
 
Nope :) ich kann es ja nicht kompelieren da der fehler von eclipse angezeigt wird.

Gesendet mit der Android-Hilfe.de-App
 
du hast sicher das falsche R importiert, wenn er dein layout nicht findet

import android.R; ist falsch

import mein.projekt.R wäre richtig
 
@sowrdi: Danke das hat geholfen, nun kann ich es wieder ausführen.

Leider habe ich direkt ein weiteres Problem und komme nicht recht draus was wie weshalb. Eventuell wisst ihr ja was.

Beim Aufruf also hier

Code:
this.listView.setAdapter(new RssItemAdapter(this, android.R.layout.simple_list_item_1, this.rssHandler.getRssItems() ));

Kriege ich im Logcat folgendes.

Code:
D/AndroidRuntime( 4758): Shutting down VM
W/dalvikvm( 4758): threadid=1: thread exiting with uncaught exception (group=0x40015568)
E/AndroidRuntime( 4758): FATAL EXCEPTION: main
E/AndroidRuntime( 4758): java.lang.RuntimeException: Unable to start activity ComponentInfo{ch.nonameweb.android.ogame/ch.nonameweb.android.ogame.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime( 4758): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1655)
E/AndroidRuntime( 4758): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1671)
E/AndroidRuntime( 4758): 	at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 4758): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 4758): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 4758): 	at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 4758): 	at android.app.ActivityThread.main(ActivityThread.java:3694)
E/AndroidRuntime( 4758): 	at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 4758): 	at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 4758): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime( 4758): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
E/AndroidRuntime( 4758): 	at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 4758): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 4758): 	at ch.nonameweb.android.ogame.MainActivity.onCreate(MainActivity.java:55)
E/AndroidRuntime( 4758): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 4758): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1619)
E/AndroidRuntime( 4758): 	... 11 more
W/ActivityManager( 1279):   Force finishing activity ch.nonameweb.android.ogame/.MainActivity
W/ActivityManager( 1279): Activity pause timeout for HistoryRecord{40d59b10 ch.nonameweb.android.ogame/.MainActivity}
I/Process ( 4758): Sending signal. PID: 4758 SIG: 9

Edit: Also hab noch etwas mehr Infos. Der RssItemAdapter wird Initialisiert aber danach kommt er nicht in die getView rein. RssItems ist gefüllt mit 78 Stück.
 
Zuletzt bearbeitet:
Okey, ich bin wieder etwas weiter gekommen.

Das Problem besteht anscheinend mit der Tabelle die ich in list_item.xml habe, muss ich die zuerst speziell aufrufen um die einzelnen Texte darin zu bearbeiten?
 

Ähnliche Themen

A
Antworten
10
Aufrufe
1.021
swa00
swa00
netfreak
  • netfreak
Antworten
10
Aufrufe
456
netfreak
netfreak
D
Antworten
9
Aufrufe
1.765
jogimuc
J
Zurück
Oben Unten