ExpandableListView - Markierungen speichern

M

Muxxxa

Neues Mitglied
0
Hallo Androider!

Leider habe ich keine Lösung zur folgende Problem im Internet gefunden und komme selbst nicht weiter :sad: Aber vieleicht kriegen wir das zusammen hin..

Es geht darum dass die Elemente einer ExpandableListView nicht persistent sind. Die ChildViews der Elemente werden nach jedem Zuklappen und Wiederöffnen der Parent immer neu Erstellt und gezeichnet.
Man kann zwar Listner auf die Views setzen und sie markieren (in meinem Bsp Farbe auf rot ändern), aber wenn mann runter scrollt oder, wie ich schon gesagt hab, Parent View zuklappe und wiederöffne dann ist die Markierung wieder weg!

Ich habe ich mir auch eine Lösung ausgedacht, nur die Funktioniert nicht weil der Listener (cl) nicht aufgerufen wird..

Hier der Code:

PHP:
package com.example.android.apis;

import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;

public class ApiDemos extends ExpandableListActivity  {
    
    //Das funktioniert!(mann muss nur das Listner-setzen in getChildView() wieder setzen) 
    //Child Elemente werden zwar markiert aber deren 
    //Zustände nicht gespeichert   =((
    private View.OnClickListener cl1 = new View.OnClickListener() {
        public void onClick(View v) {
            if(((TextView)v).isSelected() == false){
                
                 ((TextView)v).setSelected(true);
                 ((TextView)v).setTextColor(RED);
            }
            else {
                ((TextView)v).setSelected(false);
                ((TextView)v).setTextColor(WHITE);
            }
        }
    };
    
    //Daher habe ich  folgenden Ansatz probiert:
    
    //Speichern die Zustände in eigene Struktur
    class veranstaltungView {
        public String text;
        public boolean marked;
        
        public veranstaltungView(String txt) {
            text = txt;
            marked = false;
        }        
    }    
    
    //Der Listener wird nicht auf einzelne Elemente gesetzt sondern auf die ganze ExpandableListView
    private OnChildClickListener cl = new OnChildClickListener() {
        
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            
            if(((TextView)v).isSelected() == false){                
                 
                 veranstaltungenList.get(groupPosition).get(childPosition).marked = true;
                 ((TextView)v).setSelected(true);
                 ((TextView)v).setTextColor(RED);
            }
            else {
                veranstaltungenList.get(groupPosition).get(childPosition).marked = false;
                ((TextView)v).setSelected(false);
                ((TextView)v).setTextColor(WHITE);
            }
            return false;
        }
    };    
    
    
    
    private static final int WHITE = 0xffffffff;
    private static final int RED   = 0xffbc0000;
    
    ExpandableListAdapter mAdapter;
    private ArrayList<ArrayList<veranstaltungView>> veranstaltungenList;
    
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        //Daten generieren
        veranstaltungenList = new ArrayList<ArrayList<veranstaltungView>>();
        for(int i = 0; i < 6; i++) {
            ArrayList<veranstaltungView> semester = new ArrayList<veranstaltungView>();            
            for(int j = 0; j < 3; j++) {
                semester.add(new veranstaltungView("Veranstaltung "+i+"."+j));
            }
            veranstaltungenList.add(semester);
        }  
        
        
        
        //Adapter erstellen und setzen
        ExpandableListView elv = getExpandableListView();        
        mAdapter = new MyExpandableListAdapter();
        setListAdapter(mAdapter);
        
        elv.setItemsCanFocus(false);
        elv.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);
        
        elv.setOnChildClickListener(cl);
    }



    public class MyExpandableListAdapter extends BaseExpandableListAdapter {        
    
        // Parent/Group Elemente
        private String[] semesterArray = { 
                "SEMESTER1",
                "SEMESTER2", 
                "SEMESTER3",
                "SEMESTER4", 
                "SEMESTER5",
                "SEMESTER6"};
    
        public Object getChild(int groupPosition, int childPosition) {
            return veranstaltungenList.get(groupPosition).get(childPosition).text;
        }
    
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
    
        public int getChildrenCount(int groupPosition) {
            return veranstaltungenList.get(groupPosition).size();
        }
    
        public TextView getGenericView() {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
            TextView textView = new TextView(ApiDemos.this);

            textView.setTextColor(WHITE);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }
    
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            
            TextView textView = null;            
            textView = getGenericView();            
            textView.setText((String) getChild(groupPosition, childPosition));
            textView.setClickable(true);            
            textView.setSelected(veranstaltungenList.get(groupPosition).get(childPosition).marked);
            
            if(veranstaltungenList.get(groupPosition).get(childPosition).marked == true)
                 textView.setTextColor(RED);
            else textView.setTextColor(WHITE);
            
            //textView.setOnClickListener(cl1);
            
            return textView;
        }
    
        public Object getGroup(int groupPosition) {
            return semesterArray[groupPosition];
        }
    
        public int getGroupCount() {
            return veranstaltungenList.size();
        }
    
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
    
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;
        }
    
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    
        public boolean hasStableIds() {
            return true;
        }        
    }
}
Vielen Dank im Voraus!
 
Zuletzt bearbeitet:

Ähnliche Themen

B
Antworten
6
Aufrufe
1.016
jogimuc
J
A
Antworten
10
Aufrufe
1.869
swa00
swa00
H
  • HoustonWeHaveAProblem
Antworten
18
Aufrufe
957
jogimuc
J
Zurück
Oben Unten