首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用BaseAdapter?

如何使用BaseAdapter?
EN

Stack Overflow用户
提问于 2013-12-26 14:09:07
回答 5查看 1.6K关注 0票数 0

我正在使用Adapter来查看添加到会话中的文件列表,但是当我想使用带有新文件列表的newSession (重新启动活动)时,Adapter会查看所有最近的项目,所以我想在打印列表之前释放适配器。我的适配器是由下面的代码和使用它的片段提供的。我尝试了所有的建议,但我有一些问题。如下面两个图片所示,在第一个文件中,我使用了第一个文件,然后用一个新文件重新启动了我的活动,但是我有第一个和第二个文件。那么如何清除我的适配器。

代码语言:javascript
复制
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的片段:

代码语言:javascript
复制
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");
                }

            }

        }
    }



}

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-12-27 12:59:41

您考虑过可以在SessionChoose.listOfDoc中同时拥有这两个文件吗?

显然,您清除BaseAdapter的代码并没有错。

我使用它在适配器中创建一个我称之为addAll()的方法,如下所示:

代码语言:javascript
复制
public void addAll(List<String> list) {
 //mList is the List of the adapter;
 mList.clear();
 mList.addAll(list);
 notifyDatasetChanged();
}

这样,当我想清除适配器时,只需调用mAdapter.addAll(new ArrayList<String>);

票数 1
EN

Stack Overflow用户

发布于 2013-12-26 14:18:11

有三种方式:

1)调用notifyDataSetChanged()

2)将适配器设置为空

3)在添加到会话中时清除ArrayList。

票数 1
EN

Stack Overflow用户

发布于 2013-12-26 14:26:07

如果只想从适配器中删除所有条目,则必须同时清除arrayLists listCountrylistFlag,然后通知适配器。

代码语言:javascript
复制
listCountry.clear();
listFlag.clear();
mAdapter.notifyDatasetChanged();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20786257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档