├── .gitignore ├── .idea ├── .gitignore ├── compiler.xml ├── dbnavigator.xml ├── gradle.xml ├── misc.xml └── vcs.xml ├── Android开发设计大作业报告.docx ├── app ├── .gitignore ├── build.gradle.kts ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── edu │ │ └── bupt │ │ └── shopeasy │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── edu │ │ │ └── bupt │ │ │ └── shopeasy │ │ │ ├── DetailActivity.java │ │ │ ├── LoginActivity.java │ │ │ ├── MainActivity.java │ │ │ ├── RegisterActivity.java │ │ │ ├── adapter │ │ │ ├── CartAdapter.java │ │ │ ├── HomeAdapter.java │ │ │ └── TypeAdapter.java │ │ │ ├── database │ │ │ ├── CartDatabaseHelper.java │ │ │ ├── ProductDatabaseHelper.java │ │ │ └── UserDatabaseHelper.java │ │ │ └── fragment │ │ │ ├── BaseFragment.java │ │ │ ├── CartFragment.java │ │ │ ├── HomeFragment.java │ │ │ ├── TypeFragment.java │ │ │ └── UserFragment.java │ └── res │ │ ├── color │ │ ├── icon_color_selector.xml │ │ └── text_color_selector.xml │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ ├── image_1.jpg │ │ ├── image_10.webp │ │ ├── image_11.jpg │ │ ├── image_12.jpg │ │ ├── image_13.jpg │ │ ├── image_14.webp │ │ ├── image_15.jpg │ │ ├── image_16.jpg │ │ ├── image_17.jpg │ │ ├── image_18.jpg │ │ ├── image_19.jpeg │ │ ├── image_2.jpeg │ │ ├── image_20.jpg │ │ ├── image_3.jpg │ │ ├── image_4.jpg │ │ ├── image_5.png │ │ ├── image_6.jpg │ │ ├── image_7.jpg │ │ ├── image_8.jpg │ │ └── image_9.jpg │ │ ├── layout │ │ ├── activity_detail.xml │ │ ├── activity_login.xml │ │ ├── activity_main.xml │ │ ├── activity_register.xml │ │ ├── fragment_cart.xml │ │ ├── fragment_home.xml │ │ ├── fragment_type.xml │ │ ├── fragment_user.xml │ │ ├── product_cart.xml │ │ ├── product_home.xml │ │ └── product_type.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── values-night │ │ └── themes.xml │ │ ├── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ ├── style.xml │ │ └── themes.xml │ │ └── xml │ │ ├── backup_rules.xml │ │ └── data_extraction_rules.xml │ └── test │ └── java │ └── edu │ └── bupt │ └── shopeasy │ └── ExampleUnitTest.java ├── build.gradle.kts ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle.kts /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # 默认忽略的文件 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/dbnavigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Android开发设计大作业报告.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/Android开发设计大作业报告.docx -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("com.android.application") 3 | } 4 | 5 | android { 6 | namespace = "edu.bupt.shopeasy" 7 | compileSdk = 33 8 | 9 | defaultConfig { 10 | applicationId = "edu.bupt.shopeasy" 11 | minSdk = 16 12 | targetSdk = 33 13 | versionCode = 1 14 | versionName = "1.0" 15 | 16 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | isMinifyEnabled = false 22 | proguardFiles( 23 | getDefaultProguardFile("proguard-android-optimize.txt"), 24 | "proguard-rules.pro" 25 | ) 26 | } 27 | } 28 | compileOptions { 29 | sourceCompatibility = JavaVersion.VERSION_1_8 30 | targetCompatibility = JavaVersion.VERSION_1_8 31 | } 32 | } 33 | 34 | dependencies { 35 | 36 | implementation("androidx.appcompat:appcompat:1.6.1") 37 | implementation("com.google.android.material:material:1.8.0") 38 | implementation("androidx.constraintlayout:constraintlayout:2.1.4") 39 | implementation("androidx.recyclerview:recyclerview:1.3.2") 40 | testImplementation("junit:junit:4.13.2") 41 | androidTestImplementation("androidx.test.ext:junit:1.1.5") 42 | androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") 43 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/edu/bupt/shopeasy/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | assertEquals("edu.bupt.shopeasy", appContext.getPackageName()); 25 | } 26 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 16 | 19 | 22 | 25 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/DetailActivity.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.content.SharedPreferences; 7 | import android.database.Cursor; 8 | import android.database.sqlite.SQLiteDatabase; 9 | import android.os.Bundle; 10 | import android.util.Log; 11 | import android.view.View; 12 | import android.widget.Button; 13 | import android.widget.ImageView; 14 | import android.widget.TextView; 15 | import android.widget.Toast; 16 | import edu.bupt.shopeasy.adapter.CartAdapter; 17 | import edu.bupt.shopeasy.database.CartDatabaseHelper; 18 | import edu.bupt.shopeasy.database.ProductDatabaseHelper; 19 | 20 | // 商品详情页面 21 | public class DetailActivity extends Activity { 22 | private static final String TAG = "DetailActivity"; 23 | private Context mContext; 24 | private ImageView productImage; 25 | private TextView productName; 26 | private TextView productPrice; 27 | private TextView productType; 28 | private Button addToCart; 29 | 30 | CartDatabaseHelper cartDatabaseHelper; 31 | SQLiteDatabase db; 32 | Cursor cursor; 33 | String name; 34 | String image; 35 | double price; 36 | String type; 37 | SharedPreferences sp; 38 | String username; 39 | 40 | CartAdapter adapter; 41 | protected void onCreate(Bundle savedInstanceState) { 42 | super.onCreate(savedInstanceState); 43 | setContentView(R.layout.activity_detail); 44 | mContext = this; 45 | productImage = findViewById(R.id.product_image); 46 | productName = findViewById(R.id.product_name); 47 | productPrice = findViewById(R.id.product_price); 48 | productType = findViewById(R.id.product_type); 49 | addToCart = findViewById(R.id.add_to_cart); 50 | Intent intent =getIntent(); 51 | String id = intent.getStringExtra("id"); 52 | // 根据商品的id,查询数据库,获取商品的详细信息 53 | ProductDatabaseHelper dbHelper = new ProductDatabaseHelper(mContext, "Product", null, 1); 54 | SQLiteDatabase db = dbHelper.getReadableDatabase(); 55 | Cursor cursor = db.query("product", null, "id = ?", new String[]{id}, null, null, null); 56 | if (cursor.moveToFirst()) { 57 | // 获取商品的属性,例如name,price,type,image等 58 | name = cursor.getString(cursor.getColumnIndexOrThrow("name")); 59 | price = cursor.getDouble(cursor.getColumnIndexOrThrow("price")); 60 | type = cursor.getString(cursor.getColumnIndexOrThrow("type")); 61 | image = cursor.getString(cursor.getColumnIndexOrThrow("image")); 62 | // 设置控件的内容,根据商品的属性 63 | productName.setText(name); 64 | productPrice.setText("¥" + price); 65 | productType.setText(type); 66 | // 使用getIdentifier方法,传入文件名,资源类型,和包名,返回图片的资源id 67 | int imageId = getResources().getIdentifier(image, "drawable", getPackageName()); 68 | // 使用setImageResource方法,传入图片的资源id,设置商品图片 69 | productImage.setImageResource(imageId); 70 | } 71 | 72 | Listener(); 73 | } 74 | // 添加购物车时的监听 75 | private void Listener() { 76 | addToCart.setOnClickListener(new View.OnClickListener() { 77 | @Override 78 | public void onClick(View view) { 79 | cartDatabaseHelper = new CartDatabaseHelper(mContext, "Cart", null, 1); 80 | sp = mContext.getSharedPreferences("username", MODE_PRIVATE); 81 | username = sp.getString("Loginname", ""); 82 | if(username == ""){ 83 | Toast.makeText(mContext, "请先登录!", Toast.LENGTH_SHORT).show(); 84 | return; 85 | } 86 | int count = 1; 87 | boolean exist = cartDatabaseHelper.queryCart(name, username); 88 | if(exist){ 89 | cartDatabaseHelper.updateCart(name, username, count); 90 | Toast.makeText(mContext, "商品已经在购物车了。", Toast.LENGTH_SHORT).show(); 91 | }else { 92 | cartDatabaseHelper.insertCart(name, price, type, image, count, username); 93 | Toast.makeText(mContext, "添加成功!", Toast.LENGTH_SHORT).show(); 94 | } 95 | } 96 | }); 97 | } 98 | 99 | @Override 100 | protected void onStart() { 101 | super.onStart(); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/LoginActivity.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy; 2 | 3 | 4 | import android.app.Activity; 5 | import android.content.Intent; 6 | import android.content.SharedPreferences; 7 | import android.database.Cursor; 8 | import android.database.sqlite.SQLiteDatabase; 9 | import android.os.Bundle; 10 | import android.view.View; 11 | import android.widget.Button; 12 | import android.widget.EditText; 13 | import android.widget.Toast; 14 | 15 | import edu.bupt.shopeasy.database.UserDatabaseHelper; 16 | 17 | public class LoginActivity extends Activity { 18 | 19 | EditText name,pwd; 20 | Button login; 21 | UserDatabaseHelper userDatabaseHelper; 22 | 23 | SQLiteDatabase db; 24 | SharedPreferences sp1, sp2; 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_login); 30 | name = this.findViewById(R.id.name); //用户名输入框 31 | pwd = this.findViewById(R.id.pwd); //密码输入框 32 | Button login = (Button) findViewById(R.id.login); 33 | sp1 = this.getSharedPreferences("useinfo",this.MODE_PRIVATE); 34 | sp2 = this.getSharedPreferences("username",this.MODE_PRIVATE); 35 | 36 | name.setText(sp1.getString("usname",null)); 37 | pwd.setText(sp1.getString("uspwd",null)); 38 | userDatabaseHelper = new UserDatabaseHelper(this,"Userinfo",null,1); //建数据库或者取数据库 39 | db = userDatabaseHelper.getReadableDatabase(); 40 | login.setOnClickListener(new View.OnClickListener() { 41 | @Override 42 | public void onClick(View view) { 43 | Toast.makeText(LoginActivity.this, "正在登录", Toast.LENGTH_LONG).show(); 44 | String username = name.getText().toString(); 45 | String password = pwd.getText().toString(); //获取用户输入的用户名和密码 46 | //查询用户名和密码相同的数据 47 | Cursor cursor = db.query("users",new String[]{"usname","uspwd"}," usname=? and uspwd=?",new String[]{username,password},null,null,null); 48 | 49 | int flag = cursor.getCount(); //查询出来的记录项的条数,若没有该用户则为0条 50 | if(flag!=0){ //若查询出的记录不为0,则进行跳转操作 51 | Intent intent = new Intent(); 52 | intent.setClass(LoginActivity.this,MainActivity.class); //设置页面跳转 53 | SharedPreferences.Editor editor = sp2.edit(); 54 | cursor.moveToFirst(); //将光标移动到position为0的位置,默认位置为-1 55 | String loginname = cursor.getString(0); 56 | editor.putString("Loginname",loginname); 57 | editor.commit(); //将用户名存到SharedPreferences中 58 | Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show(); 59 | startActivity(intent); 60 | } 61 | else{ 62 | Toast.makeText(LoginActivity.this,"用户名或密码错误!",Toast.LENGTH_LONG).show(); //提示用户信息错误或没有账号 63 | } 64 | } 65 | }); 66 | } 67 | } -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/MainActivity.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy; 2 | 3 | import androidx.appcompat.app.AppCompatActivity; 4 | import androidx.fragment.app.FragmentTransaction; 5 | import android.os.Bundle; 6 | import android.util.Log; 7 | import android.widget.RadioGroup; 8 | import java.util.ArrayList; 9 | import edu.bupt.shopeasy.database.ProductDatabaseHelper; 10 | import edu.bupt.shopeasy.fragment.BaseFragment; 11 | import edu.bupt.shopeasy.fragment.CartFragment; 12 | import edu.bupt.shopeasy.fragment.HomeFragment; 13 | import edu.bupt.shopeasy.fragment.TypeFragment; 14 | import edu.bupt.shopeasy.fragment.UserFragment; 15 | 16 | public class MainActivity extends AppCompatActivity { 17 | private static final String TAG = "MainActivity"; 18 | private int fragmentIndex; 19 | private BaseFragment tempFragment; 20 | private RadioGroup bottom_fragment; 21 | 22 | private ProductDatabaseHelper productDatabaseHelper; 23 | 24 | 25 | ArrayList fragmentArrayList; 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_main); 30 | Log.v(TAG, "OnCreate"); 31 | bottom_fragment = findViewById(R.id.fragment_group); 32 | bottom_fragment.check(R.id.home_bottom); 33 | 34 | fragmentArrayList = new ArrayList<>(); 35 | fragmentArrayList.add(new HomeFragment()); 36 | fragmentArrayList.add(new TypeFragment()); 37 | fragmentArrayList.add(new CartFragment()); 38 | fragmentArrayList.add(new UserFragment()); 39 | 40 | Listener(); 41 | 42 | BaseFragment homeFragment = fragmentArrayList.get(0); 43 | FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 44 | transaction.add(R.id.frameLayout, homeFragment).commit(); 45 | tempFragment = homeFragment; 46 | } 47 | private void Listener() { 48 | bottom_fragment.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 49 | @Override 50 | public void onCheckedChanged(RadioGroup radioGroup, int i) { 51 | Log.i(TAG, "id = " + i); 52 | if (i == R.id.home_bottom) { 53 | fragmentIndex = 0; 54 | } else if (i == R.id.type_bottom) { 55 | fragmentIndex = 1; 56 | } else if (i == R.id.cart_bottom) { 57 | fragmentIndex = 2; 58 | } else if (i == R.id.user_bottom) { 59 | fragmentIndex = 3; 60 | } 61 | BaseFragment toFragment = getFragment(fragmentIndex); 62 | switchFragment(tempFragment, toFragment); 63 | } 64 | }); 65 | } 66 | private BaseFragment getFragment(int fragmentIndex) { 67 | if (fragmentArrayList != null && fragmentArrayList.size() > 0) { 68 | BaseFragment fragment = fragmentArrayList.get(fragmentIndex); 69 | return fragment; 70 | } 71 | return null; 72 | } 73 | private void switchFragment(BaseFragment fromFragment, BaseFragment toFragment) { 74 | if(tempFragment != toFragment) { 75 | tempFragment = toFragment; 76 | if(toFragment != null) { 77 | FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 78 | if(!toFragment.isAdded()) { 79 | if(fromFragment != null) { 80 | fromFragment.saveData(); 81 | transaction.hide(fromFragment); 82 | } 83 | transaction.add(R.id.frameLayout, toFragment).commit(); 84 | } else { 85 | if(fromFragment != null) { 86 | fromFragment.saveData(); 87 | transaction.hide(fromFragment); 88 | } 89 | toFragment.refreshData(); 90 | transaction.show(toFragment).commit(); 91 | } 92 | } 93 | } 94 | } 95 | } -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/RegisterActivity.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy; 2 | 3 | import android.app.Activity; 4 | import android.content.ContentValues; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.content.SharedPreferences; 8 | import android.database.Cursor; 9 | import android.database.sqlite.SQLiteDatabase; 10 | import android.os.Bundle; 11 | import android.view.View; 12 | import android.widget.Button; 13 | import android.widget.EditText; 14 | import android.widget.Toast; 15 | import edu.bupt.shopeasy.database.UserDatabaseHelper; 16 | 17 | public class RegisterActivity extends Activity { 18 | private static final String TAG = "RegisterActivity"; 19 | private Context mContext; 20 | EditText usename,usepwd,usepwd2; 21 | Button submit; 22 | UserDatabaseHelper userDatabaseHelper; 23 | SQLiteDatabase db; 24 | SharedPreferences sp; 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_register); 29 | mContext = this; 30 | 31 | usename = this.findViewById(R.id.usename); //用户名编辑框 32 | usepwd = this.findViewById(R.id.usepwd); //设置初始密码编辑框 33 | usepwd2 = this.findViewById(R.id.usepwd2); //二次输入密码编辑框 34 | submit = this.findViewById(R.id.submit); //注册按钮 35 | 36 | userDatabaseHelper = new UserDatabaseHelper(this,"Userinfo",null,1); //建数据库 37 | db = userDatabaseHelper.getReadableDatabase(); 38 | sp = this.getSharedPreferences("useinfo",this.MODE_PRIVATE); 39 | 40 | Listener(); 41 | } 42 | private void Listener() { 43 | submit.setOnClickListener(new View.OnClickListener() { 44 | boolean flag = true; //判断用户是否已存在的标志位 45 | @Override 46 | public void onClick(View view) { 47 | String name = usename.getText().toString(); //用户名 48 | String pwd01 = usepwd.getText().toString(); //密码 49 | String pwd02 = usepwd2.getText().toString(); //二次输入的密码 50 | String sex = ""; //性别 51 | if(name.equals("")||pwd01 .equals("")||pwd02.equals("")){ 52 | Toast.makeText(RegisterActivity.this, "用户名或密码不能为空!!", Toast.LENGTH_LONG).show(); 53 | } 54 | else{ 55 | Cursor cursor = db.query("users",new String[]{"usname"},null,null,null,null,null); 56 | 57 | while (cursor.moveToNext()){ 58 | if(cursor.getString(0).equals(name)){ 59 | flag = false; 60 | break; 61 | } 62 | } 63 | if(flag==true){ //判断用户是否已存在 64 | if (pwd01.equals(pwd02)) { //判断两次输入的密码是否一致,若一致则继续,不一致则提醒密码不一致 65 | ContentValues cv = new ContentValues(); 66 | cv.put("usname",name); 67 | cv.put("uspwd",pwd01); 68 | db.insert("users",null,cv); 69 | SharedPreferences.Editor editor = sp.edit(); 70 | editor.putString("usname",name); 71 | editor.putString("uspwd",pwd01); 72 | editor.commit(); 73 | Intent intent = new Intent(); 74 | intent.setClass(RegisterActivity.this,MainActivity.class); //跳转到登录页面 75 | startActivity(intent); 76 | Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_LONG).show(); 77 | } 78 | else { 79 | Toast.makeText(RegisterActivity.this, "密码不一致!", Toast.LENGTH_LONG).show(); //提示密码不一致 80 | } 81 | } 82 | else{ 83 | Toast.makeText(RegisterActivity.this, "用户已存在!", Toast.LENGTH_LONG).show(); //提示密码不一致 84 | } 85 | } 86 | } 87 | }); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/adapter/CartAdapter.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy.adapter; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.database.Cursor; 7 | import android.util.Log; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.Button; 12 | import android.widget.ImageView; 13 | import android.widget.TextView; 14 | import android.widget.Toast; 15 | import androidx.recyclerview.widget.RecyclerView; 16 | import edu.bupt.shopeasy.DetailActivity; 17 | import edu.bupt.shopeasy.R; 18 | import edu.bupt.shopeasy.database.CartDatabaseHelper; 19 | 20 | public class CartAdapter extends RecyclerView.Adapter { 21 | private static final String TAG = "CartAdapter"; 22 | // 定义一个Context对象,用于获取上下文 23 | private Context context; 24 | // 定义一个Cursor对象,用于存储查询商品数据库的结果 25 | private Cursor cursor; 26 | // 构造方法,传入Context对象和Cursor对象 27 | public CartAdapter(Context context, Cursor cursor) { 28 | this.context = context; 29 | this.cursor = cursor; 30 | } 31 | 32 | // 定义一个ViewHolder类,继承自RecyclerView.ViewHolder,用于封装商品列表项的视图 33 | public class CartViewHolder extends RecyclerView.ViewHolder { 34 | public String name; 35 | public String username; 36 | public int count; 37 | public double price; 38 | // 定义视图中的控件,例如ImageView,TextView等 39 | ImageView productImage; // 商品图片 40 | TextView productName; // 商品名称 41 | TextView productPrice; // 商品价格 42 | TextView productType; // 商品类型 43 | TextView productCount; // 商品数量 44 | Button productMinus; 45 | Button productAdd; 46 | 47 | CartDatabaseHelper cartDatabaseHelper; 48 | 49 | // 构造方法,传入一个View对象,用于初始化控件 50 | public CartViewHolder(View view) { 51 | super(view); 52 | 53 | productImage = view.findViewById(R.id.product_image); 54 | productName = view.findViewById(R.id.product_name); 55 | productPrice = view.findViewById(R.id.product_price); 56 | productType = view.findViewById(R.id.product_type); 57 | productCount = view.findViewById(R.id.product_count); 58 | productMinus = view.findViewById(R.id.product_minus ); 59 | productAdd = view.findViewById(R.id.product_add); 60 | 61 | 62 | 63 | view.setOnClickListener(new View.OnClickListener() { 64 | @Override 65 | public void onClick(View view) { 66 | // 获取当前点击的商品的id 67 | int position = getAdapterPosition(); 68 | cursor.moveToPosition(position); 69 | String id = cursor.getString(cursor.getColumnIndexOrThrow("id")); 70 | Log.i(TAG, "id: "+ id); 71 | // 使用Intent对象和Context对象,启动一个新的Activity,用于显示商品详情页 72 | Intent intent = new Intent(context, DetailActivity.class); 73 | // 传递商品的id作为额外数据 74 | intent.putExtra("id", id); 75 | context.startActivity(intent); 76 | } 77 | }); 78 | productMinus.setOnClickListener(new View.OnClickListener() { 79 | @SuppressLint("NotifyDataSetChanged") 80 | @Override 81 | public void onClick(View view) { 82 | Log.i(TAG, "减少商品数量按钮监听方法"); 83 | cartDatabaseHelper = new CartDatabaseHelper(context, "Cart", null, 1); 84 | Log.i(TAG, "减少"+name +username); 85 | // 判断count是否大于1 86 | if (count > 1) { 87 | // 减少数量 88 | count--; 89 | Log.i(TAG, "减少" + name + username + "的数量为" + String.valueOf(count)); 90 | cartDatabaseHelper.updateCart(name, username, count); 91 | productCount.setText("数目:"+count); 92 | productPrice.setText("¥ "+ count*price); 93 | } else { 94 | Toast.makeText(context, "不能再减少了", Toast.LENGTH_SHORT).show(); 95 | } 96 | } 97 | }); 98 | productAdd.setOnClickListener(new View.OnClickListener() { 99 | @Override 100 | public void onClick(View view) { 101 | Log.i(TAG, "增加商品数量按钮监听方法"); 102 | 103 | cartDatabaseHelper = new CartDatabaseHelper(context, "Cart", null, 1); 104 | count++; 105 | cartDatabaseHelper.updateCart(name, username, count); 106 | Log.i(TAG, "增加"+name +username); 107 | Log.i(TAG, "count"+count); 108 | productCount.setText("数目:"+count); 109 | productPrice.setText("¥ "+ count*price); 110 | } 111 | }); 112 | } 113 | } 114 | 115 | // 重写onCreateViewHolder方法,用于创建ViewHolder对象 116 | @Override 117 | public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 118 | // 使用LayoutInflater对象加载布局文件,创建一个新的视图对象 119 | View view = LayoutInflater.from(context).inflate(R.layout.product_cart, parent, false); 120 | // 返回一个包含该视图的ViewHolder对象 121 | return new CartViewHolder(view); 122 | } 123 | 124 | // 重写onBindViewHolder方法,用于绑定ViewHolder对象和数据 125 | @Override 126 | public void onBindViewHolder(CartViewHolder holder, int position) { 127 | // 使用Cursor对象移动到指定位置 128 | cursor.moveToPosition(position); 129 | // 获取商品的属性,例如name,price,type,image等 130 | // 使用@SuppressLint注解,忽略Range这个lint警告 131 | @SuppressLint("Range") 132 | // 获取商品的name字段的索引值,如果不存在,抛出异常 133 | int nameIndex = cursor.getColumnIndexOrThrow("name"); 134 | 135 | // 获取商品的price字段的索引值,如果不存在,抛出异常 136 | int priceIndex = cursor.getColumnIndexOrThrow("price"); 137 | // 获取商品的type字段的索引值,如果不存在,抛出异常 138 | int typeIndex = cursor.getColumnIndexOrThrow("type"); 139 | // 获取商品的image字段的索引值,如果不存在,抛出异常 140 | int imageIndex = cursor.getColumnIndexOrThrow("image"); 141 | int countIndex = cursor.getColumnIndexOrThrow("count"); 142 | int usernameIndex = cursor.getColumnIndexOrThrow("username"); 143 | // 使用索引值,获取商品的name,price,type,image等字段的值 144 | String name = cursor.getString(nameIndex); 145 | Log.i(TAG, "获取到名字: "+ name); 146 | double price = cursor.getDouble(priceIndex); 147 | Log.i(TAG, "获取到价格: "+ price); 148 | String type = cursor.getString(typeIndex); 149 | Log.i(TAG, "获取到类型: "+ type); 150 | String image = cursor.getString(imageIndex); 151 | Log.i(TAG, "获取到图片: "+ image); 152 | int count = cursor.getInt(countIndex); 153 | Log.i(TAG, "获取到数量: "+ count); 154 | String username = cursor.getString(usernameIndex); 155 | 156 | // 给holder的name,username,count,price等属性赋值 157 | holder.name = name; 158 | holder.username = username; 159 | holder.count = count; 160 | holder.price = price; 161 | 162 | // 设置ViewHolder对象的视图内容,根据商品的属性 163 | holder.productName.setText(name); 164 | Log.i(TAG, "设置为名字: "+ name); 165 | holder.productPrice.setText("¥" + count*price); 166 | holder.productType.setText(type); 167 | // 使用if语句,判断你的Context对象是否等于null,如果是,打印出错误信息或抛出异常 168 | //Log.e(TAG, "Context"+ context); 169 | // 使用getIdentifier方法,传入文件名,资源类型,和包名,返回图片的资源id 170 | int imageId = context.getResources().getIdentifier(image, "drawable", context.getPackageName()); 171 | // 使用setImageResource方法,传入图片的资源id,设置商品图片 172 | Log.i(TAG, "设置为图片: "+ imageId); 173 | holder.productImage.setImageResource(imageId); 174 | 175 | holder.productCount.setText("数目:"+count); 176 | } 177 | 178 | // 重写getItemCount方法,用于返回Cursor对象的行数,即商品的数量 179 | @Override 180 | public int getItemCount() { 181 | return cursor.getCount(); 182 | } 183 | // 定义一个计算属性,用于计算购物车中所有商品的总价 184 | public double getTotalPrice() { 185 | // 初始化总价为0 186 | double totalPrice = 0; 187 | // 遍历Cursor对象 188 | for (int i = 0; i < cursor.getCount(); i++) { 189 | // 移动到指定位置 190 | cursor.moveToPosition(i); 191 | // 获取商品的价格和数量 192 | double price = cursor.getDouble(cursor.getColumnIndexOrThrow("price")); 193 | int count = cursor.getInt(cursor.getColumnIndexOrThrow("count")); 194 | // 计算商品的小计,并累加到总价 195 | totalPrice += price * count; 196 | } 197 | // 返回总价 198 | return totalPrice; 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/adapter/HomeAdapter.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy.adapter; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.database.Cursor; 7 | import android.util.Log; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.ImageView; 12 | import android.widget.TextView; 13 | import androidx.recyclerview.widget.RecyclerView; 14 | import edu.bupt.shopeasy.DetailActivity; 15 | import edu.bupt.shopeasy.R; 16 | 17 | public class HomeAdapter extends RecyclerView.Adapter { 18 | private static final String TAG = "HomeAdapter"; 19 | // 定义一个Context对象,用于获取上下文 20 | private Context context; 21 | // 定义一个Cursor对象,用于存储查询商品数据库的结果 22 | private Cursor cursor; 23 | 24 | // 构造方法,传入Context对象和Cursor对象 25 | public HomeAdapter(Context context, Cursor cursor) { 26 | this.context = context; 27 | this.cursor = cursor; 28 | } 29 | 30 | // 定义一个ViewHolder类,继承自RecyclerView.ViewHolder,用于封装商品列表项的视图 31 | public class HomeViewHolder extends RecyclerView.ViewHolder { 32 | // 定义视图中的控件,例如ImageView,TextView等 33 | ImageView productImage; // 商品图片 34 | TextView productName; // 商品名称 35 | TextView productPrice; // 商品价格 36 | TextView productType; // 商品类型 37 | 38 | // 构造方法,传入一个View对象,用于初始化控件 39 | public HomeViewHolder(View view) { 40 | super(view); 41 | 42 | productImage = view.findViewById(R.id.product_image); 43 | productName = view.findViewById(R.id.product_name); 44 | productPrice = view.findViewById(R.id.product_price); 45 | productType = view.findViewById(R.id.product_type); 46 | view.setOnClickListener(new View.OnClickListener() { 47 | @Override 48 | public void onClick(View view) { 49 | // 获取当前点击的商品的id 50 | int position = getAdapterPosition(); 51 | cursor.moveToPosition(position); 52 | String id = cursor.getString(cursor.getColumnIndexOrThrow("id")); 53 | Log.i(TAG, "id: "+ id); 54 | // 使用Intent对象和Context对象,启动一个新的Activity,用于显示商品详情页 55 | Intent intent = new Intent(context, DetailActivity.class); 56 | // 传递商品的id作为额外数据 57 | intent.putExtra("id", id); 58 | context.startActivity(intent); 59 | } 60 | }); 61 | } 62 | } 63 | 64 | // 重写onCreateViewHolder方法,用于创建ViewHolder对象 65 | @Override 66 | public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 67 | // 使用LayoutInflater对象加载布局文件,创建一个新的视图对象 68 | View view = LayoutInflater.from(context).inflate(R.layout.product_home, parent, false); 69 | // 返回一个包含该视图的ViewHolder对象 70 | return new HomeViewHolder(view); 71 | } 72 | 73 | // 重写onBindViewHolder方法,用于绑定ViewHolder对象和数据 74 | @Override 75 | public void onBindViewHolder(HomeViewHolder holder, int position) { 76 | // 使用Cursor对象移动到指定位置 77 | cursor.moveToPosition(position); 78 | // 获取商品的属性,例如name,price,type,image等 79 | // 使用@SuppressLint注解,忽略Range这个lint警告 80 | @SuppressLint("Range") 81 | // 获取商品的name字段的索引值,如果不存在,抛出异常 82 | int nameIndex = cursor.getColumnIndexOrThrow("name"); 83 | 84 | // 获取商品的price字段的索引值,如果不存在,抛出异常 85 | int priceIndex = cursor.getColumnIndexOrThrow("price"); 86 | // 获取商品的type字段的索引值,如果不存在,抛出异常 87 | int typeIndex = cursor.getColumnIndexOrThrow("type"); 88 | // 获取商品的image字段的索引值,如果不存在,抛出异常 89 | int imageIndex = cursor.getColumnIndexOrThrow("image"); 90 | // 使用索引值,获取商品的name,price,type,image等字段的值 91 | String name = cursor.getString(nameIndex); 92 | Log.i(TAG, "获取到名字: "+ name); 93 | double price = cursor.getDouble(priceIndex); 94 | Log.i(TAG, "获取到价格: "+ price); 95 | String type = cursor.getString(typeIndex); 96 | Log.i(TAG, "获取到类型: "+ type); 97 | String image = cursor.getString(imageIndex); 98 | Log.i(TAG, "获取到图片: "+ image); 99 | // 设置ViewHolder对象的视图内容,根据商品的属性 100 | holder.productName.setText(name); 101 | Log.i(TAG, "设置为名字: "+ name); 102 | holder.productPrice.setText("¥" + price); 103 | holder.productType.setText(type); 104 | // 使用if语句,判断你的Context对象是否等于null,如果是,打印出错误信息或抛出异常 105 | //Log.e(TAG, "Context"+ context); 106 | // 使用getIdentifier方法,传入文件名,资源类型,和包名,返回图片的资源id 107 | int imageId = context.getResources().getIdentifier(image, "drawable", context.getPackageName()); 108 | // 使用setImageResource方法,传入图片的资源id,设置商品图片 109 | Log.i(TAG, "设置为图片: "+ imageId); 110 | holder.productImage.setImageResource(imageId); 111 | } 112 | 113 | // 重写getItemCount方法,用于返回Cursor对象的行数,即商品的数量 114 | @Override 115 | public int getItemCount() { 116 | return cursor.getCount(); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/adapter/TypeAdapter.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy.adapter; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.database.Cursor; 7 | import android.util.Log; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.ImageView; 12 | import android.widget.TextView; 13 | import androidx.annotation.NonNull; 14 | import androidx.recyclerview.widget.RecyclerView; 15 | import edu.bupt.shopeasy.DetailActivity; 16 | import edu.bupt.shopeasy.R; 17 | 18 | public class TypeAdapter extends RecyclerView.Adapter{ 19 | private static final String TAG = "TypeAdapter"; 20 | // 定义一个Context对象,用于获取上下文 21 | private Context context; 22 | // 定义一个Cursor对象,用于存储查询商品数据库的结果 23 | private Cursor cursor; 24 | public TypeAdapter(Context context, Cursor cursor) { 25 | this.context = context; 26 | this.cursor = cursor; 27 | } 28 | public class TypeViewHolder extends RecyclerView.ViewHolder { 29 | ImageView productImage; // 商品图片 30 | TextView productName; // 商品名称 31 | TextView productPrice; // 商品价格 32 | TextView productType; // 商品类型 33 | 34 | public TypeViewHolder(@NonNull View view) { 35 | super(view); 36 | productImage = view.findViewById(R.id.product_image); 37 | productName = view.findViewById(R.id.product_name); 38 | productPrice = view.findViewById(R.id.product_price); 39 | productType = view.findViewById(R.id.product_type); 40 | view.setOnClickListener(new View.OnClickListener() { 41 | @Override 42 | public void onClick(View view) { 43 | // 获取当前点击的商品的id 44 | int position = getAdapterPosition(); 45 | cursor.moveToPosition(position); 46 | String id = cursor.getString(cursor.getColumnIndexOrThrow("id")); 47 | Log.i(TAG, "id: "+ id); 48 | // 使用Intent对象和Context对象,启动一个新的Activity,用于显示商品详情页 49 | Intent intent = new Intent(context, DetailActivity.class); 50 | // 传递商品的id作为额外数据 51 | intent.putExtra("id", id); 52 | context.startActivity(intent); 53 | } 54 | }); 55 | } 56 | } 57 | @NonNull 58 | @Override 59 | public TypeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 60 | // 使用LayoutInflater对象加载布局文件,创建一个新的视图对象 61 | View view = LayoutInflater.from(context).inflate(R.layout.product_type, parent, false); 62 | // 返回一个包含该视图的ViewHolder对象 63 | return new TypeAdapter.TypeViewHolder(view); 64 | } 65 | 66 | @Override 67 | public void onBindViewHolder(@NonNull TypeAdapter.TypeViewHolder holder, int position) { 68 | // 使用Cursor对象移动到指定位置 69 | cursor.moveToPosition(position); 70 | // 获取商品的属性,例如name,price,type,image等 71 | // 使用@SuppressLint注解,忽略Range这个lint警告 72 | @SuppressLint("Range") 73 | // 获取商品的name字段的索引值,如果不存在,抛出异常 74 | int nameIndex = cursor.getColumnIndexOrThrow("name"); 75 | 76 | // 获取商品的price字段的索引值,如果不存在,抛出异常 77 | int priceIndex = cursor.getColumnIndexOrThrow("price"); 78 | // 获取商品的type字段的索引值,如果不存在,抛出异常 79 | int typeIndex = cursor.getColumnIndexOrThrow("type"); 80 | // 获取商品的image字段的索引值,如果不存在,抛出异常 81 | int imageIndex = cursor.getColumnIndexOrThrow("image"); 82 | // 使用索引值,获取商品的name,price,type,image等字段的值 83 | String name = cursor.getString(nameIndex); 84 | Log.i(TAG, "获取到名字: "+ name); 85 | double price = cursor.getDouble(priceIndex); 86 | Log.i(TAG, "获取到价格: "+ price); 87 | String type = cursor.getString(typeIndex); 88 | Log.i(TAG, "获取到类型: "+ type); 89 | String image = cursor.getString(imageIndex); 90 | Log.i(TAG, "获取到图片: "+ image); 91 | // 设置ViewHolder对象的视图内容,根据商品的属性 92 | holder.productName.setText(name); 93 | Log.i(TAG, "设置为名字: "+ name); 94 | holder.productPrice.setText("¥" + price); 95 | holder.productType.setText(type); 96 | // 使用if语句,判断你的Context对象是否等于null,如果是,打印出错误信息或抛出异常 97 | //Log.e(TAG, "Context"+ context); 98 | // 使用getIdentifier方法,传入文件名,资源类型,和包名,返回图片的资源id 99 | int imageId = context.getResources().getIdentifier(image, "drawable", context.getPackageName()); 100 | // 使用setImageResource方法,传入图片的资源id,设置商品图片 101 | Log.i(TAG, "设置为图片: "+ imageId); 102 | holder.productImage.setImageResource(imageId); 103 | } 104 | 105 | @Override 106 | public int getItemCount() { 107 | return cursor.getCount(); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/database/CartDatabaseHelper.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy.database; 2 | 3 | import android.content.ContentValues; 4 | import android.content.Context; 5 | import android.database.Cursor; 6 | import android.database.sqlite.SQLiteDatabase; 7 | import android.database.sqlite.SQLiteOpenHelper; 8 | 9 | import androidx.annotation.Nullable; 10 | 11 | public class CartDatabaseHelper extends SQLiteOpenHelper { 12 | public static final String CREATE_CART = "Create table Cart(" 13 | + "id integer primary key autoincrement," 14 | + "name text," // 商品名 15 | + "price real," // 价格 16 | + "type text," // 种类 17 | + "image text," // 图片 18 | + "count real," // 商品个数 19 | + "username text)"; // 用户名 20 | public CartDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { 21 | super(context, name, factory, version); 22 | } 23 | 24 | @Override 25 | public void onCreate(SQLiteDatabase sqLiteDatabase) { 26 | sqLiteDatabase.execSQL(CREATE_CART); 27 | } 28 | 29 | @Override 30 | public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 31 | 32 | } 33 | public void insertCart(String name, double price, String type, String image, int count, String username) { 34 | // 获取一个可写的数据库对象 35 | SQLiteDatabase db = this.getWritableDatabase(); 36 | // 创建一个ContentValues对象 37 | ContentValues values = new ContentValues(); 38 | // 将值放入ContentValues对象中 39 | values.put("name", name); 40 | values.put("price", price); 41 | values.put("type", type); 42 | values.put("image", image); 43 | values.put("count", count); 44 | values.put("username", username); 45 | // 将值插入到Cart表中 46 | long cartId = db.insert("Cart", null, values); // 返回购物车id 47 | // 关闭数据库对象 48 | db.close(); 49 | } 50 | public boolean queryCart(String name, String username) { 51 | // 获取一个可读的数据库对象 52 | SQLiteDatabase db = this.getReadableDatabase(); 53 | // 查询Cart表中,name和username都匹配的记录 54 | Cursor cursor = db.query("Cart", null, "name = ? and username = ?", new String[]{name, username}, null, null, null); 55 | // 判断Cursor对象是否为空,以及是否有数据 56 | if (cursor != null && cursor.getCount() > 0) { 57 | // 如果不为空,且有数据,说明Cart表中存在name和username都匹配的记录,返回true 58 | cursor.close(); 59 | db.close(); 60 | return true; 61 | } else { 62 | // 如果为空,或者没有数据,说明Cart表中不存在name和username都匹配的记录,返回false 63 | cursor.close(); 64 | db.close(); 65 | return false; 66 | } 67 | } 68 | public int updateCart(String name, String username, int count) { 69 | // 获取一个可写的数据库对象 70 | SQLiteDatabase db = this.getWritableDatabase(); 71 | // 创建一个ContentValues对象 72 | ContentValues values = new ContentValues(); 73 | // 将count参数的值放入ContentValues对象中 74 | values.put("count", count); 75 | // 定义一个变量,用于存储返回的行数 76 | int rows = 0; 77 | // 判断count是否等于0 78 | if (count == 0) { 79 | // 如果等于0,就删除对应的表项 80 | rows = db.delete("Cart", "name = ? and username = ?", new String[]{name, username}); 81 | } else { 82 | // 如果不等于0,就更新对应的表项 83 | rows = db.update("Cart", values, "name = ? and username = ?", new String[]{name, username}); 84 | } 85 | // 关闭数据库对象 86 | db.close(); 87 | // 返回更新的行数 88 | return rows; 89 | } 90 | 91 | public void deleteCart(String name, String username) { 92 | // 获取一个可写的数据库对象 93 | SQLiteDatabase db = this.getWritableDatabase(); 94 | // 删除Cart表中,name和username都匹配的记录 95 | int rows = db.delete("Cart", "name = ? and username = ?", new String[]{name, username}); 96 | // 关闭数据库对象 97 | db.close(); 98 | } 99 | //定义一个方法,根据用户名删除Cart表中的数据 100 | public void deleteCart(String username) { 101 | // 获取一个可写的数据库对象 102 | SQLiteDatabase db = this.getWritableDatabase(); 103 | // 删除Cart表中,username匹配的所有记录 104 | int rows = db.delete("Cart", "username = ?", new String[]{username}); 105 | // 关闭数据库对象 106 | db.close(); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/database/ProductDatabaseHelper.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy.database; 2 | 3 | import android.content.ContentValues; 4 | import android.content.Context; 5 | import android.database.sqlite.SQLiteDatabase; 6 | import android.database.sqlite.SQLiteOpenHelper; 7 | import android.widget.Toast; 8 | 9 | public class ProductDatabaseHelper extends SQLiteOpenHelper { 10 | public static final String CREATE_PRODUCT = "Create table Product(" 11 | + "id integer primary key autoincrement," 12 | + "name text," 13 | + "price real," 14 | + "type text," 15 | + "image text)"; 16 | private Context mContext; 17 | public ProductDatabaseHelper(Context context,String name, SQLiteDatabase.CursorFactory factory, 18 | int version){ 19 | super(context,name, factory,version); 20 | mContext = context; 21 | } 22 | @Override 23 | public void onCreate(SQLiteDatabase sqLiteDatabase) { 24 | sqLiteDatabase.execSQL(CREATE_PRODUCT); 25 | ContentValues values = new ContentValues(); 26 | // 插入第一个商品 27 | values.put("name","小米14"); 28 | values.put("price", 5000); 29 | values.put("type","数码"); 30 | values.put("image", "image_1"); 31 | sqLiteDatabase.insert("Product",null, values); 32 | values.clear(); 33 | // 插入第二个商品 34 | values.put("name","华为P40"); 35 | values.put("price", 6000); 36 | values.put("type","数码"); 37 | values.put("image", "image_2"); 38 | sqLiteDatabase.insert("Product",null, values); 39 | values.clear(); 40 | // 插入第三个商品 41 | values.put("name","苹果12"); 42 | values.put("price", 8000); 43 | values.put("type","数码"); 44 | values.put("image", "image_3"); 45 | sqLiteDatabase.insert("Product",null, values); 46 | values.clear(); 47 | // 插入第四个商品 48 | values.put("name","联想笔记本"); 49 | values.put("price", 4000); 50 | values.put("type","电脑"); 51 | values.put("image", "image_4"); 52 | sqLiteDatabase.insert("Product",null, values); 53 | values.clear(); 54 | // 插入第五个商品 55 | values.put("name","戴尔台式机"); 56 | values.put("price", 3000); 57 | values.put("type","电脑"); 58 | values.put("image", "image_5"); 59 | sqLiteDatabase.insert("Product",null, values); 60 | values.clear(); 61 | // 插入第六个商品 62 | values.put("name","海尔冰箱"); 63 | values.put("price", 2000); 64 | values.put("type","家电"); 65 | values.put("image", "image_6"); 66 | sqLiteDatabase.insert("Product",null, values); 67 | values.clear(); 68 | // 插入第七个商品 69 | values.put("name","美的空调"); 70 | values.put("price", 2500); 71 | values.put("type","家电"); 72 | values.put("image", "image_7"); 73 | sqLiteDatabase.insert("Product",null, values); 74 | values.clear(); 75 | // 插入第八个商品 76 | values.put("name","李宁运动鞋"); 77 | values.put("price", 500); 78 | values.put("type","服饰"); 79 | values.put("image", "image_8"); 80 | sqLiteDatabase.insert("Product",null, values); 81 | values.clear(); 82 | // 插入第九个商品 83 | values.put("name","阿迪达斯T恤"); 84 | values.put("price", 300); 85 | values.put("type","服饰"); 86 | values.put("image", "image_9"); 87 | sqLiteDatabase.insert("Product",null, values); 88 | values.clear(); 89 | // 插入第十个商品 90 | values.put("name","百雀羚护肤套装"); 91 | values.put("price", 1000); 92 | values.put("type","美妆"); 93 | values.put("image", "image_10"); 94 | sqLiteDatabase.insert("Product",null, values); 95 | values.clear(); 96 | // 插入第十一个商品 97 | values.put("name","飞利浦剃须刀"); 98 | values.put("price", 800); 99 | values.put("type","美妆"); 100 | values.put("image", "image_11"); 101 | sqLiteDatabase.insert("Product",null, values); 102 | values.clear(); 103 | // 插入第十二个商品 104 | values.put("name","韩束面膜"); 105 | values.put("price", 200); 106 | values.put("type","美妆"); 107 | values.put("image", "image_12"); 108 | sqLiteDatabase.insert("Product",null, values); 109 | values.clear(); 110 | // 插入第十三个商品 111 | values.put("name","耐克运动裤"); 112 | values.put("price", 400); 113 | values.put("type","服饰"); 114 | values.put("image", "image_13"); 115 | sqLiteDatabase.insert("Product",null, values); 116 | values.clear(); 117 | // 插入第十四个商品 118 | values.put("name","杰克琼斯牛仔裤"); 119 | values.put("price", 600); 120 | values.put("type","服饰"); 121 | values.put("image", "image_14"); 122 | sqLiteDatabase.insert("Product",null, values); 123 | values.clear(); 124 | // 插入第十五个商品 125 | values.put("name","格力空气净化器"); 126 | values.put("price", 1500); 127 | values.put("type","家电"); 128 | values.put("image", "image_15"); 129 | sqLiteDatabase.insert("Product",null, values); 130 | values.clear(); 131 | // 插入第十六个商品 132 | values.put("name","美的微波炉"); 133 | values.put("price", 1000); 134 | values.put("type","家电"); 135 | values.put("image", "image_16"); 136 | sqLiteDatabase.insert("Product",null, values); 137 | values.clear(); 138 | // 插入第十七个商品 139 | values.put("name","惠普打印机"); 140 | values.put("price", 500); 141 | values.put("type","电脑"); 142 | values.put("image", "image_17"); 143 | sqLiteDatabase.insert("Product",null, values); 144 | values.clear(); 145 | // 插入第十八个商品 146 | values.put("name","华硕显示器"); 147 | values.put("price", 800); 148 | values.put("type","电脑"); 149 | values.put("image", "image_18"); 150 | sqLiteDatabase.insert("Product",null, values); 151 | values.clear(); 152 | // 插入第十九个商品 153 | values.put("name","三星S21"); 154 | values.put("price", 7000); 155 | values.put("type","数码"); 156 | values.put("image", "image_19"); 157 | sqLiteDatabase.insert("Product",null, values); 158 | values.clear(); 159 | // 插入第二十个商品 160 | values.put("name","索尼相机"); 161 | values.put("price", 10000); 162 | values.put("type","数码"); 163 | values.put("image", "image_20"); 164 | sqLiteDatabase.insert("Product",null, values); 165 | values.clear(); 166 | Toast.makeText(mContext, "Create product table succeeded",Toast.LENGTH_LONG).show(); 167 | } 168 | 169 | @Override 170 | public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 171 | sqLiteDatabase.execSQL("drop table if exists Product"); 172 | onCreate(sqLiteDatabase); 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/database/UserDatabaseHelper.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy.database; 2 | 3 | import android.content.ContentValues; 4 | import android.content.Context; 5 | import android.database.sqlite.SQLiteDatabase; 6 | import android.database.sqlite.SQLiteOpenHelper; 7 | 8 | import androidx.annotation.Nullable; 9 | 10 | public class UserDatabaseHelper extends SQLiteOpenHelper { 11 | public static final String CREATE_USER = "create table users(" 12 | + "id integer primary key autoincrement," 13 | + "usname text," 14 | + "uspwd text)"; 15 | public UserDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { 16 | super(context, name, factory, version); 17 | } 18 | 19 | @Override 20 | public void onCreate(SQLiteDatabase db) { 21 | db.execSQL(CREATE_USER); 22 | } 23 | 24 | @Override 25 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 26 | db.execSQL("drop table if exists users"); 27 | onCreate(db); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/fragment/BaseFragment.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy.fragment; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | import androidx.annotation.Nullable; 10 | import androidx.fragment.app.Fragment; 11 | 12 | public abstract class BaseFragment extends Fragment { 13 | protected Context mContext; 14 | @Override 15 | public void onCreate(@Nullable Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | mContext = getActivity(); 18 | } 19 | @Nullable 20 | @Override 21 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 22 | return initView(); 23 | } 24 | 25 | public abstract View initView() ; 26 | 27 | @Override 28 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 29 | super.onActivityCreated(savedInstanceState); 30 | initData(); 31 | } 32 | 33 | public void initData() { 34 | } 35 | 36 | public abstract void refreshData(); 37 | 38 | public abstract void saveData(); 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/fragment/CartFragment.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy.fragment; 2 | 3 | import static android.content.Context.MODE_PRIVATE; 4 | 5 | import android.content.SharedPreferences; 6 | import android.database.Cursor; 7 | import android.database.sqlite.SQLiteDatabase; 8 | import android.location.GnssAntennaInfo; 9 | import android.util.Log; 10 | import android.view.View; 11 | import android.widget.Button; 12 | import android.widget.CheckBox; 13 | import android.widget.TextView; 14 | import android.widget.Toast; 15 | 16 | import androidx.fragment.app.Fragment; 17 | import androidx.recyclerview.widget.GridLayoutManager; 18 | import androidx.recyclerview.widget.RecyclerView; 19 | 20 | import edu.bupt.shopeasy.R; 21 | import edu.bupt.shopeasy.adapter.CartAdapter; 22 | import edu.bupt.shopeasy.adapter.HomeAdapter; 23 | import edu.bupt.shopeasy.database.CartDatabaseHelper; 24 | import edu.bupt.shopeasy.database.ProductDatabaseHelper; 25 | 26 | public class CartFragment extends BaseFragment { 27 | private final String TAG = "CartFragment"; 28 | private RecyclerView recyclerView; // 购物车商品列表 29 | private CheckBox checkAll; // 全选 30 | private CheckBox deleteAll; // 清空 31 | private TextView total; // 总价 32 | private Button settle; //结算按键 33 | CartDatabaseHelper cartDatabaseHelper; 34 | SQLiteDatabase db; 35 | Cursor cursor; 36 | 37 | CartAdapter adapter; 38 | SharedPreferences sp; 39 | String username; 40 | @Override 41 | public View initView() { 42 | Log.i(TAG, "正在初始化购物车"); 43 | View view = View.inflate(mContext, R.layout.fragment_cart, null); 44 | recyclerView = view.findViewById(R.id.recyclerView); 45 | checkAll = view.findViewById(R.id.check_all); 46 | deleteAll = view.findViewById(R.id.delete_all); 47 | total = view.findViewById(R.id.total); 48 | settle = view.findViewById(R.id.settle); 49 | return view; 50 | } 51 | 52 | public void initData(){ 53 | super.initData(); 54 | sp = mContext.getSharedPreferences("username", MODE_PRIVATE); 55 | username = sp.getString("Loginname", ""); 56 | Log.i(TAG, "加载商品列表,用户为:" +username); 57 | cartDatabaseHelper = new CartDatabaseHelper(getActivity(), "Cart", null, 1); 58 | db = cartDatabaseHelper.getReadableDatabase(); 59 | Log.i(TAG, "获取到数据库"+db); 60 | cursor = db.query("Cart", null, "username = ?", new String[]{username}, null, null, null); 61 | Log.i(TAG, "查询到数据" + cursor); 62 | adapter = new CartAdapter(mContext, cursor); 63 | Log.i(TAG, "新建adapter成功" + adapter); 64 | recyclerView.setAdapter(adapter); 65 | Log.i(TAG, "设置adapter成功"); 66 | GridLayoutManager manager = new GridLayoutManager(mContext, 1); 67 | Log.i(TAG, "设置为1列"); 68 | //循环视图加载网格布局 69 | recyclerView.setLayoutManager(manager); 70 | 71 | Listener(); 72 | } 73 | public void Listener() { 74 | 75 | checkAll.setOnClickListener(new View.OnClickListener() { 76 | @Override 77 | public void onClick(View view) { 78 | refreshData(); 79 | total.setText("¥ "+String.valueOf(adapter.getTotalPrice())); 80 | settleListener(); 81 | } 82 | }); 83 | deleteAll.setOnClickListener(new View.OnClickListener() { 84 | @Override 85 | public void onClick(View view) { 86 | cartDatabaseHelper.deleteCart(username); 87 | refreshData(); 88 | } 89 | }); 90 | } 91 | public void settleListener() { 92 | // 给settle属性添加一个事件监听器 93 | settle.setOnClickListener(new View.OnClickListener() { 94 | @Override 95 | public void onClick(View v) { 96 | double total_price = adapter.getTotalPrice(); 97 | cartDatabaseHelper.deleteCart(username); 98 | Toast.makeText(mContext, "您的总花费为"+ total_price + "元", Toast.LENGTH_SHORT).show(); 99 | total.setText("¥ 0.0"); 100 | refreshData(); 101 | } 102 | }); 103 | } 104 | @Override 105 | public void refreshData() { 106 | sp = mContext.getSharedPreferences("username", MODE_PRIVATE); 107 | username = sp.getString("Loginname", ""); 108 | Log.i(TAG, "加载商品列表,用户为:" +username); 109 | cartDatabaseHelper = new CartDatabaseHelper(getActivity(), "Cart", null, 1); 110 | db = cartDatabaseHelper.getReadableDatabase(); 111 | Log.i(TAG, "获取到数据库"+db); 112 | cursor = db.query("Cart", null, "username = ?", new String[]{username}, null, null, null); 113 | Log.i(TAG, "查询到数据" + cursor); 114 | adapter = new CartAdapter(mContext, cursor); 115 | Log.i(TAG, "新建adapter成功" + adapter); 116 | recyclerView.setAdapter(adapter); 117 | Log.i(TAG, "设置adapter成功"); 118 | GridLayoutManager manager = new GridLayoutManager(mContext, 1); 119 | Log.i(TAG, "设置为1列"); 120 | //循环视图加载网格布局 121 | recyclerView.setLayoutManager(manager); 122 | } 123 | 124 | @Override 125 | public void saveData() { 126 | 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/fragment/HomeFragment.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy.fragment; 2 | 3 | import android.database.Cursor; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.util.Log; 6 | import android.view.View; 7 | import android.widget.Button; 8 | import android.widget.SearchView; 9 | import androidx.recyclerview.widget.GridLayoutManager; 10 | import androidx.recyclerview.widget.RecyclerView; 11 | import edu.bupt.shopeasy.R; 12 | import edu.bupt.shopeasy.adapter.HomeAdapter; 13 | import edu.bupt.shopeasy.database.ProductDatabaseHelper; 14 | 15 | public class HomeFragment extends BaseFragment { 16 | private static final String TAG = "HomeFragment"; 17 | SearchView searchView; 18 | Button searchButton; 19 | RecyclerView recyclerView; 20 | ProductDatabaseHelper productDatabaseHelper; 21 | SQLiteDatabase db; 22 | Cursor cursor; 23 | private HomeAdapter adapter; // HomeAdapter的实例 24 | 25 | 26 | @Override 27 | public View initView() { 28 | View view = View.inflate(mContext, R.layout.fragment_home, null); 29 | searchView =view.findViewById(R.id.searchView); // 搜索框 30 | searchButton = view.findViewById(R.id.searchButton); // 搜索按键 31 | recyclerView = view.findViewById(R.id.recyclerView); // 商品列表 32 | return view; 33 | } 34 | 35 | public void initData() { 36 | super.initData(); 37 | Log.i(TAG, "加载商品列表"); 38 | productDatabaseHelper = new ProductDatabaseHelper(getActivity(), "Product", null, 1); 39 | db = productDatabaseHelper.getReadableDatabase(); 40 | Log.i(TAG, "获取到数据库"+db); 41 | cursor = db.query("Product", null, null, null, null, null, null); 42 | Log.i(TAG, "查询到数据" + cursor); 43 | adapter = new HomeAdapter(mContext, cursor); 44 | Log.i(TAG, "新建adapter成功" + adapter); 45 | recyclerView.setAdapter(adapter); 46 | Log.i(TAG, "设置adapter成功"); 47 | GridLayoutManager manager = new GridLayoutManager(mContext, 1); 48 | Log.i(TAG, "设置为1列"); 49 | //循环视图加载网格布局 50 | recyclerView.setLayoutManager(manager); 51 | 52 | Listener(); 53 | } 54 | public void Listener() { 55 | searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 56 | @Override 57 | public boolean onQueryTextSubmit(String query) { 58 | //执行搜索操作,query为用户输入的文本 59 | db = productDatabaseHelper.getReadableDatabase(); 60 | Cursor cursor = db.rawQuery("SELECT * FROM Product WHERE name LIKE '%" + query + "%'", null); 61 | adapter = new HomeAdapter(mContext, cursor); 62 | recyclerView.setAdapter(adapter); 63 | GridLayoutManager manager = new GridLayoutManager(mContext, 1); 64 | //循环视图加载网格布局 65 | recyclerView.setLayoutManager(manager); 66 | //显示搜索结果 67 | return true; 68 | } 69 | 70 | @Override 71 | public boolean onQueryTextChange(String newText) { 72 | //用户输入文本变化时的操作 73 | return false; 74 | } 75 | }); 76 | 77 | searchButton.setOnClickListener(new View.OnClickListener() { 78 | @Override 79 | public void onClick(View v) { 80 | //调用点击事件处理方法 81 | onClickSearch(v); 82 | } 83 | }); 84 | } 85 | public void onClickSearch(View view) { 86 | //获取搜索框中的文本 87 | String query = searchView.getQuery().toString(); 88 | //执行搜索操作,query为用户输入的文本 89 | db = productDatabaseHelper.getReadableDatabase(); 90 | Cursor cursor = db.rawQuery("SELECT * FROM Product WHERE name LIKE '%" + query + "%'", null); 91 | adapter = new HomeAdapter(mContext, cursor); 92 | recyclerView.setAdapter(adapter); 93 | GridLayoutManager manager = new GridLayoutManager(mContext, 1); 94 | //循环视图加载网格布局 95 | recyclerView.setLayoutManager(manager); 96 | //显示搜索结果 97 | } 98 | @Override 99 | public void refreshData() { 100 | Log.i(TAG, "加载商品列表"); 101 | productDatabaseHelper = new ProductDatabaseHelper(getActivity(), "Product", null, 1); 102 | db = productDatabaseHelper.getReadableDatabase(); 103 | Log.i(TAG, "获取到数据库"+db); 104 | cursor = db.query("Product", null, null, null, null, null, null); 105 | Log.i(TAG, "查询到数据" + cursor); 106 | adapter = new HomeAdapter(mContext, cursor); 107 | Log.i(TAG, "新建adapter成功" + adapter); 108 | recyclerView.setAdapter(adapter); 109 | Log.i(TAG, "设置adapter成功"); 110 | GridLayoutManager manager = new GridLayoutManager(mContext, 1); 111 | Log.i(TAG, "设置为1列"); 112 | //循环视图加载网格布局 113 | recyclerView.setLayoutManager(manager); 114 | } 115 | 116 | @Override 117 | public void saveData() { 118 | 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/fragment/TypeFragment.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy.fragment; 2 | 3 | import android.database.Cursor; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.graphics.Color; 6 | import android.util.Log; 7 | import android.view.View; 8 | 9 | import androidx.fragment.app.Fragment; 10 | import androidx.recyclerview.widget.GridLayoutManager; 11 | import androidx.recyclerview.widget.RecyclerView; 12 | 13 | import edu.bupt.shopeasy.R; 14 | import edu.bupt.shopeasy.adapter.HomeAdapter; 15 | import edu.bupt.shopeasy.adapter.TypeAdapter; 16 | import edu.bupt.shopeasy.database.ProductDatabaseHelper; 17 | 18 | import android.widget.Button; 19 | 20 | public class TypeFragment extends BaseFragment { 21 | private static final String TAG = "TypeFragment"; 22 | 23 | Button allButton; // 所有商品按键 24 | Button digitalButton; // 数码商品按键 25 | Button computerButton; // 电脑商品按键 26 | Button homeButton; // 家电商品按键 27 | Button clothingButton; // 服饰商品按键 28 | Button beautyButton; // 美妆商品按键 29 | RecyclerView recyclerView; // 商品列表 30 | 31 | ProductDatabaseHelper productDatabaseHelper; 32 | SQLiteDatabase db; 33 | Cursor cursor; 34 | private TypeAdapter adapter; 35 | // 定义一个数组,存放所有的 Button 36 | private Button[] buttons; 37 | // 定义一个变量,表示当前选择的商品类别,0 表示全部,1 表示数码,2 表示电脑,依次类推 38 | private int category = 0; 39 | 40 | @Override 41 | public View initView() { 42 | View view = View.inflate(mContext, R.layout.fragment_type, null); 43 | allButton = view.findViewById(R.id.all_button); 44 | digitalButton = view.findViewById(R.id.digital_button); 45 | computerButton = view.findViewById(R.id.computer_button); 46 | homeButton = view.findViewById(R.id.home_button); 47 | clothingButton = view.findViewById(R.id.clothing_button); 48 | beautyButton = view.findViewById(R.id.beauty_button); 49 | recyclerView = view.findViewById(R.id.recyclerView); 50 | // 初始化数组 51 | buttons = new Button[]{allButton, digitalButton, computerButton, homeButton, clothingButton, beautyButton}; 52 | 53 | return view; 54 | } 55 | public void initData() { 56 | super.initData(); 57 | buttons[0].setBackgroundColor(Color.parseColor("#6750A4")); 58 | buttons[0].setTextColor(Color.parseColor("#FFFFFF")); 59 | Log.i(TAG, "加载商品列表"); 60 | productDatabaseHelper = new ProductDatabaseHelper(getActivity(), "Product", null, 1); 61 | db = productDatabaseHelper.getReadableDatabase(); 62 | Log.i(TAG, "获取到数据库"+db); 63 | cursor = db.query("Product", null, null, null, null, null, null); 64 | Log.i(TAG, "查询到数据" + cursor); 65 | adapter = new TypeAdapter(mContext, cursor); 66 | Log.i(TAG, "新建adapter成功" + adapter); 67 | recyclerView.setAdapter(adapter); 68 | Log.i(TAG, "设置adapter成功"); 69 | GridLayoutManager manager = new GridLayoutManager(mContext, 2); 70 | Log.i(TAG, "设置为2列"); 71 | //循环视图加载网格布局 72 | recyclerView.setLayoutManager(manager); 73 | 74 | Listener(); 75 | } 76 | 77 | public void Listener() { 78 | Log.i(TAG, "buttons "+ buttons); 79 | Log.i(TAG, "button3 "+ buttons[3]); 80 | // 为每个 Button 添加点击事件监听器 81 | for (int i = 0; i < buttons.length; i++) { 82 | // 获取当前的 Button 83 | Button button = buttons[i]; 84 | // 设置一个标签,表示 Button 的序号 85 | Log.i(TAG, "button"+ i + " "+buttons[i]); 86 | button.setTag(i); 87 | // 设置点击事件 88 | button.setOnClickListener(new View.OnClickListener() { 89 | @Override 90 | public void onClick(View v) { 91 | // 获取当前点击的 Button 的 id 92 | int id = v.getId(); 93 | // 根据 id,设置 category 的值 94 | if (id == R.id.all_button) { 95 | category = 0; 96 | } else if (id == R.id.digital_button) { 97 | category = 1; 98 | } else if (id == R.id.computer_button) { 99 | category = 2; 100 | } else if (id == R.id.home_button) { 101 | category = 3; 102 | } else if (id == R.id.clothing_button) { 103 | category = 4; 104 | } else if (id == R.id.beauty_button) { 105 | category = 5; 106 | } 107 | // 遍历所有的 Button,根据 category 的值,设置不同的背景颜色和文字颜色 108 | for (int j = 0; j < buttons.length; j++) { 109 | // 获取当前的 Button 110 | Button b = buttons[j]; 111 | // 如果当前的 Button 的标签和 category 相同,表示选中,设置特殊的颜色 112 | if (j == category) { 113 | b.setBackgroundColor(Color.parseColor("#6750A4")); 114 | b.setTextColor(Color.parseColor("#FFFFFF")); 115 | } else { 116 | // 否则,表示未选中,恢复默认的颜色 117 | b.setBackgroundColor(Color.parseColor("#FFFFFF")); 118 | b.setTextColor(Color.parseColor("#000000")); 119 | } 120 | } 121 | // 根据 category 的值,重新查询数据库,获取对应类别的商品数据 122 | if (category == 0) { 123 | // 如果 category 为 0,表示查询所有商品 124 | cursor = db.query("Product", null, null, null, null, null, null); 125 | } else { 126 | // 否则,使用 selection 和 selectionArgs 参数,指定查询条件,比如 category = ? 127 | //获取搜索框中的文本 128 | Button b = buttons[category]; 129 | String query = b.getText().toString(); 130 | //执行搜索操作,query为用户输入的文本 131 | db = productDatabaseHelper.getReadableDatabase(); 132 | Log.i(TAG, "查询 "+ query); 133 | cursor = db.rawQuery("SELECT * FROM Product WHERE type LIKE '%" + query + "%'", null); 134 | } 135 | // 将查询结果的 Cursor 传递给 adapter,更新 RecyclerView 的数据源 136 | adapter = new TypeAdapter(mContext, cursor); 137 | recyclerView.setAdapter(adapter); 138 | GridLayoutManager manager = new GridLayoutManager(mContext, 2); 139 | //循环视图加载网格布局 140 | recyclerView.setLayoutManager(manager); 141 | } 142 | }); 143 | } 144 | } 145 | @Override 146 | public void refreshData() { 147 | } 148 | 149 | @Override 150 | public void saveData() { 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /app/src/main/java/edu/bupt/shopeasy/fragment/UserFragment.java: -------------------------------------------------------------------------------- 1 | package edu.bupt.shopeasy.fragment; 2 | 3 | import static android.content.Context.MODE_PRIVATE; 4 | import android.content.Intent; 5 | import android.content.SharedPreferences; 6 | import android.util.Log; 7 | import android.view.View; 8 | import android.widget.Button; 9 | import android.widget.TextView; 10 | import edu.bupt.shopeasy.LoginActivity; 11 | import edu.bupt.shopeasy.R; 12 | import edu.bupt.shopeasy.RegisterActivity; 13 | 14 | public class UserFragment extends BaseFragment { 15 | private final String TAG = "UserFragment"; 16 | private static final int LOGIN_REQUEST_CODE = 1; 17 | 18 | Button login_button; 19 | Button register_button; 20 | TextView show_hello; 21 | SharedPreferences sp; 22 | 23 | @Override 24 | public View initView() { 25 | View view = View.inflate(mContext, R.layout.fragment_user, null); 26 | login_button = view.findViewById(R.id.login_button); 27 | register_button = view.findViewById(R.id.register_button); 28 | sp = getActivity().getSharedPreferences("username", MODE_PRIVATE); //获取sharepreferences 29 | show_hello = view.findViewById(R.id.mainword); 30 | show_hello.setText("欢迎你!"+sp.getString("Loginname","")); //获取用户名 31 | return view; 32 | } 33 | 34 | private void Listener() { 35 | login_button.setOnClickListener(new View.OnClickListener() { 36 | @Override 37 | public void onClick(View view) { 38 | Log.i(TAG, "用户登录事件"); 39 | Intent intent = new Intent(mContext, LoginActivity.class); 40 | startActivity(intent); 41 | } 42 | }); 43 | 44 | register_button.setOnClickListener(new View.OnClickListener() { 45 | @Override 46 | public void onClick(View view) { 47 | Log.i(TAG, "用户注册事件"); 48 | Intent intent = new Intent(mContext, RegisterActivity.class); 49 | startActivity(intent); 50 | } 51 | }); 52 | } 53 | 54 | public void initData() { 55 | super.initData(); 56 | Log.i(TAG, "用户数据初始化"); 57 | 58 | Listener(); 59 | 60 | } 61 | 62 | @Override 63 | public void refreshData() { 64 | 65 | } 66 | 67 | @Override 68 | public void saveData() { 69 | 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/res/color/icon_color_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/color/text_color_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_1.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_10.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_10.webp -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_11.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_12.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_13.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_14.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_14.webp -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_15.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_16.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_17.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_18.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_18.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_19.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_19.jpeg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_2.jpeg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_20.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_20.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_3.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_4.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_5.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_6.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_7.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_8.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/image_9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tingwuren/AndroidFinalHomework/c082e9054f18c2aa585933e4bdc4975d88277625/app/src/main/res/drawable/image_9.jpg -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_detail.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 13 | 14 | 22 | 23 | 31 | 32 | 39 | 40 |