如何使用BaseAdapter? [英] How to use BaseAdapter?

查看:120
本文介绍了如何使用BaseAdapter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的适配器来查看添加到会话,但问题的文件列表,当我想用​​一个newsession的(重启活动)用文件的新名单,我的适配器视图中的所有最近的项目,因此,我想我的自由适配器之前打印的清单。我的适配器是由code以下以及在何处使用它的片段给出。我尝试了所有的给定的命题,但我有一些问题。如在我使用的第一文件,则第一个如下所示的两个图像我重新启动我用新文件的活动,但是我有第一和第二文件。因此,如何清除我的适配器。

I'm using an Adapter to view a list of files added to a session but the problem when I would like to use a newSession (restarts Activity) with a new list of files, my Adapter view all the recent items, So I Would like to free my adapter before to print the list. My adapter is given by the code below and the fragment where to use it. I tried All the given proposition but I have the some problem. As illustrated by the two images below in the first one I used a first file then I restarted my Activity with a new file but I have both the first and the second file. So how to clear my Adapter.

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片段:

the fragment where i'm using 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");
                }

            }

        }
    }



}

推荐答案

你有没有考虑,你可以在你的 SessionChoose.listOfDoc有两个文件

显然你的code清除BaseAdapter是没有错的。

我用来创建我称之为一种方法的addAll()在我的适配器,如:

Have you considered that you could have both files in your SessionChoose.listOfDoc ?
Apparently your code to clear the BaseAdapter is not wrong.
I use to create a method which I call addAll() in my Adapter, like this:

public void addAll(List<String> list) {
 //mList is the List of the adapter;
 mList.clear();
 mList.addAll(list);
 notifyDatasetChanged();
}

这样,当我想清楚适配器,我只是叫 mAdapter.addAll(新的ArrayList&LT;串GT;);

这篇关于如何使用BaseAdapter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆