android listView的数据如何清空,在添加进去显示啊!
publicclassMessQueryextendsActivity{Spinnersp=null;EditTexted=null;Buttonbn=null;Line...
public class MessQuery extends Activity { Spinner sp = null; EditText ed =null; Button bn =null; LinearLayout line= null; LinearLayout lines = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.query); //获取控件 TextView tv = (TextView)findViewById(R.id.text); ImageView img = (ImageView)findViewById(R.id.img); sp = (Spinner)findViewById(R.id.spi); ed = (EditText)findViewById(R.id.edi); bn = (Button)findViewById(R.id.bun); line = (LinearLayout)findViewById(R.id.lin); lines = (LinearLayout)findViewById(R.id.lines); //赋值 tv.setText(getString(R.string.mq_te).toString()); img.setImageResource(R.drawable.g); //为Spinner设定样式 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //固定的下拉列表的值 adapter.add("登录名"); adapter.add("用户名"); adapter.add("邮\t箱"); sp.setAdapter(adapter);//将数据添加入 //为button添加监听 bn.setOnClickListener(new OnClickListener() { public void onClick(View v) { bunnClick(v); //调用监听方法 } }); } //按钮监听事件 public void bunnClick(View v){ String spstr = String.valueOf(sp.getSelectedItemId()); //通过选择的item判断传递到后台的参数 if("0".equals(spstr)){ toShow("l_name"); }else if("1".equals(spstr)){ toShow("name"); }else if("2".equals(spstr)){ toShow("email"); } } /** *生成界面的方法 * */ void toShow(String la){ String str = ed.getText().toString().trim(); List<User> l = new UserMan().getUsers(la, str); List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); list = getData(l); //对数据进行清空 lines.removeView(line);//将原有的LinearLayout 用以包含的部分移除 //生成数据 SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.userl, new String[]{"img","id","name","l_name","pass","em"}, new int[]{R.id.img,R.id.user_id,R.id.user_name,R.id.user_l_name,R.id.user_pass,R.id.user_em}); //写入数据 ListView lv = new ListView(this); lv.setAdapter(adapter); //将 整理好的SimpleAdapter放入 ListView界面中 lines.addView(lv);//添加显示 setContentView(lines);//显示ListView界面 } //调用生成数据的方法 List<Map<String,Object>> getData(List<User> l){ List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); for(int i =0;i<l.size();i++){ //将信息写入simpleadpater中。 User u = (User)l.get(i); Map<String,Object> map = new HashMap<String,Object>(); map.put("id",u.getU_id()); map.put("name",u.getU_name()); map.put("l_name",u.getU_lo_name()); map.put("pass",u.getU_pass()); map.put("em",u.getU_email()); map.put("img",R.drawable.g); list.add(map); } return list; }}
展开
1个回答
2011-07-19
展开全部
把simpleAdapter换成baseAdapter
//清空:
list=null;
adapter.notifyDataChanged();
//添加:
list=你想赋得值;
adapter.notifyDataChanged();
ps:为什么你arrayAdapter和simpleAdapter 的instance都叫adapter 换个名字吧
//清空:
list=null;
adapter.notifyDataChanged();
//添加:
list=你想赋得值;
adapter.notifyDataChanged();
ps:为什么你arrayAdapter和simpleAdapter 的instance都叫adapter 换个名字吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询