0

学習目的で書いているコードで SimpleCursorTreeAdapter を使用しようとしています。何が起こっているのかというと、親をクリックすると展開されて子が表示されますが、それ以上何もできません。デバッガーを使用してコードを調べたところ、looper.loop 内で終わりのないループに陥っていることに気付きました。以下のスニペットで実際に機能するすべてのものをコメントアウトしましたが、それでもハングします。以下のスニペットでは、2 番目の親をクリックすると展開されることが予想されます (矢印が変化することで示されます)。私が何をしようとしているのかを示すために、コードに古いものを残しました (ただしコメントアウトしました)。

アドバイスや提案をいただければ幸いです。

    private void showEvents(final Cursor cursor) {

    ExpandableListView lv1 = this.getExpandableListView();



    SimpleCursorTreeAdapter  adapter = new SimpleCursorTreeAdapter (this, cursor, R.layout.simplerow, FROM, TO, 
            R.layout.childrow, FROMCHILD, TOCHILD) {
        //@Override
        //public void setViewText(TextView v, String text) {
        //  super.setViewText(v, convText(v, text, cursor));
        //}

        @Override
        protected Cursor getChildrenCursor(Cursor parentCursor) {

            int groupPos = parentCursor.getPosition(); 
              Log.d("TEST", "getChildrenCursor() for groupPos " + groupPos); 

            String sql = "select f._id, f.NAME as ASSETCLASS, "
                    + "f.value as AMOUNT, "
                    + "0 as PERCENTAGE, "
                    + "0 as TARGET "
                    + "from funds f "
                    + "where f.asset_class = " + parentCursor.getInt(0) + ";";

            sql = "select f._id, f.NAME as FUNDNAME, "
                    + "f.value as AMOUNT_OWNED "
                    + "from funds f "
                    + "where 1  = 1;";



            Log.d("TEST", sql);

            //SQLiteDatabase db = pfdata.getReadableDatabase();

            //Cursor cursor = db.rawQuery(sql, null);
            //startManagingCursor(cursor);
            //return cursor;

            //Log.d("TEST", Integer.toString(cursor.getCount()));
            //return cursor;
            return null;
        }

    };

    // Log.d("TEST", Integer.toString(adapter.getCount()));

    lv1.setAdapter(adapter);

}

提案を試みましたが、動作に変化は見られませんでした。これが私が現在持っているものです:

    private void showEvents(final Cursor cursor) {

    ExpandableListView lv1 = this.getExpandableListView();


    MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(cursor, this, 
            R.layout.simplerow, R.layout.childrow, 
               FROM, TO, 
               FROMCHILD, TOCHILD); 
        lv1.setAdapter(mAdapter); 
       }

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter { 


public MyExpandableListAdapter(Cursor cursor, Context context, 
        int groupLayout, int childLayout, String[] groupFrom, 
        int[] groupTo, String[] childrenFrom, int[] childrenTo) { 
    super(context, cursor, groupLayout, groupFrom, groupTo, 
            childLayout, childrenFrom, childrenTo); 
} 

@Override 
protected Cursor getChildrenCursor(Cursor parentCursor) { 

    String sql = "select f._id, f.NAME as ASSETCLASS, "
            + "f.value as AMOUNT, "
            + "0 as PERCENTAGE, "
            + "0 as TARGET "
            + "from funds f "
            + "where f.asset_class = " + parentCursor.getInt(0) + ";";

    Log.d("TEST", sql);

    return null;        
} 

実際にクエリを実行してカーソルを返すようにしてみましたが、変わらないので基本に立ち返ってみました。何が起こっているのかについて誰かが何か考えを持っているなら、私はそれを大いに感謝します.

4

1 に答える 1

3

あなたの問題はSimpleCursorTreeAdapter、拡張するのではなくオーバーライドしているためだと思います。それを行うときは、リストを操作するために使用するすべてのメソッドを再作成する必要があります (まだ行っていません)。

不足しているすべてのメソッドを追加するか、それを拡張SimpleCursorTreeAdapterしてアダプターとして使用する新しいクラスを作成します。最後の (IMO) は、最初よりもはるかに簡単です。必要なメソッドをオーバーライドするだけです。

私のプロジェクトの 1 つの例を次に示します。

mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
       R.layout.rowlayout_expgroup, R.layout.rowlayout_itemlist_exp,
       new String[] { "_id" }, new int[] { android.R.id.text1 },
       new String[] { GroceryDB.ITEM_NAME, GroceryDB.LISTITEM_QTY,
               GroceryDB.ITEM_UNIT }, new int[] { R.id.ListItem1,
               R.id.ListItem3, R.id.ListItem2 });
lv.setAdapter(mAdapter);

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
    public MyExpandableListAdapter(Cursor cursor, Context context,
            int groupLayout, int childLayout, String[] groupFrom,
            int[] groupTo, String[] childrenFrom, int[] childrenTo) {
        super(context, cursor, groupLayout, groupFrom, groupTo,
                childLayout, childrenFrom, childrenTo);
    }

    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        Cursor childCursor = mDbHelper.fetchChildren(GroceryListMain.group,
                groupCursor.getString(groupCursor.getColumnIndex("_id")),
                ListFragment_ShopList.listId);
        getActivity().startManagingCursor(childCursor);
        childCursor.moveToFirst();
        return childCursor;
    }

    protected void bindChildView(View view, Context context, Cursor cursor,
            boolean isLastChild) {
        TextView name = (TextView) view.findViewById(R.id.ListItem1);
        TextView qty = (TextView) view.findViewById(R.id.ListItem3);
        TextView unit = (TextView) view.findViewById(R.id.ListItem2);
        TextView cost = (TextView) view.findViewById(R.id.ListItem4);

        name.setText(cursor.getString(2));
        qty.setText(cursor.getString(1));
        unit.setText(cursor.getString(4));
        DecimalFormat df = new DecimalFormat("0.00");
        Double hold = Double.valueOf(cursor.getString(3));
        Double qtyhold = Double.valueOf(cursor.getString(1));
        Double total = hold * qtyhold;
        cost.setText("$" + df.format(total));
    }
}
于 2012-06-23T04:25:54.963 に答える