gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:09:36 25 4
gpt4 key购买 nike

如果我在这里使用惰性适配器,当我请求另一个适配器时如何清除 gridview 我将我的代码用于 gridview Activity 和 lazyadapter 我将 asynctask 用于 gridview 项目

这里我放了我的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;
}
}

最佳答案

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

您可以使用相同的 VisitorMiddleListAsyncTask 再次填充数据。

关于android - 如何清除以前的 Gridview 项目并在 android 中重新填充新的适配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14560948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com