水果展示案例

xiaoxiao2021-02-27  325

app

  mainfests

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.bzu.edu.shopshow"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>

  java

      cn.bzu.edu.shopshow:

        Goodsdao:

public class GoodsDao { private DBHelper dbHelper; public GoodsDao(Context context){ dbHelper=new DBHelper(context,1); } public void add(Goods goods){ SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("name",goods.getName()); values.put("amount",goods.getAmount()); long id=sqLiteDatabase.insert("goods",null,values); goods.setId(id); sqLiteDatabase.close(); } public int delete(long id){ SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase(); int count=sqLiteDatabase.delete("goods","_id=?",new String[]{id+""}); sqLiteDatabase.close(); return count; } public int update(Goods goods){ SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("name",goods.getName()); values.put("amount",goods.getAmount()); int count=sqLiteDatabase.update("goods",values,"_id=?",new String[]{goods.getId()+""}); sqLiteDatabase.close(); return count; } public List<Goods> queryAll(){ List<Goods> goodsList=new ArrayList<>(); SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase(); Cursor cursor=sqLiteDatabase.query("goods",null,null,null,null,null,"amount desc"); while (cursor.moveToNext()){ long id=cursor.getLong(cursor.getColumnIndex("_id")); String name=cursor.getString(cursor.getColumnIndex("name")); int amount=cursor.getInt(cursor.getColumnIndex("amount")); Goods goods=new Goods(id,name,amount); goodsList.add(goods); } cursor.close(); sqLiteDatabase.close(); return goodsList; } }   entity:

   GoodsAdapter:

public class GoodsAdapter extends ArrayAdapter<Goods>{ private int resounceId; private GoodsDao goodsDao; private List<Goods> goodsList; public GoodsAdapter(Context context, int resource, List<Goods> objects, GoodsDao goodsDao) { super(context, resource, objects); resounceId=resource; goodsList=objects; this.goodsDao=goodsDao; } @NonNull @Override public View getView(int position, View convertView, ViewGroup parent) { final Goods goods=getItem(position); View view=null; ViewHolder viewHolder; if (convertView==null){ view=LayoutInflater.from(getContext()).inflate(resounceId,null); viewHolder=new ViewHolder(); viewHolder.tvId=(TextView) view.findViewById(R.id.tvId); viewHolder.tvName=(TextView)view.findViewById(R.id.tvName); viewHolder.tvAmount=(TextView)view.findViewById(R.id.tvAmount); viewHolder.ivUp=(ImageView) view.findViewById(R.id.ivUp); viewHolder.ivDowm=(ImageView) view.findViewById(R.id.ivDown); viewHolder.ivDelete=(ImageView) view.findViewById(R.id.ivDelete); view.setTag(viewHolder); }else { view=convertView; viewHolder=(ViewHolder)view.getTag(); } viewHolder.tvId.setText(goods.getId()+""); viewHolder.tvName.setText(goods.getName()); viewHolder.tvAmount.setText(goods.getAmount()+""); viewHolder.ivDelete.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ AlertDialog.Builder builder=new AlertDialog.Builder(getContext()); builder.setTitle("你确定要删除吗?"); builder.setPositiveButton("yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { goodsList.remove(goods); goodsDao.delete(goods.getId()); notifyDataSetChanged(); } }); builder.setNegativeButton("cancle",null); builder.show(); } }); viewHolder.ivUp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { goods.setAmount(goods.getAmount()-1); goodsDao.update(goods); notifyDataSetChanged(); } }); viewHolder.ivDowm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { goods.setAmount(goods.getAmount()+1); goodsDao.update(goods); notifyDataSetChanged(); } }); return view; } class ViewHolder{ TextView tvId; TextView tvName; TextView tvAmount; ImageView ivUp; ImageView ivDowm; ImageView ivDelete; } } MainActivity:

         

package cn.bzu.edu.shopshow; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import java.util.List; import cn.bzu.edu.shopshow.dao.GoodsDao; import cn.bzu.edu.shopshow.entity.Goods; public class MainActivity extends AppCompatActivity { private EditText etName; private EditText etAmount; private ListView lvGoods; private GoodsAdapter goodsAdapter; private GoodsDao goodsDao; private List<Goods> goodsList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etName=(EditText) findViewById(R.id.etName); etAmount=(EditText) findViewById(R.id.etAmount); goodsDao=new GoodsDao(this); goodsList=goodsDao.queryAll(); goodsAdapter=new GoodsAdapter(this,R.layout.item,goodsList,goodsDao); lvGoods=(ListView)findViewById(R.id.lvGoods); lvGoods.setAdapter(goodsAdapter); } public void addGoods(View view){ String name=etName.getText().toString(); String amount=etAmount.getText().toString(); if (TextUtils.isEmpty(name)||TextUtils.isEmpty(amount)) return; Goods goods=new Goods(name,Integer.parseInt(amount)); goodsDao.add(goods); goodsList.add(goods); goodsAdapter.notifyDataSetChanged(); etName.setText(""); etAmount.setText(""); Toast.makeText(this,"添加成功",Toast.LENGTH_LONG).show(); } } cn.bzu.edu.shopshow(androidTest):

   

ExampleInstrumentedTest public class ExampleInstrumentedTest { @Test public void useAppContext() throws Exception { // Context of the app under test. Context appContext = InstrumentationRegistry.getTargetContext(); assertEquals("cn.bzu.edu.shopshow", appContext.getPackageName()); } } cn.bzu.edu.shopshow(test):

ExampleUnitTest      public class ExampleUnitTest { @Test public void addition_isCorrect() throws Exception { assertEquals(4, 2 + 2); } }

res

   layout

       activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="8dp" tools:context="cn.bzu.edu.shopshow.MainActivity"> <LinearLayout android:id="@+id/llAdd" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/etName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="商品名称" android:inputType="textPersonName"/> <EditText android:id="@+id/etAmount" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="商品金额" android:inputType="number"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="addGoods" android:id="@+id/ivAdd" android:src="@android:drawable/ic_input_add"/> </LinearLayout> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lvGoods" android:layout_below="@+id/llAdd" > </ListView> </LinearLayout>           item.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:padding="10dp"> <TextView android:id="@+id/tvId" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="13" android:textColor="#000000" android:textSize="20sp" /> <TextView android:id="@+id/tvName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="商品名称" android:gravity="left" android:textColor="#000000" android:textSize="20sp" /> <TextView android:id="@+id/tvAmount" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="金额" android:gravity="left" android:textColor="#000000" android:textSize="20sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/ivUp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="2dp" android:src="@android:drawable/arrow_up_float" /> <ImageView android:id="@+id/ivDown" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/arrow_up_float" /> </LinearLayout> <ImageView android:id="@+id/ivDelete" android:layout_width="25dp" android:layout_height="25dp" android:src="@android:drawable/ic_menu_delete" /> </LinearLayout>

转载请注明原文地址: https://www.6miu.com/read-1246.html

最新回复(0)