3

2つのビュータイプListViewを拡張して持つカスタムアダプタを使用しています。BaseAdapterメソッドを実行するadapter.removeRow(position)と、アダプターのデータが正しく更新され、リストにこれが反映されますが、ビュータイプが正しく更新されません。アダプターはによって支えられています

ArrayList<Map<String, String>> rows = new ArrayList<Map<String, String>>();

そして私はサブセットを持っています

List<Integer> flashSet = new ArrayList<Integer>();

これは、(標準のビュータイプ0ではなく)ViewType1のすべての位置のリストです。

これが私のアダプタremoveRow(position)メソッドです:

    public void removeRow(int position) {
        if (getItemViewType(position) == TYPE_FLASH) {
            flashSet.remove(position);
        }
        for (int flashPosition:flashSet) {
            System.out.println(tag+"is "+flashPosition+" going to be moved?");
             if (flashPosition > position) {
                 flashPosition -= 1;
                 System.out.println(tag+"Yes! It's been moved to "+flashPosition);
             }
        }
        rows.remove(position);
        notifyDataSetChanged();
    }

これが私のgetView方法です:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FlashHolder flashHolder;
        ClipHolder clipHolder;
        int type = getItemViewType(position);
        if (convertView == null) {
            if (type == TYPE_CLIP) {
                convertView = rowInflater.inflate(R.layout.clip_note_row_layout, null);
                clipHolder = new ClipHolder();
                flashHolder = null;
                clipHolder.textView = (TextView)(convertView.findViewById(R.id.clip_text));
                convertView.setTag(clipHolder);
            } else {
                convertView = rowInflater.inflate(R.layout.flash_row_layout, null);
                clipHolder = null;
                flashHolder = new FlashHolder();
                flashHolder.front = (TextView)(convertView.findViewById(R.id.flash_text));
                flashHolder.back = (TextView)(convertView.findViewById(R.id.clip_text));
                convertView.setTag(flashHolder);
            }
        } else {
            if (type == TYPE_CLIP) {
                clipHolder = (ClipHolder)convertView.getTag();
                flashHolder = null;
            } else {
                clipHolder = null;
                flashHolder = (FlashHolder)convertView.getTag();
            }
        }
        if (type == TYPE_CLIP) {
            clipHolder.textView.setText(rows.get(position).get("clip"));
        } else {
            flashHolder.front.setText(rows.get(position).get("flash_text"));
            flashHolder.back.setText(rows.get(position).get("clip"));
        }
        return convertView;
    }

私は新しいものを作成し、adapterそれを更新ArrayListして呼び出すことができることを知っていますlistView.setAdapter(adapter)が、潜在的に長いリストから1つのアイテムを削除しようとしているだけでは、これは完全にやり過ぎのようです。削除前と削除後の写真を参照してください。 前

次に、最初のアイテムを削除します。「Let'swatchit」アイテムの後ろに「which」という単語が隠されていましたが、「inspiredby…」アイテムは空白のアイテム3の後ろに隠されています。

後

したがって、データは更新されますが、ビュータイプは更新されません。助けてくれてありがとう!

4

3 に答える 3

1

私はそれを考え出した。他の人が同じ間違いを犯すとは思わないので、これは誰にとっても役に立ちません。

こうすることで素朴に思った

    for (int flashPosition:flashSet) {
        System.out.println(tag+"is "+flashPosition+" going to be moved?");
         if (flashPosition > position) {
             flashPosition -= 1;
             System.out.println(tag+"Yes! It's been moved to "+flashPosition);
         }
    }

に格納されている実際の値を変更していましたList<Integer> flashSet = new ArrayList<Integer>();

実際、代わりに次のことを行う必要があります。

for (int flashPosition:flashSet) {
    System.out.println(tag+"is "+flashPosition+" going to be moved?");
     if (flashPosition > position) {
         flashSet.remove((Object)flashPosition);
         flashPosition -= 1;
         flashSet.add(flashPosition);
         System.out.println(tag+"Yes! It's been moved to "+flashPosition);
     }
}
于 2013-03-13T11:03:24.697 に答える
0

これを試してください。アイテムを削除または追加した後、アダプタの更新を呼び出す必要があります。

Youradapter.notifyDataSetChanged();
于 2013-03-13T10:34:26.160 に答える
0

使用するyourlistview.invalidateViews()

それ以外の

notifyDataSetChanged();

わたしにはできる。

于 2013-03-13T10:35:07.857 に答える