时间:2021-07-01 10:21:17 帮助过:2人阅读
</pre><span style="font-size:18px;"></span><pre name="code" class="java"><span style="font-size:18px;">public class MainActivity extends Activity { private ListView lv; private Button bt1; private Button bt2; private EditText ed1; private DbUtils dbutils; private List<Goods> list; private Myad ad; private SharedPreferences sp; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 100 && resultCode == 100) { Goods g = (Goods) data.getSerializableExtra("good"); try { dbutils.update(g, WhereBuilder.b("id", "=", g.getId())); } catch (DbException e) { // TODO Auto-generated catch block e.printStackTrace(); } selAll(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbutils = DbUtils.create(this, "db"); list = new ArrayList<Goods>(); getdata(); lv = (ListView) findViewById(R.id.listView1); bt1 = (Button) findViewById(R.id.b1); bt2 = (Button) findViewById(R.id.b2); ed1 = (EditText) findViewById(R.id.ed1); ad = new Myad(); lv.setAdapter(ad); // 模糊查询 bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { list.clear(); String str = ed1.getText().toString(); if (str.equals("")) { Toast.makeText(MainActivity.this, "请输入查询内容", 0).show(); } try { list = dbutils.findAll(Goods.class, WhereBuilder.b("name", "like", "%" + str + "%")); ad.notifyDataSetChanged(); } catch (DbException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); // 全查 bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { selAll(); } }); } private void getdata() { sp = getSharedPreferences("goods", MODE_PRIVATE); boolean bl = sp.getBoolean("first", true); if (bl) { for (int i = 0; i <= 10; i++) { try { dbutils.save(new Goods("商品" + i, "¥" + ((i + 1) * 10))); } catch (DbException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Editor ed = sp.edit(); ed.putBoolean("first", false); ed.commit(); } class Myad extends BaseAdapter { private Button bu1; private Button bu2; private TextView tv1; private TextView tv2; @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { convertView = View.inflate(MainActivity.this, R.layout.item, null); bu1 = (Button) convertView.findViewById(R.id.bu1); bu2 = (Button) convertView.findViewById(R.id.bu2); tv1 = (TextView) convertView.findViewById(R.id.textView1); tv2 = (TextView) convertView.findViewById(R.id.textView2); tv1.setText(list.get(position).getName()); tv2.setText(list.get(position).getPrice()); bu1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, TwoActivity.class); i.putExtra("good", list.get(position)); // startActivity(i); startActivityForResult(i, 100); } }); bu2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Goods g3 = list.get(position); list.remove(position); try { dbutils.delete(Goods.class,WhereBuilder.b("id","=",g3.getId())); } catch (DbException e) { // TODO Auto-generated catch block e.printStackTrace(); } ad.notifyDataSetChanged(); } }); return convertView; } } public void selAll() { try { list.clear(); list = dbutils.findAll(Goods.class); ad.notifyDataSetChanged(); } catch (DbException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }</span>
<span style="font-size:18px;">public class TwoActivity extends Activity { private EditText ed1; private EditText ed2; private Goods g; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); ed1 = (EditText) findViewById(R.id.editText1); ed2 = (EditText) findViewById(R.id.editText2); Intent i = getIntent(); g = (Goods) i.getSerializableExtra("good"); ed1.setText(g.getName()); ed2.setText(g.getPrice()); Button b = (Button) findViewById(R.id.tijiao); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String str1 = ed1.getText().toString(); String str2 = ed2.getText().toString(); g.setName(str1); g.setPrice(str2); Intent i = new Intent(TwoActivity.this, MainActivity.class); i.putExtra("good", g); setResult(100, i); finish(); } }); } }</span>
Dbutil增删改查
标签: