2

我有一个奇怪的问题,这是我拖网 StackOverflow 和 Google 的第三天,寻找答案。到目前为止,我感到头疼:) 我还是个新手,所以请原谅这个问题。

我正在创建一个按年、月和类别(即杂货、租金等)汇总支出的应用程序。我想以下列方式使用 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 来做同样的事情。然而,可扩展列表视图非常适合视觉和房地产用途。感谢开发者


新问题

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

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

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

我尝试了一个笨拙的解决方案(如下所示),它奇迹般地工作,但只返回标题组而没有孩子。实际上它一遍又一遍地重复组中的第一条记录。(我很高兴至少到目前为止取得了进展)

问题:

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

湾。我已经通过谷歌浏览了示例,即 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);
   }
4

2 回答 2

1

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

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

在 getGroupview 中创建扩展 BaseExpandableListAdapter 的示例类

覆盖方法,对标题使用用户布局,在 getChildview 方法中,将布局用于

孩子们。

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

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

于 2013-04-19T18:21:43.030 回答
0

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

android:layout_toLeftOf="@+id/txtSentAmountYr"

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

于 2013-04-19T18:01:39.023 回答