2013-09-24 54 views
0

我曾尝试方法为“adapter.clear”,“adapter.invalidate”,adapter.notifyDataSetChanged“和其他建议 - 没什么作用的,基本上我有一个调用片段活动,其上有spiner,在那里解决了一些文件的路径 - 当spinner中的元素被选中时,呈现带有列表视图的片段。问题是,当在下一个迭代数组填充了较少的项目,因为它是以前,列表项目目前这种新的元素,但那些从旧的阵列也。 T在痛苦....任何人有这个解决方案?如何清除ListView中的数据(在ListFragment扩展),这是由自定义阵列适配器填充

活动类

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

}

回答

0

可以removeAllViews()从实际的ListView,而不仅仅是清除适配器...尝试片段CLAS

((ListBitmapsFragment)fragment).getListView().removeAllViews() 
+0

嗨ATOMIX,我会试着将张贴反馈。 – user2774394

+0

此方法不受支持,并在调用时引发UnsupportedOperationException。 –

0

感谢Atomix,我找到了解决这个问题的方法 - 在callTheFragment()方法中我做了一些改变:我使用了关于容器中片段的信息,并且在它之后我删除了它,如果它已经exixsted和一起,我找到ViewGroup(容器)的片段,并清除所有的意见。它的工作原理。 谢谢, 埃里克D.

public void callTheFragment(String pathValue) { 
    // set the object for the fragment manager 
    FragmentManager fm = getSupportFragmentManager(); 
    // set object of the fragment class - container for the fragment 
    Fragment fragment = fm.findFragmentById(R.id.item_list); 
    // if there is a fragment allready in the container 
    if (!(fragment == null)) { 
     // remove the existing fragment 
     fm.beginTransaction().remove(fragment).commit(); 
     // get reference to the parrent container 
     ViewGroup vg = (ViewGroup) findViewById(R.id.item_list); 
     // delete all views from the parrent container 
     vg.removeAllViews(); 
    } 
    // new fragment 
    // object of variables for pass them to the fragment 
    Bundle args = new Bundle(); 
    args.putString("pathValue", pathValue); 
    // FragmentTransaction ft = fm.beginTransaction(); 
    fragment = new ListBitmapsFragment(); 
    // set the object of arguments to the new fragment 
    fragment.setArguments(args); 
    // execute transaction 
    fm.beginTransaction().add(R.id.item_list, fragment).commit(); 
} 
相关问题