簡體   English   中英

添加自定義適配器時,Android ListAdapter notifyDataSetChanged不起作用

[英]Android ListAdapter notifyDataSetChanged doesn't work when I add my custom adapter

on onCreate我用這個

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

這是createDirectory

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;
        }
    };

這是我的menulistadapter

 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;
}

任何人都可以告訴我什么是差異? 我究竟做錯了什么?

您提交給自定義適配器的數據集與您在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);

}

問題是您沒有修改MenuAdapter中存儲的數據。 要解決此問題,您可以在適配器中創建更新方法,並在用戶單擊時調用它:

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();
}

好吧,我管理它的唯一方法就是這樣

我曾經在onCreate這個

AlertDialog dirsDialog = dialogBuilder.create();

所以在UpdateDirectory()里面我把它放了

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

我認為這不是最好的方法,但這是我管理它的唯一方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM