5

最近、拡張可能なリストビューをいじっています。

子ビューの要素の 1 つとしてチェックボックスを持つリスト ビューを取得しようとしています。

このチュートリアルhttp://mylifewithandroid.blogspot.com/2010/02/expandable-lists-and-check-boxes.htmlを見つけましたが、完璧に思えました。しかし、それをコンパイルして遊んでみると、非常にバグがあることに気付きました。あるグループのボックスをオンにすると、別のグループのランダムなボックスがオンまたはオフになる可能性があります。

これは私がこれについて見つけることができる唯一のチュートリアルです。多くのアプリで使用されるもののように思われるので、これを扱う他の良いチュートリアルやリソースはありますか?

またはさらに良いことに、これが機能するようになった独自のコードを表示したい人はいますか...

ありがとう

ケブ

4

2 に答える 2

3

NagiosクライアントをAndroidに開発するときに同じ問題がありましたが、両方のconvertViewパラメーターに新しい値を割り当てることが重要であることがわかりました

  • public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
  • public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)

BaseExpandableListAdapter拡張機能のメソッド。

そうしないと、親/子レンダラーがキャッシュから構築され、正しいものが表示されません。

監視対象のサービスに問題が発生した場合に、ユーザーが何らかのアラートを必要とするかどうかを示すチェックボックスが必要です。

これを達成する私の方法は次のとおりです。

//hosts: the list of data used to build up the hierarchy shown by this adapter's parent list.
private class MyExpandableListAdapter extends BaseExpandableListAdapter
{
    private LayoutInflater inflater;

    public MyExpandableListAdapter()
    {
        inflater = LayoutInflater.from(Binding.this);
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        final Host host = hosts.get(groupPosition);
        final boolean needsLargeView = isExpanded && (host.getTitle() != null) && (host.getTitle().length() > 0);
        if (needsLargeView)
            convertView = inflater.inflate(R.layout.grouprow_expanded, parent, false);
        else
            convertView = inflater.inflate(R.layout.grouprow, parent, false);
        convertView.setBackgroundResource(host.getBackgroundResource(isExpanded));
        [...]
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        final Host host = hosts.get(groupPosition);
        final NagService service = host.getServices().get(childPosition);
        convertView = inflater.inflate(R.layout.childrow, parent, false);
        convertView.setBackgroundResource(host.getChildBackgroundResource());
        convertView.findViewById(R.id.servicename_status).setBackgroundResource(service.getStatusBackground());
        [...]
        CheckBox alertChb = (CheckBox) convertView.findViewById(R.id.alert);
        alertChb.setChecked(service.isNeedsAlert());
        alertChb.setOnCheckedChangeListener(new YourCheckChangedListener(service));
        return convertView;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        return hosts.get(groupPosition).getServices().get(childPosition);
    }

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

    @Override
    public int getChildrenCount(int groupPosition)
    {
        return hosts.get(groupPosition).getServices().size();
    }

    @Override
    public Object getGroup(int groupPosition)
    {
        return hosts.get(groupPosition);
    }

    @Override
    public int getGroupCount()
    {
        return hosts.size();
    }

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

    @Override
    public void notifyDataSetChanged()
    {
        super.notifyDataSetChanged();
    }

    @Override
    public boolean isEmpty()
    {
        return ((hosts == null) || hosts.isEmpty());
    }

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

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

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

使用されるレイアウトの中で、childrow.xml はチェックボックスを含むものです。

内部ではCheckedChanhedListener、影響を受けるインスタンス (私の場合はサービス) に新しい状態を保存する必要があります。

これがお役に立てば幸いです

于 2011-04-12T07:32:52.370 に答える
0

これは私が見つけたもので、チェックボックスまたはリストビュー内のテキストビューであるかどうかにかかわらず、カスタムリストビューを作成する方法についての良いガイドラインが与えられています

http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html

ありがとう..

ラケシュ

于 2010-09-02T17:50:15.667 に答える