grouprow xml中包含2个TextView的可扩展列表视图

时间:2013-04-19 15:20:08

标签: android listview expandablelistview

我有一个奇怪的问题,这是我第三天搜索StackOverflow和谷歌,寻找答案。到目前为止,我已经头疼了:) 我还是个新手,请原谅这个问题。

我正在创建一个应用程序,按年份,月份和类别(即杂货,租金等)汇总支出。我想以下列方式使用ExpandableListview显示此数据

Year:2013 ------Total Spent 1,234.56 Total Income: 7,890.11 (this is the group header)
    Apr: Spent 4,000.00 ------Recv: 6,0000 (child rows opened on clicking header)
    Mar: Spent 3,500.00 ------Recv: 5,0000
    Feb: Spent 2,000.00 ------Recv: 4,0000
    Jan: Spent 1,500.00 ------Recv: 3,0000

最后点击这些孩子中的任何一个,用户将被转移到一个页面,显示金额是按类别划分的。

问题:有没有办法显示

Year:2013 ------Total Spent 1,234.56 Total Income: 7,890.11 
标题组行中的

?如果是这样,组xml会是什么样子。

我尝试了以下作为grouprow标题,但应用程序只是在启动时崩溃。我认为你只能在grouprow标题中有一个文本项。事实上,即使在xml文件中只描述了一个文本项,即使尝试使用相对等布局也会崩溃。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="top"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" >
    <CheckedTextView
        android:drawableLeft="@drawable/green_check"
        android:gravity="left|top"
        android:height="25dp"
        android:id="@+id/txtYear"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:layout_width="wrap_content"
        android:text="Year"
        android:textStyle="bold"
        android:typeface="sans" />
    <TextView
        android:gravity="center|center_vertical|top"
        android:height="@dimen/activity_horizontal_margin"
        android:id="@+id/txtSentAmountYr"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        android:layout_width="wrap_content"
        android:numeric="decimal"
        android:padding="3dp"
        android:text="Total Sent"
        android:textColor="#442a8d" />
    <TextView
        android:gravity="center|center_vertical|top"
        android:height="25dp"
        android:id="@+id/txtRecvdAmountYr"
        android:layout_alignBaseline="@+id/txtYear"
        android:layout_alignBottom="@+id/txtYear"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/txtSentAmountYr"
        android:layout_weight="3"
        android:layout_width="wrap_content"
        android:numeric="decimal"
        android:text="Total Received"
        android:textColor="#28a928"
        android:textStyle="normal"
        android:typeface="normal" />
</RelativeLayout>

如果它不可能我正在考虑使用带有文本等的TableLayout来做同样的事情。但是,可扩展列表视图非常适合视觉和真实状态。 谢谢Dev's


新问题

我设法采用了Krishna和JFrankie提供的答案。然而,我最终遇到了一系列新问题:(

我有一个扩展BaseExpandableListAdapter的类,以便我可以修改我的标题视图。

因此,我在显示来自数据库的数据时遇到了很大的问题,因为为了根据我看到的所有示例从数据库中检索,您需要扩展SimpleCursorTreeAdapter。 (我不知道你是否可以通过2个差异类扩展你的课程?)

我尝试了一个奇怪的工作方式但是只返回标题组而没有子节点的kludgy解决方案(如下面粘贴的那样)。实际上,它一遍又一遍地重复该组中的第一个记录。 (我很高兴到目前为止至少取得了进展)

问题:

一个。如何在同一个适配器中使用BaseExpandableListAdapter和SimpleCursorTreeAdapter?这样我可以获得自定义布局以及从DB读取数据吗?

湾我已经浏览了样本,即谷歌的ExpandableListView 1和2,但对于一个新手,只是没有足够的点来连接如何做不同的实现。 感谢JFrankie我的代码我研究创建适配器。

继承人自定义适配器

public class ExpandableListViewAdapterCustom extends BaseExpandableListAdapter {
    protected Activity currentActivity;
    public ExpandableListViewAdapterCustom(Activity callingActivity){
    this.currentActivity = callingActivity;
    }

    private Cursor mGroupsCursorLocal ;
    private Cursor mChildCursor;
    private Context ctx;
    private int groupItem;
    private int childItem;
    private String[] fieldsToUseFromGroupCursor;
    private int[] screenTextsToMapGroupDataTo;
    private String[] fieldsToUseFromChildCursor;
    private int[] screenTextsToMapChildDataTo;

    public ArrayList<String> tempChild;
    public LayoutInflater minflater;
    public Activity activity;
    public int intGroupTotal;


    public void setCurrentActivity(Activity activity) {
       this.activity = activity;
    }

    public void setCtx(Context ctx) {
       this.ctx = ctx;
    }

     public void setGroupItem(int groupItem) {
         this.groupItem = groupItem;
      }

    public void setChildItem(int childItem) {
        this.childItem = childItem;
    }

    public Activity getCurrentActivity() {
        return currentActivity;
    }

     public Cursor getmGroupsCursorLocal() {
        return mGroupsCursorLocal;
      }

    public Context getCtx() {
        return currentActivity.getBaseContext();
    }

    public void setmGroupsCursorLocal(Cursor mGroupsCursor) {
        this.mGroupsCursorLocal = mGroupsCursor;
    }

    public ExpandableListViewAdapterCustom(Cursor mGroupsCursor,
                                       Activity activity,
                                       int groupItem,
                                       int childItem,
                                       String[] fieldsToUseFromGroupCursor,
                                       int[] screenTextsToMapGroupDataTo,
                                       String[] fieldsToUseFromChildCursor,
                                       int[] screenTextsToMapChildDataTo) {



        DatabaseRoutines db = new DatabaseRoutines(activity);

        setmGroupsCursorLocal(mGroupsCursor);
        mGroupsCursorLocal = db.fetchGroup();
        activity.startManagingCursor (mGroupsCursor);
        mGroupsCursorLocal.moveToFirst();

        mChildCursor=db.fetchChildren(mGroupsCursorLocal.getColumnIndex("Year"));
        mChildCursor.moveToFirst();
        activity.startManagingCursor(mChildCursor);



        setCtx(activity);
        setCurrentActivity(activity);

        }

        public void setInflater(LayoutInflater mInflater, Activity act) {
        this.minflater = mInflater;
        activity = act;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
           return null;
        }

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


        @Override
        public View getChildView(int groupPosition, 
            int childPosition,boolean         
            isLastChild, 
            View convertView, 
            ViewGroup parent) {
           View v = convertView;
           if (v == null) 
        {
        LayoutInflater inflater = 
       (LayoutInflater)     ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            
       v = inflater.inflate(R.layout.exp_listview_childrow, parent, false);
        }

        TextView txtMonth = (TextView) v.findViewById(R.id.txtMonth);
        TextView txtMonthAmountSent = (TextView)
        v.findViewById(R.id.txtMonthAmountSentValue);
        TextView txtMonthReceived = (TextView)
        v.findViewById(R.id.txtMonthAmountReceivedValue);
        txtMonth.setText(mChildCursor.getString(mChildCursor.getColumnIndex("Month")));

    txtMonthAmountSent.setText
   (mChildCursor.getString(mChildCursor.getColumnIndex("TotalSent")));
    txtMonthReceived.setText

    (mChildCursor.getString(mChildCursor.getColumnIndex("TotalReceived")));
    return v;
     }


    @Override
    public int getChildrenCount(int groupPosition) {
        return (mChildCursor.getCount());
       }

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

    @Override
    public int getGroupCount() {
    return mGroupsCursorLocal.getCount();
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
    }

    @Override
     public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
    }

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


    @Override
    public View getGroupView(
       int groupPosition, 
       boolean isExpanded,
       View convertView,     
       ViewGroup parent) 
     {
         View v = convertView;
         if (v == null) {
        LayoutInflater inflater =  
        (LayoutInflater)  ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.exp_listview_groupheader, parent, false);
       }

       TextView txtYear = (TextView) v.findViewById(R.id.txtYearValue);
       TextView txtAmountSent = (TextView) v.findViewById(R.id.txtAmountSentValue);
       TextView txtAmountRecieved = (TextView) 
       v.findViewById(R.id.txtAmountReceivedValue);

       txtYear.setText(mGroupsCursorLocal.getString(
       mGroupsCursorLocal.getColumnIndex("Year")));

       txtAmountSent.setText(
       mGroupsCursorLocal.getString(mGroupsCursorLocal.getColumnIndex("TotalSent")));

       txtAmountRecieved.setText(
      GroupsCursorLocal.getString(mGroupsCursorLocal.getColumnIndex("TotalReceived")));
      return v;
    }

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

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


}

数据库代码是这样的

public Cursor fetchGroup() {
    SQLiteDatabase db = this.getReadableDatabase();  //if memory leaks check here
        String query = "SELECT DISTINCT MIN(ID) AS id, 
    Year, SUM(SentAmount) AS    TotalSent, SUM(ReceivedAmount) AS TotalReceived 
     FROM  MyTbl GROUP BY Year ORDER BY Year DESC ";
      return db.rawQuery(query, null);}

public Cursor fetchChildren(int Yr) {
    SQLiteDatabase db = this.getReadableDatabase(); //if memory leaks check here
    String query = "SELECT  MIN(ID) AS id, 
    Year, Month, SUM(SentAmount) AS TotalSent, 
     SUM(ReceivedAmount) AS TotalReceived        
     FROM  MyTbl Where Year= "+ Yr +"   GROUP BY Year, 
     Month ORDER BY Year DESC, Month DESC";
    return db.rawQuery(query, null);
   }

2 个答案:

答案 0 :(得分:1)

如果您想创建像这样的标题视图

Year:2013 ------Total Spent 1,234.56 Total Income: 7,890.11 

创建一个扩展BaseExpandableListAdapter的示例类,并在getGroupview

覆盖方法,使用标头的用户布局和getChildview方法,使用

的布局

儿童的。

Note :     @Override
public int getGroupCount() {
    return Size of Group item;
}

为了更好的答案,请提供logcat错误..

答案 1 :(得分:0)

您是否尝试过实现自定义适配器并覆盖方法getGroupView并使您的布局膨胀? 您应该发送日志,以便我们可以尝试了解错误。 例如

android:layout_toLeftOf="@+id/txtSentAmountYr"

不正确..删除'+',alson in alignBottom等等。 如果您想了解有关创建自定义可扩展适配器的信息,请查看我的博客here