ListView neu erzeugen/komplett neu aufbauen

  • 0 Antworten
  • Letztes Antwortdatum
T

Tartaros

Neues Mitglied
0
Hallo Community :)
Bitte seid mir nicht allzu böse, wenn ich es im falschen Bereich gepostet habe. Bin neu hier und hab noch nicht ganz den durchblick :)

Ich habe schonmal eine kleine Spaß-Androidapp entwickelt und wollte nun etwas mehr, und wollte mir eine Stundenplanapp bauen. Sie sollte immer den aktuellen Tag anzeigen. Durch "wischen" sollte man den nächsten Tag angezeigt bekommen.
Derzeit kann er jedoch nur den aktuellen Tag anzeigen, jedoch scheitere ich beim "wischen". Ich habe mir eine Funktion, die testhalber einen wert aus der ArrayList löschen sollte und die ListView wieder neu zu erzeugen oder aktualisieren.
Jedoch bekomme ich einen Error, wenn ich die Funktion zum löschen des elementes aufrufe.
Code:
02-20 17:09:07.606: E/AndroidRuntime(272): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class com.stundenplan_htl.StundenAdapter)]

Meine derzeite MainActivity:
Ich glaube, der Fehler ist dort behoben, jedoch weiß ich nicht wohin ich die Funktion zum löschen verschieben muss :unsure:
Code:
package com.stundenplan_htl;

import java.io.IOException;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ListView;

@SuppressLint("HandlerLeak")
public class MainActivity extends ListActivity {

	// declare class variables
	static ArrayList<Stunde> allhours = new ArrayList<Stunde>();
	static ArrayList<Stunde> s_parts = new ArrayList<Stunde>();
	private final String link = "http://klassenbuch.htl-hallein.at:8080/WebUntis/Ical.do?school=htl-hallein&elemType=1&elemId=6&rpt_sd=";
	private Runnable viewParts;
	private StundenAdapter s_adapter;
	public static int diffdays = 0;
	private float move_x, move_y;
	private long lastupdate = 0;

	/** Called when the activity is first created. **/
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// WISCHEN 
		ListView listview = (ListView) findViewById(android.R.id.list);
		listview.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View view, MotionEvent event) {
				//System.out.println("x = " + event.getX() + ", y = " + event.getY());
				switch(event.getAction()) {
					case(MotionEvent.ACTION_DOWN): { 
						System.out.println("actiondown");
						move_x = event.getX();
						move_y = event.getY();
					}
					case(MotionEvent.ACTION_UP): {
						if(((event.getX() - move_x) > 150 ) && ((event.getY() - move_y) < 50)) {
							System.out.println("wisch nach rechts");
							lastDay();
						} else if(((move_x - event.getX()) > 150 ) && ((event.getY() - move_y) < 50)) {
							System.out.println("wisch nach links");
							nextDay();
						}
					}
				}
				return false;
			}
		});
		
		// instantiate our ItemAdapter class
		s_adapter = new StundenAdapter(this, R.layout.list_stunde, s_parts);
		setListAdapter(s_adapter);
		s_adapter.notifyDataSetChanged();
		
		// here we are defining our runnable thread.
		viewParts = new Runnable() {
			public void run() {
				handler.sendEmptyMessage(0);   //1231373027771
			}
		};

		// here we call the thread we just defined - it is sent to the handler below.
		Thread thread = new Thread(null, viewParts, "MagentoBackground");
		thread.start();
	}
	
	private Handler handler = new Handler() {
		@SuppressLint({ "HandlerLeak", "HandlerLeak" })
		public void handleMessage(Message msg) {

			s_parts.clear();

				System.out.println(link + MyDate.formatDateLink(MyDate.getDate()));
				System.out.println(MyDate.getMillis());
				if((lastupdate == 0) || (MyDate.getMillis() - lastupdate > 900000)) {
					try {
						CreateHours.createHoursAll(link + MyDate.formatDateLink(MyDate.getDate()));
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			s_adapter = new StundenAdapter(MainActivity.this,
					R.layout.list_stunde, s_parts);

			// display the list.
			setListAdapter(s_adapter);
		}
	};
	
	private static void nextDay() {
		s_parts.remove(s_parts.size()-1);
		System.out.println(s_parts.size());
	}
	
	private static void lastDay() {
		s_parts.remove(s_parts.size()-1);
		System.out.println(s_parts.size());
	}
}

Welche Methode wird aufgerufen wenn ich es mit
Code:
notifyDataSetChanged();
machen möchte? Wird dabei auch der Handler neu aufgerufen oder lediglich der StundenAdapter?

PS: gibt es hier soetwas wie spoiler?
 

Ähnliche Themen

S
Antworten
7
Aufrufe
1.291
Silvasurf
S
Zurück
Oben Unten