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;
}
} 任何人都可以告诉我什么是差异?我做错了什么?
答案 0 :(得分:1)
您提交到自定义适配器的数据集与您在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);
}
答案 1 :(得分:1)
问题是您没有修改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();
}
答案 2 :(得分:0)
好的,我管理它的唯一方法就是这样
我曾在onCreate这个
AlertDialog dirsDialog = dialogBuilder.create();
所以在UpdateDirectory()里面我把这个
ListView list = dirsDialog.getListView();
m_listAdapter = createListAdapter(m_subdirs);
list.setAdapter(m_listAdapter);
我认为这不是最好的方法,但这是我管理它的唯一方法。