我确实尝试了“adapter.clear”、“adapter.invalidate”、adapter.notifyDataSetChanged”等方法和其他建议 - 没有任何效果。基本上我有一个调用片段活动,上面有微调器,其中一些文件的路径已解决 - 当微调器中的元素被选中, 显示带有列表视图的片段. 问题是, 当在下一个迭代数组中填充的项目比以前少时, 列表项会显示这个新元素, 但也显示来自旧数组的元素. t痛苦....有人对此有解决方案吗?
活动课
public class ListBitmapsFragmentActivity extends FragmentActivity implements
ListBitmapsFragment.Callbacks {
// variable spinner to show values for the paths to the images
Spinner s1;
// variable of ArrayAdapter class to hold the values of the possible picture file paths for the spinner
ArrayAdapter<String> adapter;
// variable of the ArrayList class to store the values of possible picture paths
ArrayList<String> values = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the content
setContentView(R.layout.list_bitmap_fragment_activity);
// setting adapter for the spinner widget
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, values);
s1 = (Spinner) findViewById(R.id.spinner1);
// set some possible paths to the images in the adapter for the spinner
adapter.add("SELECT THE PATH TO THE PICTURES:");
adapter.add("storage/extSdCard/DCIM/Camera/");
adapter.add(Environment.getExternalStoragePublicDirectory("DCIM/")
.getAbsolutePath());
adapter.add(Environment.DIRECTORY_PICTURES);
s1.setAdapter(adapter);
// set listener for the spinner
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int id, long position) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(22);
String pathValue = s1.getSelectedItem().toString();
if (!(pathValue == "SELECT THE PATH TO THE PICTURES:")) {
callTheFragment(pathValue);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
@Override
// overriden method from the inner interface within the fragment class
public void onBitmapSelected(String imagePath) {
// communicate the result of the clicked row in the fragment class to the calling activity
Intent i = new Intent();
// use the putExtra() method to return some value to the calling activity---
i.putExtra("path", imagePath);
setResult(RESULT_OK, i);
finish();
}
public void callTheFragment(String pathValue) {
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.item_list);
Bundle args = new Bundle();
args.putString("pathValue", pathValue);
// if fragment is not present on it's container
if (fragment == null) {
FragmentTransaction ft = fm.beginTransaction();
fragment = new ListBitmapsFragment();
// set the object of arguments to the fragment
fragment.setArguments(args);
ft.remove(fragment);
ft.add(R.id.item_list, fragment);
// add fragment to the stack in case of using back button to recall the fragment
// ft.addToBackStack(null);
ft.commit();
}
}
}
扩展 ListFragment 的片段类
public class ListBitmapsFragment extends ListFragment {
private Callbacks mCallbacks;
public interface Callbacks {
public void onBitmapSelected(String path);
}
private static Callbacks sDummyCallbacks = new Callbacks() {
@Override
public void onBitmapSelected(String imagePath) {
}
};
public ListBitmapsFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new CustomAdapter(getActivity(), pathArray()));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException(
"Klicoca aktivnost mora implementirati callback!!");
}
mCallbacks = (Callbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = sDummyCallbacks;
}
@Override
public void onListItemClick(ListView listView, View view, int position,
long id) {
super.onListItemClick(listView, view, position, id);
View row = view;
TextView tv = (TextView) row.findViewById(R.id.imagePath);
mCallbacks.onBitmapSelected(tv.getText().toString());
}
private List<String> pathArray() {
String rootPath = getArguments().getString("pathValue");
final List<String> directoryEntries = new ArrayList<String>();
File directory = new File(rootPath);
if (directory.isDirectory()) {
File[] files = directory.listFiles();
Arrays.sort(files, new Comparator<File>() {
public int compare(File f1, File f2) {
return -Long.valueOf(f1.lastModified()).compareTo(
f2.lastModified());
}
});
directoryEntries.clear();
for (File file : files) {
directoryEntries.add(file.getPath());
}
}
if (directoryEntries.isEmpty()) {
directoryEntries.add("storage/extSdCard/DCIM/Camera/no_photo.jpg");
}
return directoryEntries;
}
class CustomAdapter extends ArrayAdapter<String> {
private final Activity context;
private final List<String> paths;
public CustomAdapter(Activity context, List<String> paths) {
super(context, R.layout.list_bitmap_paths, paths);
this.context = context;
this.paths = paths;
}
class ViewContainer {
public ImageView imageView;
public TextView txtPath;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewContainer viewContainer;
View rowView = view;
Bitmap unscaledBitmap;
Bitmap scaledBitmap;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_bitmap_paths, null,
true);
viewContainer = new ViewContainer();
viewContainer.txtPath = (TextView) rowView
.findViewById(R.id.imagePath);
viewContainer.imageView = (ImageView) rowView
.findViewById(R.id.icon);
rowView.setTag(viewContainer);
} else {
viewContainer = (ViewContainer) rowView.getTag();
}
viewContainer.txtPath.setText(this.paths.get(position));
viewContainer.imageView.setImageBitmap(null);
unscaledBitmap = ScalingUtilities.decodeFile(
this.paths.get(position), 100, 100, ScalingLogic.FIT);
scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap,
100, 100, ScalingLogic.FIT);
viewContainer.imageView.setImageBitmap(scaledBitmap);
if (position % 2 == 0) {
rowView.setBackgroundColor(Color.rgb(238, 233, 233));
} else {
rowView.setBackgroundColor(Color.rgb(255, 255, 255));
}
return rowView;
}
}
}