0

私は実際に、データベースから項目を入力して ListView を作成しています。
リストビューの最後に到達したら、さらにいくつかのアイテムを入力します。ここで、データベースから返された値に基づいて ListView の仕切りが必要です。データベースの 2 つの連続する値が同じ場合は、それらを細い線で区切り、太い線でない場合は区切ります。

このようにアダプターを介して設定してみました

if (convertView == null) {

            holder = new ViewHolder();

            if (eventSource.get(position).equalsIgnoreCase("asdsadas")
                    && eventSource.get(position + 1).equalsIgnoreCase(
                            "fdgdfgfd")
                    || eventSource.get(position).equalsIgnoreCase(
                            "dfgdfgdfg")
                    && eventSource.get(position + 1).equalsIgnoreCase(
                            "jgghjhhgg")) {
                convertView = mInflater.inflate(R.layout.list_adapter, null);
            } else {
                convertView = mInflater.inflate(R.layout.list_adapterthinline,
                        null);
            }

状況に応じて新しいレイアウトを膨らませています。初めて機能しますが、下にスクロールして表示が変わると表示が変わります。それはすべて混同されます。

このように、アクティビティでも仕切りの高さを設定してみました。onCreate と onScroll リスナーでも「setdivider」メソッドを呼び出します。

public void setdivider() {
        // TODO Auto-generated method stub
        for (int i = 0; i < listSource.size() - 1; i++) {


            if (!listSource.get(i).equalsIgnoreCase(
                    listSource.get(i + 1))) {
                Log.v("inside not equals", "become smalllllllllllllllll");
                list.setDivider(red);
                list.setDividerHeight(5);
            } else if (listSource.get(i).equalsIgnoreCase(
                    listSource.get(i + 1))) {
                Log.v("inside equals", "become bigggggggggggg");
                list.setDivider(blue);
                list.setDividerHeight(10);
            }
        }
    }

ただし、ここでは両方のログ コメントが LogCat に表示されますが、リストには 1 つの仕切りしか設定されていません。
どこが間違っているのか教えてください。もしあれば、他のアプローチを提案してください。

4

2 に答える 2

1

ListView caches views, so when you scroll around, view are reused. This is why convertView isn't always null. However, since you have two different kinds of views, you need to tell that to the ListView so that the convertView you get back is the kind you want. You do that by implementing Adapter.getItemViewType() and Adapter.getViewTypeCount().

In your example, you would let getViewTypeCount return 2, and let getItemViewType return 1 if it's a divider, and 0 if it's not.

于 2012-05-03T06:52:26.570 に答える
0

レイアウト R.layout.list_adapter と R.layout.list_adapterthinline を 1 つのレイアウトにマージします。

デフォルトで細い線を設定します。必要なときに表示するように設定します

.....ここにあなたのレイアウト。

回線が必要なとき。

convertView.findViewById(R.id.thin_line).setVisible(View.Visible)

いらない時。

convertView.findViewById(R.id.thin_line).setVisible(View.Gone)

于 2012-05-03T05:19:01.197 に答える