我正在使用适配器来查看添加到会话的文件列表,但是当我想使用newSession(重新启动Activity)和新的文件列表时,问题是我的适配器查看所有最近的项目,所以我想在打印列表之前释放我的适配器。我的适配器由下面的代码和片段在哪里使用它给出。我尝试了所有给定的命题,但我遇到了一些问题。如下面第一个中的两个图像所示,我使用了第一个文件,然后我用一个新文件重新启动了我的活动,但我同时拥有第一个和第二个文件。那么如何清除我的适配器。
public class GridviewAdapter extends BaseAdapter
{
private ArrayList<String> listCountry;
private ArrayList<Integer> listFlag;
private Activity activity;
public GridviewAdapter(Activity activity, ArrayList<String> listCountry, ArrayList<Integer> listFlag) {
super();
this.listCountry = listCountry;
this.listFlag = listFlag;
this.activity = activity; }
@Override
public int getCount() {
// TODO Auto-generated method stub
return listCountry.size();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listCountry.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public ImageView imgViewFlag;
public TextView txtViewTitle;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridview_row, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewTitle.setText(listCountry.get(position));
view.imgViewFlag.setImageResource(listFlag.get(position));
return convertView;
}
}
我正在使用myAdapter的片段:
public class MatFragment extends Fragment {
private GridViewAjoutFile adapter;
private GridViewAjoutFile ajout = new GridViewAjoutFile();
public static boolean continuer = false;
private GridviewAdapter mAdapter;
private ArrayList<String> listCountry;
private ArrayList<Integer> listFlag;
private ArrayList<String> intentType;
private GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!(mAdapter.isEmpty())){
listCountry.clear();
listFlag.clear();
mAdapter.notifyDataSetChanged();
}
prepareList();
// prepared arraylist and passed it to the Adapter class
mAdapter = new GridviewAdapter(getActivity(),listCountry, listFlag);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ArrayList<String> profilTabShortCut;
View view = inflater.inflate(R.layout.maindoc,
container, false);
// Set custom adapter to gridview
mAdapter.notifyDataSetChanged();
gridView = (GridView) view.findViewById(R.id.gridView1);
gridView.setAdapter(mAdapter);
// Implement On Item click listener
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Intent intent1 = new Intent();
intent1.setAction(android.content.Intent.ACTION_VIEW);
intent1.setType("application/pdf") ;
intent1.setData(Uri.parse(url));
startActivity(Intent.createChooser(intent1,
"Ouvrir le fichier avec")); */
SetIntentType(SessionChoose.listofUrl.get(position),intentType.get(position));
}
});
return view;
}
public Intent SetIntentType(String url, String intenttype ){
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType(intenttype) ;
intent.setData(Uri.parse(url));
startActivity(Intent.createChooser(intent,
"Ouvrir le fichier avec"));
return null;
}
public void prepareList()
{
listCountry = new ArrayList<String>();
listFlag = new ArrayList<Integer>();
intentType = new ArrayList<String>();
if(SessionChoose.listofDoc.size()>0){
for(int i = 0; i< SessionChoose.listofDoc.size() ; i++){
listCountry.add(SessionChoose.listofDoc.get(i));
if((SessionChoose.listoftype).get(i).contains("pdf")){
listFlag.add(R.drawable.pdf);
intentType.add("application/pdf");
} else if(((SessionChoose.listoftype).get(i).contains("png"))||((SessionChoose.listoftype).get(i).contains("bitmap")) || ((SessionChoose.listoftype).get(i).contains("jpg")) || ((SessionChoose.listoftype).get(i).contains("jpeg"))){
Log.d("*/*/*/*/DoCFragment*/*/*/*", SessionChoose.listoftype.get(i));
listFlag.add(R.drawable.png);
intentType.add("application/pdf");
} else if((SessionChoose.listoftype).get(i).contains("txt")){
listFlag.add(R.drawable.txt);
intentType.add("application/pdf");
} else if ((SessionChoose.listoftype).get(i).contains("mp4")){
listFlag.add(R.drawable.video);
intentType.add("application/pdf");
} else{
listFlag.add(R.drawable.inconnu);
intentType.add("application/pdf");
}
}
}
}
}
答案 0 :(得分:1)
有三种方式:
1)致电 notifyDataSetChanged()
2)将适配器设置为空
3)在添加到Session时清除ArrayList。
答案 1 :(得分:1)
如果您只想删除适配器中的所有条目,则必须同时清除arrayLists listCountry
和listFlag
,然后通知适配器。
listCountry.clear();
listFlag.clear();
mAdapter.notifyDatasetChanged();
答案 2 :(得分:1)
gridView.setAdapter(null);
gridView.setAdapter(mAdapter);
在尝试将适配器设置为mAdapter之前,应该先设置setAdapter(null)。
答案 3 :(得分:1)
您是否认为SessionChoose.listOfDoc
中有两个文件?
显然你清除BaseAdapter的代码没错。
我用来创建一个方法,我在我的适配器中调用addAll()
,如下所示:
public void addAll(List<String> list) {
//mList is the List of the adapter;
mList.clear();
mList.addAll(list);
notifyDatasetChanged();
}
这样,当我想清除适配器时,我只需要调用mAdapter.addAll(new ArrayList<String>);
答案 4 :(得分:0)
你需要做的就是setyouradapter为null