【问题标题】:How to add data to the Expandable listview with child having multiple views如何使用具有多个视图的子级将数据添加到可扩展列表视图
【发布时间】:2013-10-03 08:45:09
【问题描述】:

我参考了本教程的可扩展列表视图

http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

我的主要活动

  BusActivity extends Activity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild,listDataStart,listDataEnd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
            // get the listview
    expListView = (ExpandableListView) findViewById(R.id.lvExp);



    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild,listDataStart,listDataEnd);

    // setting list adapter
    expListView.setAdapter(listAdapter);

    // Listview Group click listener
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // Toast.makeText(getApplicationContext(),
            // "Group Clicked " + listDataHeader.get(groupPosition),
            // Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Expanded",
                    Toast.LENGTH_SHORT).show();
        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Collapsed",
                    Toast.LENGTH_SHORT).show();

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(
                    getApplicationContext(),
                    listDataHeader.get(groupPosition)
                            + " : "
                            + listDataChild.get(
                                    listDataHeader.get(groupPosition)).get(
                                    childPosition), Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
    });
}

/*
 * Preparing the list data
 */
private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();
    listDataStart = new HashMap<String, List<String>>();
    listDataEnd = new HashMap<String, List<String>>();
    // Adding child data
    listDataHeader.add("Volvo");
    listDataHeader.add("Deluxe");
    listDataHeader.add("Express");
    listDataHeader.add("Ordinary");
    // Adding child data
    List<String> volvo = new ArrayList<String>();
    volvo.add("The Shawshank Redemption");
    volvo.add("The Godfather");
    volvo.add("The Godfather: Part II");
    volvo.add("Pulp Fiction");


    List<String> nowShowing = new ArrayList<String>();
    nowShowing.add("The Conjuring");
    nowShowing.add("Despicable Me 2");
    nowShowing.add("Turbo");
    nowShowing.add("Grown Ups 2");

            List<String> now = new ArrayList<String>();
    now.add("The Conjuring");
    now.add("Despicable Me 2");
    now.add("Turbo");
    now.add("Grown Ups 2");

    List<String> comingSoon = new ArrayList<String>();
    comingSoon.add("2 Guns");
    comingSoon.add("The Smurfs 2");
    comingSoon.add("The Spectacular Now");
    comingSoon.add("The Canyons");


    List<String> comingup = new ArrayList<String>();
    comingup.add("2 Guns");
    comingup.add("The Smurfs 2");
    comingup.add("The Spectacular Now");
    comingup.add("The Canyons");


    listDataChild.put(listDataHeader.get(0), volvo); // Header, Child data
    listDataChild.put(listDataHeader.get(1), now);

    listDataStart.put(listDataHeader.get(0), nowShowing); // Header, Child data
    listDataStart.put(listDataHeader.get(1), comingup);
    );
    listDataEnd.put(listDataHeader.get(0), volvo); // Header, Child data
    listDataEnd.put(listDataHeader.get(2), comingSoon);

}

}

我的适配器

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild,listStart,listEnd;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
        HashMap<String, List<String>> listChildData,HashMap<String, List<String>> listStart, HashMap<String, List<String>> listEnd) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    System.out.println("Start"+listStart+"Bus"+listChildData+"End"+listEnd);
    this._listDataChild = listChildData;
    this.listStart=listStart;
    this.listEnd=listEnd;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

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

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


    final String childText = (String) getChild(groupPosition, childPosition);
    System.out.println("Child Text"+childText);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);

    }

    TextView txtListBus= (TextView) convertView
            .findViewById(R.id.txt_busno);
    if(groupPosition==0)
    {
        //txtListBus.setBackgroundColor(Color.parseColor("#FED966"));
        //txtListBus.setTextColor(Color.parseColor("#A07900"));
    }
    else if(groupPosition==1)
    {
        txtListBus.setBackgroundColor(Color.parseColor("#FED966"));
        txtListBus.setTextColor(Color.parseColor("#A07900"));
    }
    else if(groupPosition==2)
    {
        txtListBus.setBackgroundColor(Color.RED);
        txtListBus.setTextColor(Color.BLACK);
    }
    else
    {
        txtListBus.setBackgroundColor(Color.BLUE);
        txtListBus.setTextColor(Color.WHITE);

    }
    txtListBus.setText(childText);
    TextView txtList= (TextView) convertView
            .findViewById(R.id.txt_start);
    if(groupPosition==0)
    {
        txtList.setBackgroundColor(Color.parseColor("#FED966"));
        txtList.setTextColor(Color.parseColor("#A07900"));
    }
    else if(groupPosition==1)
    {
        txtList.setBackgroundColor(Color.parseColor("#64D8CD"));
        txtList.setTextColor(Color.parseColor("#067B6F"));
    }
    else if(groupPosition==2)
    {
        txtList.setBackgroundColor(Color.RED);
        txtList.setTextColor(Color.BLACK);
    }
    else
    {
        txtList.setBackgroundColor(Color.BLUE);
        txtList.setTextColor(Color.WHITE);
    }
    txtList.setText(childText);
    TextView txt= (TextView) convertView
            .findViewById(R.id.txt_end);
    if(groupPosition==0)
    {
        txt.setBackgroundColor(Color.parseColor("#FED966"));
        txt.setTextColor(Color.parseColor("#A07900"));
    }
    else if(groupPosition==1)
    {
        txt.setBackgroundColor(Color.parseColor("#64D8CD"));
        txt.setTextColor(Color.parseColor("#067B6F"));
    }
    else if(groupPosition==2)
    {
        txt.setBackgroundColor(Color.RED);
        txt.setTextColor(Color.BLACK);
    }
    else
    {
        txt.setBackgroundColor(Color.BLUE);
        txt.setTextColor(Color.WHITE);
    }

    txt.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

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

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

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

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




    String headerTitle = (String) getGroup(groupPosition);
    System.out.println("Group position"+groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.ls_head);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);
    if(groupPosition==0)
    {
        lblListHeader.setBackgroundColor(Color.parseColor("#FFD243"));
        lblListHeader.setTextColor(Color.parseColor("#A07900"));
    }
    else if(groupPosition==1)
    {
        lblListHeader.setBackgroundColor(Color.parseColor("#00BFAC"));
        lblListHeader.setTextColor(Color.parseColor("#067B6F"));
    }
    else if(groupPosition==2)
    {
        lblListHeader.setBackgroundColor(Color.RED);
        lblListHeader.setTextColor(Color.BLACK);
    }
    else
    {
        lblListHeader.setBackgroundColor(Color.BLUE);
        lblListHeader.setTextColor(Color.WHITE);
    }

    return convertView;
}

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

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

}

list_item 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:background="#fff"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/txt_start"
        android:layout_width="130dp"
        android:layout_height="match_parent"
        android:text="Hello"
         android:gravity="right|center_vertical"

        android:textColor="#000"
        android:textSize="17dp" />

    <LinearLayout
        android:layout_width="50dp"
        android:layout_height="55dp"
        android:background="@drawable/ic_launcher"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt_busno"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"

            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    <TextView
        android:id="@+id/txt_end"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="Hello"
        android:textColor="#000"
        android:textSize="17dp" />

</LinearLayout>

list_group 与链接保持一致

在这个子列表中只有一个文本视图。我想在子列表中添加一个 imageview 和一个 textview。我可以通过更改 list_item.xml 以包含视图来添加它。如何向这个具有多个视图的子视图添加数据?

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    根据教程

    添加视图(ImageView 和 TextView 到你的)list_item.xml

    然后在你的 ExpandableListAdapter 更新下面的方法

     @Override
        public View getChildView(int groupPosition, final int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
    
            final String childText = (String) getChild(groupPosition, childPosition);
    
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_item, null);
            }
    
            TextView txtListChild = (TextView) convertView
                    .findViewById(R.id.lblListItem);
    
              // Get Id's of new added Text views here
    
              TextView myNewText= (TextView) convertView
                    .findViewById(R.id.new_id);
             // Get Id's of new added Image views here
    
               ImageView myImg= (ImageView) convertView
                    .findViewById(R.id.new_id_img);
    
    
    
            txtListChild.setText(childText);
    
            // Set Text & Image Here
    
              myNewText.setText(childText);// use your desired text to display instead of 'childText'
              myImg.setBackgroundResource(R.drawable.ic_launcher);
    
            return convertView;
        }
    

    如何将数据表单活动传递给适配器

    在您的 prepareListData() 方法中

    添加

    List<String> newList = new ArrayList<String>();
            newList.add("2 Guns");
            newList.add("The Smurfs 2");
            newList.add("The Spectacular Now");
            newList.add("The Canyons");
            newList.add("Europa Report");
    

    在方法的底部添加这个

     listDataChild.put(listDataHeader.get(3), newList);
    

    【讨论】:

    • 我得到了这个并且能够做到这一点。如何将数据从主要活动传递给这个 txtListChild 和 myNewText。
    • 你能帮我解决这个问题吗?
    • 在标题的相同位置 3 我必须插入 3 textview、imageview 和其他 textview 的数据。我该怎么做
    • 详细说明您的要求
    • 我能够将文本设置为只有一个这样的视图,即使用您上面提供的代码的文本视图。在同一位置 listDataHeader.get(3) 我有一个 textview、一个 imageview 和另一个 textview。当您在同一个标​​题下有 3 个子视图时,我不知道如何设置数据。
    猜你喜欢
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多