Alex Fragotsis
Alex Fragotsis

Reputation: 1268

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

on onCreate i use this

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

and this is the 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;
}

when the user clicks on a folder i use this

 private void updateDirectory() {

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

    m_listAdapter.notifyDataSetChanged();
}

when the create list adapter is like this it doesnt work

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

but when i use it like this it works fine, I just dont have the folder image

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

and this is my 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;
}

} can anyone tell me whats the difference? what am i doing wrong?

Upvotes: 0

Views: 2285

Answers (3)

Alex Fragotsis
Alex Fragotsis

Reputation: 1268

Ok so the only way I managed it to work is like this

I had in onCreate this

AlertDialog dirsDialog = dialogBuilder.create();

so inside UpdateDirectory() I put this

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

I don't think that thats the best way to do this but its the only way I managed it to work.

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157447

The data set you submitted to your custom adapter is different from the one you are modifying inside updateDirectory(). The way you wrote it, the simplest thing that you can do is to modify update directory this way:

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

}

Upvotes: 1

ben75
ben75

Reputation: 28706

The problem is that you are not modifying the data stored in your MenuAdapter. To fix the problem you can create an update method in your adapter and call it when the user click :

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

}

And in you MenuListAdapter, add to following method

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

Upvotes: 1

Related Questions