2015-01-06 19 views
0

我已经创建了一个ListView,它由一个目录中的文件列表组成。 每行都有一个按钮,用户可以通过该按钮删除文件。 删除文件后,我想更新列表视图,使行不再可用。 我试图使用notifyDataSetChanged(),但它没有更新列表。Android:notifyDataSetChanged()在删除元素后不更新ListView

public class CustomList extends ArrayAdapter<String> { 
    private final Activity context; 
    private final String[] web; 
    private final Integer[] imageId; 
    private final String[] datetime; 
    public CustomList(Activity context, 
         String[] web, Integer[] imageId,String[] datetime) { 
     super(context, R.layout.list_single, web); 
     this.context = context; 
     this.web = web; 
     this.imageId = imageId; 
     this.datetime=datetime; 
    } 
    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     final View rowView= inflater.inflate(R.layout.list_single, null, true); 
     final TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); 
     ImageView imageView = (ImageView) rowView.findViewById(R.id.img); 
     TextView txtTitle2 = (TextView) rowView.findViewById(R.id.txt1); 
     txtTitle.setText(web[position]); 
     imageView.setImageResource(R.drawable.ic_launcher); 
     txtTitle2.setText(datetime[position]); 

     final Button button=(Button)rowView.findViewById(R.id.button1); 
     button.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       String text=txtTitle.getText().toString(); 
       String text1= Environment.getExternalStorageDirectory().getAbsolutePath().toString() +"/Notate/"+text+".pcm"; 
       Toast.makeText(getContext(), text1, Toast.LENGTH_LONG).show(); 
       File filetodelete=new File(text1); 
       filetodelete.delete(); 
       button.setText("Deleted"); 
       button.setEnabled(false); 
       adapter.notifyDataSetChanged(); 


      } 
     }); 
     return rowView; 
    } 
} 

ListView list; 
public FragmentB() { 
    // Required empty public constructor 
} 
CustomList adapter=null; 

String[] days; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    String path = Environment.getExternalStorageDirectory().toString()+"/Notate"; 
    File f = new File(path); 
    File file[] = f.listFiles(); 
    days=new String[file.length]; 
    Integer[] imageId=new Integer[file.length]; 
    String[] dateTime=new String[file.length]; 

    for (int i=0; i < file.length; i++) 
    { 
     String temp=file[i].getName(); 
     String temp2=temp.substring(0,temp.length()-4); 
     Date lastModDate = new Date(file[i].lastModified()); 

     days[i]=temp2; 
     imageId[i]=R.drawable.ic_launcher; 
     dateTime[i]=lastModDate.toString().substring(0,lastModDate.toString().length()-14); 
    } 

    // Inflate the layout for this fragment 
    final View contextView = inflater.inflate(R.layout.fragment_fragment_c,container,false); 
    adapter = new 
      CustomList(this.getActivity(), days, imageId,dateTime); 
    list=(ListView) contextView.findViewById(R.id.listView); 
    list.setAdapter(adapter); 
list.setOnItemClickListener(this); 
    return contextView; 
} 
+0

您删除了该文件,但未从适配器中删除其条目。 – Rohit5k2

回答

2

问题是您的ArrayAdapter支持String[] web所以即使您删除File,web保持不变。将web转换为List<String>,并在删除文件时在notifyDataSetChanged()之前调用web.remove(position)

+0

谢谢!它非常完美! – shivram

+0

@ user3460885如果你想以正常的方式做 - 检查我的答案。 – Divers

1

实现利用getCount()方法

@Override 
    public int getCount() { 
return objects.size(); 

    } 
1

你正在做它在一个错误的方式。不要在Adapter中传递所有这些数组。像这样创建类别(POJO):

public class MyClass { 
private String web; 
private Integer imageId; 
..... 
} 

创建这些对象的列表,例如, List<MyClass> myClasses = new ArrayList<>(),并填写它将适当的数据。

然后这个列表传递给您的适配器,并使用这些数据在getView方法:

public class CustomList extends ArrayAdapter<MyClass> { 
    public CustomList(Activity context, List<MyClass> myClasses) { 
     super(context, R.layout.list_single, myClasses; 
    } 

    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     MyClass myClass = getItem(position); //Take a look here 
     (TextView) rowView.findViewById(R.id.txt).setText(myClass.getWeb()) 
     ... 
     ... 
    } 
} 

就是这样。

0

您刚刚删除了该文件。您还需要从阵列中删除条目。