可扩展的列表视图与2 TextViews在grouprow xml



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

我正在创建一个按年、月和类别(如杂货、租金等)汇总支出的应用程序。我希望使用expandableelistview以以下方式显示此数据

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,这样我就可以修改我的头视图。

正因为如此,我有一个大问题显示数据从数据库中,因为为了从DB检索按照我看到的所有例子,你需要扩展SimpleCursorTreeAdapter。(我不知道你是否可以将你的类扩展为2个不同的类?)

我已经尝试了一个笨拙的解决方案(粘贴在下面),这是奇迹般地工作,但只返回头组和没有孩子。实际上,它在组中不断重复第一个记录。(我很高兴至少取得了进展)

问题:

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

b。我已经通过样例即expandableelistview 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);
   }

如果你想创建这样的头部视图

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错误…

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

android:layout_toLeftOf="@+id/txtSentAmountYr"

是不正确的…删除'+',还有在alignBottom等等。如果你想获得一些关于创建自定义可扩展适配器的信息,请查看我的博客

最新更新