【问题标题】:Android ListAdapter notifyDataSetChanged doesn't work when I add my custom adapter添加自定义适配器时,Android ListAdapter notifyDataSetChanged 不起作用
【发布时间】:2014-02-21 14:06:43
【问题描述】:

onCreate 我用这个

AlertDialog.Builder dialogBuilder = createDirectoryChooserDialog(title, m_subdirs, new DirectoryOnClickListener());

这是创建目录

private AlertDialog.Builder createDirectoryChooserDialog(String title, List<String> listItems, DialogInterface.OnClickListener onClickListener) {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(m_context);

    // Create custom view for AlertDialog title containing
    // current directory TextView and possible 'New folder' button.
    // Current directory TextView allows long directory path to be wrapped to multiple lines.
    LinearLayout titleLayout = new LinearLayout(m_context);
    titleLayout.setOrientation(LinearLayout.VERTICAL);

    m_titleView = new TextView(m_context);
    m_titleView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    m_titleView.setTextAppearance(m_context, android.R.style.TextAppearance_Large);
    // m_titleView.setTextColor(m_context.getResources().getColor(android.R.color.white));
    m_titleView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
    m_titleView.setText(title);

    titleLayout.addView(m_titleView);

    dialogBuilder.setCustomTitle(titleLayout);

    m_listAdapter = createListAdapter(listItems);

    dialogBuilder.setSingleChoiceItems(m_listAdapter, -1, onClickListener);
    dialogBuilder.setCancelable(false);

    return dialogBuilder;
}

当用户点击一个文件夹时,我使用这个

 private void updateDirectory() {

    m_subdirs.clear();
    m_subdirs.addAll(getDirectories(m_dir));
    m_titleView.setText(m_dir);

    m_listAdapter.notifyDataSetChanged();
}

当创建列表适配器是这样的时候它不起作用

  private MenuListAdapter createListAdapter(List<String> items) {
    int[] icons = new int[items.size()];
    String[] folders = new String[items.size()];
    for (int i = 0; i < items.size(); i++) {
        icons[i] = R.drawable.ic_folder;
        folders[i] = items.get(i);
    }
    return new MenuListAdapter(m_context, folders, icons);
}

但是当我这样使用它时它工作正常,我只是没有文件夹图像

        private MenuListAdapter createListAdapter(List<String> items) {

    return new ArrayAdapter<String>(m_context, android.R.layout.select_dialog_item, android.R.id.text1, items) {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);

            if (v instanceof TextView) {
                // Enable list item (directory) text wrapping
                TextView tv = (TextView) v;
                tv.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
                tv.setEllipsize(null);
            }
            return v;
        }
    };

这是我的菜单适配器

 public class MenuListAdapter extends BaseAdapter {

// Declare Variables
Context context;
String[] mTitle;
int[] mIcon;
LayoutInflater inflater;

public MenuListAdapter(Context context, String[] title, int[] icon) {
    this.context = context;
    this.mTitle = title;
    this.mIcon = icon;
}

@Override
public int getCount() {
    return mTitle.length;
}

@Override
public Object getItem(int position) {
    return mTitle[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Declare Variables
    TextView txtTitle;
    ImageView imgIcon;

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.drawer_list_item, parent, false);

    // Locate the TextViews in drawer_list_item.xml
    txtTitle = (TextView) itemView.findViewById(R.id.title);

    // Locate the ImageView in drawer_list_item.xml
    imgIcon = (ImageView) itemView.findViewById(R.id.icon);

    // Set the results into TextViews
    txtTitle.setText(mTitle[position]);

    // Set the results into ImageView
    imgIcon.setImageResource(mIcon[position]);

    return itemView;
}

} 谁能告诉我有什么区别?我做错了什么?

【问题讨论】:

    标签: android android-listview listadapter


    【解决方案1】:

    问题在于您没有修改存储在 MenuAdapter 中的数据。要解决此问题,您可以在适配器中创建一个 update 方法,并在用户单击时调用它:

    private void updateDirectory() {
        m_titleView.setText(m_dir);
        m_listAdapter.updateList(getDirectories(m_dir));
    

    }

    在你的 MenuListAdapter 中,添加到以下方法

    public void updateList(List<String> newItems){
        mIcons = new int[newItems.size()];
        mTitle = new String[newItems.size()];
        for (int i = 0; i < newItems.size(); i++) {
            mIcons[i] = R.drawable.ic_folder;
            mTitle[i] = items.get(i);
        }
        notifyDataSetChanged();
    }
    

    【讨论】:

    • 如果我尝试这样做,但在更新列表中没有新的 int 和新字符串,它只会用新的元素更新数组的第一个元素。如果我尝试使用新的,它根本不起作用。我认为它只是失去了对 mIcons 和 mTitle 的引用。我也尝试使用 ArrayList 而不是 new 来尝试清除它并添加新元素,但它仍然不起作用:/
    【解决方案2】:

    您提交给自定义适配器的数据集与您在updateDirectory() 中修改的数据集不同。您编写它的方式,您可以做的最简单的事情就是这样修改更新目录:

    private void updateDirectory() {
       m_subdirs.clear();
       m_subdirs.addAll(getDirectories(m_dir));
       m_titleView.setText(m_dir);
       dialogBuilder.setSingleChoiceItems(createListAdapter(m_subdirs), -1, onClickListener);
    
    }
    

    【讨论】:

    • 它们在我看来并不相同。仔细看看
    • 但是当 m_listAdapter 在 dialogbuilder 中时,我该如何设置呢?
    • 将对话框构建器保留为成员类并使用新适配器调用 setSingleChoiceItem。
    【解决方案3】:

    好吧,我管理它的唯一方法就是这样

    我在 onCreate 这个

    AlertDialog dirsDialog = dialogBuilder.create();
    

    所以在 UpdateDirectory() 里面我放了这个

    ListView list = dirsDialog.getListView();
    m_listAdapter = createListAdapter(m_subdirs);
    list.setAdapter(m_listAdapter);
    

    我不认为这是最好的方法,但它是我管理它工作的唯一方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      相关资源
      最近更新 更多