3

この投稿で問題を発見しました Updateing ExpandableListView with notifyDataSetChanged()

「setNotifyDatasetChanged を使用してビューを更新するたびに、アダプターはリストのループを呼び出します。アダプターのリストは、行った変更により null 値を取得します。」

私は初心者で、リストを適切に変更できません

私の情報源

public class UrlArrayAdapter extends ArrayAdapter<UrlItem> {

    private LayoutInflater inflater;
    private ListView urlListView1;
    private ArrayList<UrlItem> urlItems1;

    public UrlArrayAdapter(Context context, ListView urlListView,
            ArrayList<UrlItem> urlLists) {
        super(context, urlListView.getId(), urlLists);
        this.urlItems1 = urlLists;
        this.urlListView1 = urlListView;


        inflater = LayoutInflater.from(context);

ベース アダプタの urlLists のリストからアイテムを削除するにはどうすればよいですか??

4

2 に答える 2

0

アイテムを削除するには:urlList.remove(item_index); 次に、データ セットが変更されたことを通知します。

以下のコメントに基づいて、回答を更新しています。
このremove()メソッドは、要素自体ではなく、削除される要素のインデックスを受け入れます。

したがって、リストから特定の番号を削除するには、まずそのインデックスを見つけてから、そのインデックスをremove()メソッドに渡す必要があります。

元:

public void deleteItem(int numberToDelete) {
    int index=-1;

    // Find the index of numberToDelete
    if(urlLists.contains(numberToDelete)){
        index = urlLists.indexOf(numberToDelete);
    }

    // Delete the number by spefying the index found.
    if(index!=-1){
        urlLists.remove(index);
    }

//...
}
于 2012-08-16T15:25:02.287 に答える
0

のオーバーライドに加えてpublic View getView(int position, View view, ViewGroup parent)、拡張するクラスがArrayAdapterこれらのメソッドをオーバーライドしていることを確認してください。

public int getCount()
public UrlItem getItem(int position)
public int getPosition(Hold item)
public long getItemId(int position)

アイテムの数を判断するためにアダプターを呼び出すと思いますnotifyDataSetChanged()getCount()このメソッドをオーバーライドしないと、カスタム アダプターがそのサイズと内容をクライアントに伝える方法がないため、問題が差し迫っているように見えますreturn urlItems1.size();IndexOutOfBoundException

于 2012-08-20T20:20:06.687 に答える