我已经有一个listview我想将它转换为可扩展的ListView

时间:2015-04-21 06:16:04

标签: android listview android-listview expandablelistview

以下是我的基本适配器代码。我将使用此类作为ListView的适配器。

public class CustombaseAdapterTxnCartSelect extends BaseAdapter
{
    private Context mContext;
    private List<RowItemTxnCartSelect> mRowItems;

    TextView standDesc, seats, totalTicketTariff, eventDesc;

    public CustombaseAdapterTxnCartSelect(Context context, List<RowItemTxnCartSelect> rowItems) {
        mContext = context;
        mRowItems = rowItems;       
    }

    @Override
    public int getCount() {
        return mRowItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mRowItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mRowItems.indexOf(getItem(position));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = null;

        LayoutInflater mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflator.inflate(R.layout.event_layout_txn__cart_select, null);
        standDesc = (TextView) convertView.findViewById(R.id.textViewTxnCartSelectStandDesc);
        seats = (TextView) convertView.findViewById(R.id.textViewTxnCartSelectSeats);
        totalTicketTariff = (TextView) convertView.findViewById(R.id.textViewTxnCartSelectTotalTicketTariff);
        eventDesc = (TextView) convertView.findViewById(R.id.textViewEventDesc);

        RowItemTxnCartSelect row = (RowItemTxnCartSelect) getItem(position);
        standDesc.setText(row.getmStandDesc());
        seats.setText(row.getmSeats());
        totalTicketTariff.setText(row.getMtotalTicketTariff());
        eventDesc.setText(row.getmEventDesc());

        return convertView;
    }
}

我只想将其转换为可扩展的列表视图我不清楚我必须添加的内容。

2 个答案:

答案 0 :(得分:0)

也许你可以这样:

public class FevList extends ExpandableListActivity {
    String title;
    String url;
    SQLiteDatabase database;
    DbUtil db;
    HashMap<String, String> map = new HashMap<String, String>();
    ArrayList<HashMap<String, String>> subjectList = new ArrayList<HashMap<String, String>>();


    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.exlist);
            db = new DbUtil();
            database = db.openConnection(this);
            // Thread for getting values of title from DataBase.
            SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
                    this, createGroupList(), R.layout.group_row,
                    new String[] { "subject" }, new int[] { R.id.row_name },
                    createChildList(), R.layout.child_row,
                    new String[] { "title" }, new int[] { R.id.grp_child });
            setListAdapter(expListAdapter);
            registerForContextMenu(getExpandableListView());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

//适配器代码

/ *为行创建Hashmap * /                 的ArrayList&GT; result = new ArrayList&gt;();

            @SuppressWarnings("unchecked")
            private List createGroupList() {

// write your code here.

                return (List) result;

            }

            /* creatin the HashMap for the children */

            @SuppressWarnings("unchecked")
            private List createChildList() {

// write your code here.
                return result;
            }

            public void onContentChanged() {

                System.out.println("onContentChanged");

                super.onContentChanged();

            }

            /* This function is called on each child click */

            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {

// write your code here.

                return true;

            }

            /* This function is called on expansion of the group */

            public void onGroupExpand(int groupPosition) {

                try {

                    System.out.println("Group exapanding Listener => groupPosition = "
                            + groupPosition);

                } catch (Exception e) {

                    System.out.println(" groupPosition Errrr +++ " + e.getMessage());

                }

            }
        }

这是一个片段,您可以参考this

答案 1 :(得分:0)

Here是一个用于演示ExpandableListView的Opensource示例项目。这是开始使用Expandable Listviews的好例子。