如何清除以前的Gridview项目并在android中重新填充新的适配器?

时间:2013-01-28 11:23:18

标签: android gridview

当我请求另一个适配器时如何清除gridview如果我在这里使用延迟适配器我把我的代码用于gridview活动和lazyadapter我使用asynctask for gridview items

这里我把我的代码用于gridview

  

Gridview Activity.java

public class VisitorActivity extends Activity implements OnClickListener{
{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new visitormiddlelistasyntask.execute(""); 
       }   

public class VisitorMiddleListAsyncTask extends AsyncTask<String,ArrayList<HashMap<String, String>>,ArrayList<HashMap<String, String>>>
    {
        String city;
        JsonParser jparser=new JsonParser();
        String visitorurl="http://digitalhoteladnetwork.com/vixxa_beta/index.php/visitors/webvisitorlist";
        //String visitorurl="http://digitalhoteladnetwork.com/vixxa_beta/index.php/visitors/websearch_visitor_guide";

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(String... params) 
        {
            //For Get city

            Geocoder geocoder = new Geocoder(VisitorActivity.this, Locale.getDefault());
            try
            {
                List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
                Log.e("Addresses","-->"+addresses);
                city = addresses.get(0).getSubAdminArea();
               // Log.e("Cityname","--->"+city);

            }
            catch (IOException e)
            {
                e.printStackTrace();
              //  Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }


                    WebServiceData wsd=new WebServiceData();

                    String visitorstring=wsd.VisitorGuide(city, visitorurl);

                    //Log.e("webservice visitorstring","-->"+wsd.VisitorGuide(city, visitorurl));

                Log.e("webservice visitorstring","-->"+visitorstring);



                    try
                    {
                        JSONObject jobject=new JSONObject(visitorstring);
                        JSONArray jvisitorlist=jobject.getJSONArray(TAG_VISITORGUIDELIST);

                        for(int i=0;i<jvisitorlist.length();i++)
                        {
                            HashMap<String, String> map=new HashMap<String, String>();

                            String id=jvisitorlist.getJSONObject(i).getString(TAG_VISITORGUIDEID).toString();
                            //Log.e("Id","-->"+id);

                            String title=jvisitorlist.getJSONObject(i).getString(TAG_VISITORGUIDETITLE).toString();
                            //Log.e("Title","-->"+title);

                            String image=jvisitorlist.getJSONObject(i).getString(TAG_VISITORGUIDEIMAGE).toString();
                            //Log.e("Image","-->"+image);

                            //String show=jvisitorlist.getJSONObject(i).getString(TAG_VISITORTITLESHOW).toString();

                            map.put(TAG_VISITORGUIDEID,id);
                            map.put(TAG_VISITORGUIDETITLE,title);
                            map.put(TAG_VISITORGUIDEIMAGE, image);
                            //map.put(TAG_VISITORTITLESHOW,show );

                            visitormiddle.add(map);

                        }

                        //Log.e("Visitor Guide List","-->"+jvisitorlist);

                    }
                    catch (Exception e)
                    {
                            e.printStackTrace();
                    }

                    return visitormiddle;

        }

        protected void onPostExecute(ArrayList<HashMap<String, String>> result)
        {

            gridview=(GridView)findViewById(R.id.gridview);
            adapter=new LazyAdapter(VisitorActivity.this, visitormiddle);
            gridview.setAdapter(adapter);

        }


    }


}
  

LazyAdapter.java

public class LazyAdapter extends BaseAdapter {

    private static final String TAG_VISITORGUIDELIST="visitorlist";
    private static final String TAG_VISITORGUIDEID="visitor_guide_id";
    private static final String TAG_VISITORGUIDETITLE="visitor_guide_cat_title";
    private static final String TAG_VISITORGUIDEIMAGE="visitor_guide_cat_image";
    //private static final String TAG_VISITORTITLESHOW="visitor_guide_cat_titleshow";

    private Activity activity;

    private ArrayList<HashMap<String, String>> result; 
    private static LayoutInflater inflater=null;
    public GridImageLoader gridimageLoader; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> r) {
        activity = a;
        result=r;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        gridimageLoader=new GridImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return result.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder{
        public TextView text;
        public ImageView image;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.griditem, null);
            //vi = inflater.inflate(R.layout.griditem,parent,false);
            holder=new ViewHolder();
            holder.text=(TextView)vi.findViewById(R.id.textview);;
            holder.image=(ImageView)vi.findViewById(R.id.imageview);
            vi.setTag(holder);
        }
        else

            holder=(ViewHolder)vi.getTag();


        holder.text.setText(result.get(position).get(TAG_VISITORGUIDETITLE));
        holder.image.setTag(result.get(position).get(TAG_VISITORGUIDEIMAGE));
        gridimageLoader.DisplayImage(result.get(position).get(TAG_VISITORGUIDEIMAGE), activity, holder.image);
        return vi;
    }
}

2 个答案:

答案 0 :(得分:3)

您可以使用ArrayAdapter然后调用adapter.clear()清除网格视图,然后使用新数据重新填充它。

您可以使用相同的VisitorMiddleListAsyncTask重新填充数据。

答案 1 :(得分:1)

要清除gridView,您可以设置空适配器或将其可见性设置为不可见或消失。