1. Nimm jetzt an unserem 2. ADVENT-Gewinnspiel teil - Alle Informationen findest Du hier!

Eigener Adapter funktioniert nicht

Dieses Thema im Forum "Android App Entwicklung" wurde erstellt von Prengepower, 28.03.2011.

  1. Prengepower, 28.03.2011 #1
    Prengepower

    Prengepower Threadstarter Android-Hilfe.de Mitglied

    Beiträge:
    62
    Erhaltene Danke:
    0
    Registriert seit:
    15.03.2011
    Phone:
    HTC Sensation
    Hallo!

    Ich wollte mir einen eigenen Adapter basteln, der mir alle meine Datensätze aus einer Datenbank anzeigt und falls das Datum zwischen 2 Einträgen nicht unterschiedliche ist, soll er eine Trennlinie mit dem Datum einfügen.

    Hier mein Code:
    Code:
    	private class SubjectAdapter extends BaseAdapter{
    		private static final int TYPE_ITEM = 0;
            private static final int TYPE_SEPARATOR = 1;
            private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
     
            private ArrayList<ArrayList<String>> mData = new ArrayList<ArrayList<String>>();
            private ArrayList<String> mSubject = new ArrayList<String>();
            private LayoutInflater mInflater;
     
            private TreeSet mSeparatorsSet = new TreeSet();
     
            public SubjectAdapter(Cursor cursor) {
                mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        		startManagingCursor(cursor);
                String date = "";
                if (cursor.moveToFirst()){
                	do{            		
                		mSubject.add(1, cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.KEY_TITLE)));
                		mSubject.add(2, cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.KEY_ROOM)));
                		mSubject.add(3, cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.KEY_DATESTRING)));
                		String actualDate = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.KEY_DATESTRING));
                		
                		addItem(mSubject);
                		
                		if (!actualDate.equals(date)){
                			ArrayList<String> mDate = new ArrayList<String>();
                			mDate.add(1, actualDate);
                			addSeparatorItem(mDate);
                		}
                		
                		date = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.KEY_DATESTRING));
                		
                		mSubject.clear();
                	}while(cursor.moveToNext());
                }
            }
     
            public void addItem(final ArrayList<String> item) {
                mData.add(item);
                notifyDataSetChanged();
            }
     
            public void addSeparatorItem(final ArrayList<String> item) {
                mData.add(item);
                // save separator position
                mSeparatorsSet.add(mData.size() - 1);
                notifyDataSetChanged();
            }
     
            @Override
            public int getItemViewType(int position) {
                return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
            }
     
            @Override
            public int getViewTypeCount() {
                return TYPE_MAX_COUNT;
            }
     
            @Override
            public int getCount() {
                return mData.size();
            }
     
            @Override
            public ArrayList<String> getItem(int position) {
                return mData.get(position);
            }
     
            @Override
            public long getItemId(int position) {
                return position;
            }
     
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;
                int type = getItemViewType(position);
                if (convertView == null) {
                    holder = new ViewHolder();
                    switch (type) {
                        case TYPE_ITEM:
                            convertView = mInflater.inflate(R.layout.subject_row, null);
                            holder.titleView = (TextView)convertView.findViewById(R.id.title);
                            holder.dateView = (TextView)convertView.findViewById(R.id.time);
                            holder.roomView = (TextView)convertView.findViewById(R.id.lecturer);
                            break;
                        case TYPE_SEPARATOR:
                            convertView = mInflater.inflate(R.layout.subject_seperator, null);
                            holder.seperatorView = (TextView)convertView.findViewById(R.id.seperator);
                            break;
                    }
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder)convertView.getTag();
                }
                
                Log.i("Syso", "Subjectgröße"+mData.size());
                Log.i("Syso", "Position"+position);
                
                Log.i("Syso", "Subjectgröße"+mData.get(1).size());
                
                holder.titleView.setText(mData.get(position+1).get(1));
                holder.roomView.setText(mData.get(position+1).get(2));
                holder.dateView.setText(mData.get(position+1).get(3));
                holder.seperatorView.setText(mData.get(position+2).get(1));
                return convertView;
            }
     
        }
    	
    	public static class ViewHolder{
    		public TextView titleView;
    		public TextView dateView;
    		public TextView roomView;
    		public TextView seperatorView;
    	}
    Er wirft keinen Fehler - aber meine Liste bleibt einfach leer O_o Hat einer eine Idee woran das liegt?

    Danke :)

    Gruß!
     
  2. Prengepower, 28.03.2011 #2
    Prengepower

    Prengepower Threadstarter Android-Hilfe.de Mitglied

    Beiträge:
    62
    Erhaltene Danke:
    0
    Registriert seit:
    15.03.2011
    Phone:
    HTC Sensation
    Also der Adapter läuft jetzt so einigermaßen, allerdings habe ich das Problem, dass die Elemente irgendwie zum Teil doppelt und zum Teil unsortiert vorhanden sind.. Woran kann das liegen?

    Hier nochmal der Code von der Funktion, die den Adapter aufruft:
    Code:
    	private void fillData(long date) {
    		cursor = mDbAdapter.fetchAllSubjects(date);
    		startManagingCursor(cursor);
    		
    		SubjectAdapter subjectAdapter = new SubjectAdapter(cursor);
    		String fillDate = "";
    		
    		if (cursor.moveToFirst()){
            	do{
            		String title = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.KEY_TITLE));
            		String room = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.KEY_ROOM));
            		String datestring = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.KEY_DATESTRING));
            		
            		String actualDate = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.KEY_DATE));
            		
            		if (actualDate.equals(fillDate)){
            			subjectAdapter.addItem(new String[] {title, room, datestring});
            		}else{
            			subjectAdapter.addSeparatorItem(new String[] { actualDate });
            			subjectAdapter.addItem(new String[] { title, room, datestring });
            		}
            		
            		fillDate = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.KEY_DATE));
            	}while(cursor.moveToNext());
        	}
    		
    		setListAdapter(subjectAdapter);
    	}

    Code vom Adapter:
    Code:
    	private class SubjectAdapter extends BaseAdapter{
    		private static final int TYPE_ITEM = 0;
            private static final int TYPE_SEPARATOR = 1;
            private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
     
            List<String[]> mData = new ArrayList<String[]>();
            private LayoutInflater mInflater;
     
            private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
     
            public SubjectAdapter(Cursor cursor) {
                mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);            
            }
     
            public void addItem(final String[] strings) {
                mData.add(strings);
                notifyDataSetChanged();
            }
     
            @SuppressWarnings("unused")
    		public void addSeparatorItem(final String[] strings) {
                mData.add(strings);
                // save separator position
                mSeparatorsSet.add(mData.size() - 1);
                notifyDataSetChanged();
            }
     
            @Override
            public int getItemViewType(int position) {
                return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
            }
     
            @Override
            public int getViewTypeCount() {
                return TYPE_MAX_COUNT;
            }
     
            @Override
            public int getCount() {
                return mData.size();
            }
     
            @Override
            public String[] getItem(int position) {
                return mData.get(position);
            }
     
            @Override
            public long getItemId(int position) {
                return position;
            }
     
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;
                int type = getItemViewType(position);
                if (convertView == null) {
                    holder = new ViewHolder();
                    switch (type) {
                        case TYPE_ITEM:
                            convertView = mInflater.inflate(R.layout.subject_row, null);
                            holder.titleView = (TextView)convertView.findViewById(R.id.title);
                            holder.dateView = (TextView)convertView.findViewById(R.id.time);
                            holder.roomView = (TextView)convertView.findViewById(R.id.lecturer);
                            
                            holder.titleView.setText(mData.get(position)[0]);
                            holder.roomView.setText(mData.get(position)[1]);
                            holder.dateView.setText(mData.get(position)[2]);
                            break;
                        case TYPE_SEPARATOR:
                            convertView = mInflater.inflate(R.layout.subject_seperator, null);
                            holder.seperatorView = (TextView)convertView.findViewById(R.id.seperator);
    
                            holder.seperatorView.setText(mData.get(position)[0]);
                            break;
                    }
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder)convertView.getTag();
                }
                
                return convertView;
            }
     
        }
    	
    	public static class ViewHolder{
    		public TextView titleView;
    		public TextView dateView;
    		public TextView roomView;
    		public TextView seperatorView;
    	}

    Hier der Code, womit der Cursor aufgerufen wird:
    Code:
    	public Cursor fetchAllSubjects(long date) {
    		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("CET"));
    		
    		Date now = cal.getTime();
    		Integer year = now.getYear();
    		Integer month = now.getMonth();
    		Integer day = now.getDate();
    		Integer hours = now.getHours();
    		Integer minutes = now.getMinutes();
    		
    		Date newDate = new Date(date);
    		
    		if (date != 0){			
    			year = newDate.getYear();
    			month = newDate.getMonth();
    			day = newDate.getDate();
    			hours = 0;
    			minutes = 0;
    		}else{
    			if (minutes<=30){
    				if (hours == 0){
    					cal.add( Calendar.DATE, -1 );
    					now = cal.getTime();	
    					hours = 23;
    				}
    			
    				minutes = minutes-30;
    			
    				year = now.getYear();
    				month = now.getMonth();
    				day = now.getDate();
    			}else{
    				hours--;
    				minutes = 60+(minutes-30);
    			}
    		}
    		
    		Long datetime = (new Date(year, month, day, hours, minutes)).getTime();
    		
    		if (date == 0){
    			return database.rawQuery("SELECT DISTINCT * FROM "+DATABASE_TABLE+" WHERE "+KEY_SHOW+"=1 AND "+KEY_BEGIN+">=? ORDER BY "+KEY_BEGIN+" ASC", new String[] {datetime.toString()});
    		}else{
    			cal.setTime(newDate);
    			cal.add(Calendar.DATE, +1);
    			
    			Long datetime_end = cal.getTime().getTime();
    			
    			return database.rawQuery("SELECT * FROM "+DATABASE_TABLE+" WHERE "+KEY_SHOW+"=1 AND "+KEY_BEGIN+">=? AND "+KEY_END+"<=? ORDER BY "+KEY_BEGIN+" ASC", new String[] {datetime.toString(),datetime_end.toString()});
    		}
    	}
    Danke :)
     

Diese Seite empfehlen