【问题标题】:How to use BaseAdapter?如何使用 BaseAdapter?
【发布时间】:2013-12-27 20:59:41
【问题描述】:

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

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

            }

        }
    }



}

【问题讨论】:

  • 只需清除两个arrayLists,然后使用notifyDataSetChanged();
  • @Piyush Gupta 怎么做
  • @Rohan Kandwal 我试过类似你的提议。但我有空指针。
  • 当我打开新会话时你是什么意思?
  • 当我更改两个 Arraylist 时,项目列表会发生变化。当我放置文件的 url 时,我有很多会话(在文件中)我更改了要查看的项目列表,所以我有了新列表。

标签: android baseadapter


【解决方案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&lt;String&gt;);

【讨论】:

    【解决方案2】:

    有三种方式:

    1) 调用 notifyDataSetChanged()

    2) 将您的适配器设置为 null

    3) 在添加到 Session 时清除 ArrayList。

    【讨论】:

      【解决方案3】:

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

      listCountry.clear();
      listFlag.clear();
      mAdapter.notifyDatasetChanged();
      

      【讨论】:

        【解决方案4】:
         gridView.setAdapter(null);
         gridView.setAdapter(mAdapter);
        

        您应该在尝试将适配器设置为上述 mAdapter 之前设置 setAdapter(null)。

        【讨论】:

        • 我试过这个,但我有一些问题。当我重新打开某个会话时,我将项目加倍。
        【解决方案5】:

        您需要做的就是将您的适配器设置为空

        【讨论】:

        • gridView.setAdapter(null);
        猜你喜欢
        • 2012-07-03
        • 1970-01-01
        • 2012-09-22
        • 2016-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-09
        • 1970-01-01
        相关资源
        最近更新 更多