我在活动中没有获得消耗列表视图?

时间:2017-09-25 09:45:25

标签: android

我的活动中没有Expendable列表视图。 我的活动包含两个图像视图,然后是一个消耗性列表视图,但显示的是图像,但不显示列表视图。 请帮帮我。 在这里,我发布了我的代码。

MainActivity.java这是我的主要活动java文件

public class MainActivity extends Activity {

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dropdown_menu);

    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    // preparing list data
    prepareListData();

    listAdapter = new ExpendableListAdapter(this, listDataHeader, listDataChild) {



        @Override
        public int getGroupCount() {
            return 0;
        }

        @Override
        public int getChildrenCount(int i) {
            return 0;
        }

        @Override
        public Object getGroup(int i) {
            return null;
        }

        @Override
        public Object getChild(int i, int i1) {
            return null;
        }

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

        @Override
        public long getChildId(int i, int i1) {
            return 0;
        }

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

        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            return null;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            return null;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return false;
        }

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

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

        @Override
        public void onGroupExpanded(int i) {

        }

        @Override
        public void onGroupCollapsed(int i) {

        }

        @Override
        public long getCombinedChildId(long l, long l1) {
            return 0;
        }

        @Override
        public long getCombinedGroupId(long l) {
            return 0;
        }
    };

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

/*
 * Preparing the list data
 */
private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("EXOTIC AND FROZEN");
    listDataHeader.add("FRUITS");
    listDataHeader.add("VEGETABLES");

    // Adding child data
    List<String> EXOTIC = new ArrayList<String>();
    EXOTIC.add("ALL EXOTIC AND FROZEN");
    /*top250.add("The Godfather");
    top250.add("The Godfather: Part II");
    top250.add("Pulp Fiction");
    top250.add("The Good, the Bad and the Ugly");
    top250.add("The Dark Knight");
    top250.add("12 Angry Men");*/

    List<String> FRUITS = new ArrayList<String>();
    FRUITS.add("APPLE");
    FRUITS.add("ORANGES");
    FRUITS.add("PINEAPPLE");
    FRUITS.add("BANANA");
    FRUITS.add("GRAPES");
    FRUITS.add("POMEGRANATE");

    List<String> VEG = new ArrayList<String>();
    VEG.add("CAULIFLOWER(GOBHI)");
    VEG.add("POTATOES");
    VEG.add("LADY FINGER");
    VEG.add("TOMATOES");
    VEG.add("EGG PLANT(BAINGAN)");
    listDataChild.put(listDataHeader.get(0), EXOTIC); // Header, Child data
    listDataChild.put(listDataHeader.get(1), FRUITS);
    listDataChild.put(listDataHeader.get(2), VEG)}}

ExpendableListAdapter.java这是我的适配器java文件

public class ExpendableListAdapter 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;

public ExpendableListAdapter(Context _context,List<String> _listDataHeader,HashMap<String,List<String>> _listDataChild)
{

   this._context=_context;
   this._listDataHeader=_listDataHeader;
   this._listDataChild=_listDataChild;}

@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);

    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);

    txtListChild.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);
    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.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

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

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

dropdown_menu.xml我项目中的主要xml布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f4f4f4">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="160dp"
    android:id="@+id/imageView22"
    android:background="@drawable/shopp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_height="30dp"
    android:id="@+id/imageView23"
    android:background="@drawable/sa"
    android:layout_below="@+id/imageView22"
    android:layout_centerHorizontal="true" />

<ExpandableListView
    android:id="@+id/lvExp"
    android:layout_marginTop="5dp"
    android:layout_below="@+id/imageView23"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

list_group.xml这是消耗性列表视图中父级的xml文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#000000">


<TextView
    android:id="@+id/lblListHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="?
    android:attr/expandableListPreferredItemPaddingLeft"
    android:textSize="17dp"
    android:textColor="#f9f93d" />

list_item.xml这是我父母的孩子的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="vertical" >

<TextView
    android:id="@+id/lblListItem"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="17dip"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

1 个答案:

答案 0 :(得分:3)

试试这个,

    listAdapter = new ExpendableListAdapter(this, listDataHeader, listDataChild) ;
    xpListView.setAdapter(listAdapter); 
相关问题