簡體   English   中英

如何在ExpandableListView中添加多個Textviews

[英]How to add multiple Textviews in a ExpandableListView

我不知道如何在我的Expandablelist適配器和MainActivty中添加多個Textview。 每當我嘗試在適配器中添加新的Textview時,便無法弄清楚如何在MainAcivity中初始化Textview。 它只是將我在第一個Textview中放入的文本復制到新文本中。 我發現其他一些Stackoverflow帖子也對此有所考慮,但都沒有答案。 如果有人知道如何執行此操作,我將不勝感激。 請舉一個例子,因為我是初學者。 我遵循了來自http://www.tutorialsbuzz.com/2014/07/custom-expandable-listview-image-text.html的教程

在我的代碼中還可以看到一些注釋,以查看我想要做什么。

ExpandablelistAdapter:

import android.view.LayoutInflater;


                import android.content.Context;

                import android.view.View;
                import android.view.ViewGroup;
                import android.widget.BaseExpandableListAdapter;
                import android.widget.ImageView;
                import android.widget.TextView;

        import java.util.ArrayList;

        public class ExpandListAdapter extends BaseExpandableListAdapter {

            private Context context;
            private ArrayList<Group> groups;

            public ExpandListAdapter(Context context, ArrayList<Group> groups) {
                this.context = context;
                this.groups = groups;

            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                ArrayList<Child> chList = groups.get(groupPosition).getItems();
                return chList.get(childPosition);

            }

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

            @Override
            public View getChildView(int groupPosition, int childPosition,
                                     boolean isLastChild, View convertView, ViewGroup parent) {

                Child child = (Child) getChild(groupPosition, childPosition);
                if (convertView == null) {
                    LayoutInflater infalInflater = (LayoutInflater) context
                            .getSystemService(context.LAYOUT_INFLATER_SERVICE);
                    convertView = infalInflater.inflate(R.layout.child_item, null);

                }
                TextView tv = (TextView) convertView.findViewById(R.id.country_name);
//trying to add this textview ltv
                TextView ltv = (TextView) convertView.findViewById(R.id.large_text);
                ImageView iv = (ImageView) convertView.findViewById(R.id.flag);
                tv.setText(child.getName().toString());
                iv.setImageResource(child.getImage());
                ltv.setText(child.getName().toString());












                    return convertView;
            }

            @Override
            public int getChildrenCount(int groupPosition) l

            @Override
            public Object getGroup(int groupPosition) {
                return groups.get(groupPosition);
            }

            @Override
            public int getGroupCount() {
                return groups.size();
            }

            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                                     View convertView, ViewGroup parent) {
                Group group = (Group) getGroup(groupPosition);
                if (convertView == null) {
                    LayoutInflater inf = (LayoutInflater) context
                            .getSystemService(context.LAYOUT_INFLATER_SERVICE);
                    convertView = inf.inflate(R.layout.group_item, null);
                }
                TextView tv = (TextView) convertView.findViewById(R.id.group_name);
                tv.setText(group.getName());
                return convertView;
            }

            @Override
            public boolean hasStableIds() {
                return true;
            }

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

        }

主要活動:

    import java.util.ArrayList;
            import java.util.Set;

            import android.app.Activity;
            import android.content.Intent;
            import android.os.Bundle;

            import android.view.View;
            import android.widget.Button;
            import android.widget.ExpandableListView;





            public class Brandspage extends Activity {

                private ExpandListAdapter ExpAdapter;
                private ExpandableListView ExpandList;
                private ArrayList<Group> ExpListItems;


                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_brandspage);
                    Button b1 = (Button) findViewById(R.id.button4);
                    b1.setOnClickListener(new View.OnClickListener() {


                        @Override
                        public void onClick(View v) {
                            Intent i = new Intent(Brandspage.this, MainActivity.class);
                            startActivity(i);

                        }
                    });


                    ExpandList = (ExpandableListView) findViewById(R.id.exp_list);
                    ExpListItems = SetStandardGroups();
                    ExpAdapter = new ExpandListAdapter(Brandspage.this, ExpListItems);
                    ExpandList.setAdapter(ExpAdapter);


                }


                public ArrayList<Group> SetStandardGroups() {

//how to initialize the ltv textview here with the other strings 


                    String group_names[] = {"A-G", "H-N", "O-U", "V-Z"
                    };


                    String country_names[] = {

                            " " +

                                    "\n" +
                                    "\n" +
                                    "  \n" +
                                    "\n" +
                                    "\n" +
                                    "\n" +
                                    "  \n" +
                                    "\n" +
                                    " \n" +
                                    "\n" +
                                    " \n "

                                   };




                    int Images[] = {R.drawable.ic_launcher, R.drawable.ic_launcher,
                            R.drawable.ic_launcher, R.drawable.ic_launcher


                    };


                    ArrayList<Group> list = new ArrayList<Group>();

                    ArrayList<Child> ch_list;

                    int size = 20;
                    int j = 0;


                    for (String group_name : group_names) {
                        Group gru = new Group();
                        gru.setName(group_name);





                            ch_list = new ArrayList<Child>();
                            for (; j < size; j++) {
                                Child ch = new Child();
                                ch.setName(country_names[j]);


                                ch.setImage(Images[j]);
                                ch_list.add(ch);


                            }
                            gru.setItems(ch_list);
                            list.add(gru);

                            size = size + 20;
                        }

                        return list;


                    }


                }

子item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:layout_marginLeft="50dp"
    >

    <TextView
        android:id="@+id/country_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="20dp"
        android:layout_below="@+id/large_text"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="66dp" />

    <ImageView
        android:id="@+id/flag"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher"

        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />


    //Want this Large Text to be in the ExpandableListView
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/large_text"
            android:layout_below="@+id/flag"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

    </RelativeLayout>

您需要編輯Child類以添加另一個字段,例如Detail和該字段的getter setter方法。然后在

 public ArrayList<Group> SetStandardGroups()

您可以在ExpandListAdapter中獲取字段值,然后可以設置為第二個文本視圖。 BrandPage活動類別為

public class BrandPage extends ActionBarActivity {

private ExpandListAdapter ExpAdapter;
private ExpandableListView ExpandList;
private ArrayList<Group> ExpListItems;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_brand_page);
    Button b1 = (Button) findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            Intent i = new Intent(BrandPage.this, MyActivity.class);
            startActivity(i);

        }
    });


    ExpandList = (ExpandableListView) findViewById(R.id.exp_list);
    ExpListItems = SetStandardGroups();
    ExpAdapter = new ExpandListAdapter(BrandPage.this, ExpListItems);
    ExpandList.setAdapter(ExpAdapter);

}


public ArrayList<Group> SetStandardGroups() {

//如何在這里用其他字符串初始化ltv textview

    String group_names[] = {"A-G", "H-N", "O-U", "V-Z"
    };

    String country_names[] = {"1","2"};

    int Images[] = {R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher
    };


    ArrayList<Group> list = new ArrayList<Group>();
    ArrayList<Child> ch_list;
    int size = 2;
    int j = 0;

    for (String group_name : group_names) {
        Group gru = new Group();
        gru.setName(group_name);
        ch_list = new ArrayList<Child>();
        for (j=0; j < size; j++) {
            Child ch = new Child();
            // Set the fields
            ch.setDetails("item:"+j);
            ch.setName(country_names[j]);
            ch.setImage(Images[j]);
            ch_list.add(ch);
        }
        gru.setItems(ch_list);
        list.add(gru);
        //size = size + 2;
    }
    return list;
}

}

您的ExpandableAdapter類是

public class ExpandListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<Group> groups;

public ExpandListAdapter(Context context, ArrayList<Group> groups) {
    this.context = context;
    this.groups = groups;

}

@Override
public Object getChild(int groupPosition, int childPosition) {
    ArrayList<Child> chList = groups.get(groupPosition).getItems();
    return chList.get(childPosition);

}

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

@Override
public View getChildView(int groupPosition, int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    Child child = (Child) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_item, null);

    }
    TextView tv = (TextView) convertView.findViewById(R.id.country_name);
    //trying to add this textview ltv
    TextView ltv = (TextView) convertView.findViewById(R.id.large_text);
    ImageView iv = (ImageView) convertView.findViewById(R.id.flag);
    tv.setText(child.getName());
    iv.setImageResource(child.getImage());
    //Set the large textview
    ltv.setText(child.getDetails());


    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<Child> chList = groups.get(groupPosition).getItems();
    return chList.size();
}

@Override
public Object getGroup(int groupPosition) {
    return groups.get(groupPosition);
}

@Override
public int getGroupCount() {
    return groups.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    Group group = (Group) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.group_item, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.group_name);
    tv.setText(group.getName());
    return convertView;
}

@Override
public boolean hasStableIds() {
    return true;
}

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM