Frage

Da ich noch nicht genug Reputationspunkte habe, um frühere Fragen zu kommentieren, musste ich eine neue erstellen. Ich brauchte eine erweiterbare Listenansicht, die nicht die gesamte Aktivität in Anspruch nahm. Deshalb habe ich dieses Beispiel verwendet, um dies ohne erweiterte Absichtlichkeit zu tun:

ExpanceableList View nicht erweitern

Mein leicht modifizierter Code:

public class Main extends Activity {

    ExpandableListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandable_list);
        lv = (ExpandableListView) this.findViewById(R.id.expandableListView1);
        MyExpandableListAdapter expandableAdapter = new MyExpandableListAdapter();
        lv.setAdapter(expandableAdapter);
    }

    class MyExpandableListAdapter extends BaseExpandableListAdapter {

    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
    private String[][] children = {
            { "Arnold", "Barry", "Chuck", "David" },
            { "Ace", "Bandit", "Cha-Cha", "Deuce" },
            { "Fluffy", "Snuggles" },
            { "Goldy", "Bubbles" }
    };


        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        public TextView getGenericView() {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 64);

            TextView textView = new TextView(Main.this);
            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 = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;

        }

        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        public int getGroupCount() {
            return groups.length;
        }

        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 hasStableIds() {
            return true;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }

}

Dies funktioniert gut, aber um es robuster zu machen, wollte ich die in Code erstellten Textviews in XML -Dateien trennen (dies ist eine gute Idee, oder?) Hier stoße ich auf einige FC -Probleme.

gruppe_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/row_name"
         android:paddingLeft="50px"
         android:textSize="20px"
         android:textStyle="normal"
         android:layout_width="320px"
         android:layout_height="wrap_content"/>
</LinearLayout>

Und ich habe dies in der obigen Java -Datei geändert:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    TextView parentRow = (TextView) findViewById(R.id.row_name);
    parentRow.setText(getGroup(groupPosition).toString());
    return parentRow;
}

Ich habe auch versucht, den Linearlayout -Wrapper zu entfernen, aber das hat nichts behoben. Kann mir jemand sagen, warum meine XML -Ansicht nicht funktioniert? Vielen Dank.

BEARBEITEN: Danke Flo, neuer Code, der mit dem Tutorial funktioniert:

gruppe_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/group_name"
    android:paddingLeft="50dp"
    android:textSize="30sp"
    android:textStyle="normal"
    android:layout_height="wrap_content"/>

Hauptsächlich:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {


        View parentView = convertView;
        if (parentView == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            parentView = vi.inflate(R.layout.group_row, null);
        }
                TextView parentText = (TextView) parentView.findViewById(R.id.group_name);
                if (parentText != null) {
                      parentText.setText(getGroup(groupPosition).toString());                     
                }

       return parentText;


    }
War es hilfreich?

Lösung

Ihre Implementierung der GetGroupView () -Methode ist falsch. Im Moment suchen Sie im Layout der Aktivität nach einem Element mit dem Element R.id.row_name was nicht existiert. Ich denke die Linie parentRow.setText(getGroup(groupPosition).toString()); wirft eine Ausnahme als parentRow ist Null.

Die korrekte Implementierung der Methode mit einem benutzerdefinierten Zeilenlayout aus einer XML -Datei besteht darin, dieses Layout aufzublasen. Kasse Dieses Tutorial. Es gilt für normale ListViews und Adapter, aber das Konzept, die Zeilenlayouts gleich aufzublasen.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top