实现从数据库存取收货地址

master
Michael 3 years ago
parent f5f19096f6
commit 1f45613999

@ -9,6 +9,7 @@
<entry key="..\:/Android/Projects/android_-design/app/src/main/res/layout/fragment_my_page.xml" value="0.21875" />
<entry key="..\:/Android/Projects/android_-design/app/src/main/res/layout/item_delivery.xml" value="0.21875" />
<entry key="..\:/Android/Projects/android_-design/app/src/main/res/layout/item_delivery_select.xml" value="0.21875" />
<entry key="..\:/Android/Projects/android_-design/app/src/main/res/layout/item_stagger.xml" value="0.2536231884057971" />
<entry key="..\:/andriod/Android_Couser_Design/app/src/main/res/drawable/add_shopping_cart_24.xml" value="0.108" />
<entry key="..\:/andriod/Android_Couser_Design/app/src/main/res/drawable/baseline_account_circle_18.xml" value="0.1345" />
<entry key="..\:/andriod/Android_Couser_Design/app/src/main/res/drawable/baseline_account_circle_20.xml" value="0.1345" />

@ -23,10 +23,10 @@
android:theme="@style/Theme.Android_Couser_Design"
tools:targetApi="31">
<activity
android:name=".DetailDeliveryActivity"
android:name=".activity.DetailDeliveryActivity"
android:exported="false" />
<activity
android:name=".DeliverActivity"
android:name=".activity.DeliverActivity"
android:exported="false" />
<activity
android:name=".activity.ConfirmPurchaseActivity"

@ -1,25 +0,0 @@
package com.android;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class DeliverActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delivery);
Button new_addr = findViewById(R.id.btn_delivery_add);
new_addr.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DeliverActivity.this,
DetailDeliveryActivity.class);
startActivity(intent);
}
});
}
}

@ -1,14 +0,0 @@
package com.android;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class DetailDeliveryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delivery_detail);
}
}

@ -0,0 +1,80 @@
package com.android.SQL;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View;
import com.android.R;
import com.android.bean.DeliveryAdr;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
View view;
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + DeliveryAdr.DeliveryEntry.TABLE_NAME + " (" +
DeliveryAdr.DeliveryEntry._ID + " INTEGER PRIMARY KEY, " +
DeliveryAdr.DeliveryEntry.COLUMN_PEOPLE_NAME + " VARCHAR(200), " +
DeliveryAdr.DeliveryEntry.COLUMN_PEOPLE_TELE + " VARCHAR(200), " +
DeliveryAdr.DeliveryEntry.COLUMN_DETAIL_ADR + " VARCHAR(100), " +
DeliveryAdr.DeliveryEntry.COLUMN_IS_RAW + " VARCHAR(100) " +
")" ;
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + DeliveryAdr.DeliveryEntry.TABLE_NAME;
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "deliverys.db";
private Context mContext;
public MySQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase,
int oldVersion, int newVersion) {
sqLiteDatabase.execSQL(SQL_DELETE_ENTRIES);
onCreate(sqLiteDatabase);
}
// private void initDb(SQLiteDatabase sqLiteDatabase) {
//
// Resources resources = mContext.getResources();
// String[] titles = resources.getStringArray(R.array.titles);
// String[] authors = resources.getStringArray(R.array.authors);
// String[] contents = resources.getStringArray(R.array.contents);
// TypedArray images = resources.obtainTypedArray(R.array.images);
//
// int length = 0;
// length = Math.min(titles.length, authors.length);
// length = Math.min(length, contents.length);
// length = Math.min(length, images.length());
//
// for (int i = 0; i < length; i++) {
// ContentValues values = new ContentValues();
// values.put(DeliveryAdr.DeliveryEntry.COLUMN_PEOPLE_NAME ,
// titles[i]);
// values.put(DeliveryAdr.DeliveryEntry.COLUMN_PEOPLE_TELE,
// authors[i]);
// values.put(DeliveryAdr.DeliveryEntry.COLUMN_DETAIL_ADR ,
// contents[i]);
// values.put(DeliveryAdr.DeliveryEntry.COLUMN_IS_RAW ,
// images.getString(i));
//
// long r = sqLiteDatabase.insert(
// DeliveryAdr.DeliveryEntry.TABLE_NAME,
// null,
// values);
// }
// }
}

@ -0,0 +1,83 @@
package com.android.activity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.android.R;
import com.android.SQL.MySQLiteOpenHelper;
import com.android.activity.adapter.DeliverysAdapter;
import com.android.bean.DeliveryAdr;
import com.android.model.Delivery;
import java.util.ArrayList;
import java.util.List;
public class DeliverActivity extends AppCompatActivity {
MySQLiteOpenHelper myDbHelper;
SQLiteDatabase db;
private List<Delivery> deliList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delivery);
Button new_addr = findViewById(R.id.btn_delivery_add);
new_addr.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DeliverActivity.this,
DetailDeliveryActivity.class);
startActivity(intent);
}
});
//查询数据
myDbHelper = new MySQLiteOpenHelper(DeliverActivity.this);
db = myDbHelper.getReadableDatabase();
Cursor cursor = db.query(
DeliveryAdr.DeliveryEntry.TABLE_NAME,
null, null, null, null, null, null);
int nameIndex = cursor.getColumnIndex(
DeliveryAdr.DeliveryEntry.COLUMN_PEOPLE_NAME);
int teleIndex = cursor.getColumnIndex(
DeliveryAdr.DeliveryEntry.COLUMN_PEOPLE_TELE);
int adrIndex = cursor.getColumnIndex(
DeliveryAdr.DeliveryEntry.COLUMN_DETAIL_ADR);
int swIndex = cursor.getColumnIndex(
DeliveryAdr.DeliveryEntry.COLUMN_IS_RAW);
while (cursor.moveToNext()) {
Delivery delivers = new Delivery();
String name = cursor.getString(nameIndex);
String tel = cursor.getString(teleIndex);
String adr = cursor.getString(adrIndex);
String sw = cursor.getString(swIndex);
delivers.setReceiver(name);
delivers.setPhone(tel);
delivers.setLocation(adr);
deliList.add(delivers);
}
DeliverysAdapter deliAdapter = new DeliverysAdapter(
DeliverActivity.this,
R.layout.item_delivery,
deliList);
ListView lvdelisList = findViewById(R.id.rv_delivery);
lvdelisList.setAdapter(deliAdapter);
}
}

@ -0,0 +1,67 @@
package com.android.activity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.R;
import com.android.SQL.MySQLiteOpenHelper;
import com.android.bean.DeliveryAdr;
public class DetailDeliveryActivity extends AppCompatActivity {
MySQLiteOpenHelper myDbHelper;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delivery_detail);
myDbHelper = new MySQLiteOpenHelper(DetailDeliveryActivity.this);
db = myDbHelper.getReadableDatabase();
Toast.makeText(this, "创建数据库", Toast.LENGTH_SHORT).show();
EditText evtel = findViewById(R.id.et_delivery_phone);
EditText evname = findViewById(R.id.et_delivery_receiver);
EditText evloc = findViewById(R.id.et_delivery_location);
SwitchCompat scdef = findViewById(R.id.sc_delivery_default);
Button btsave = findViewById(R.id.btn_save);
btsave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(DeliveryAdr.DeliveryEntry.COLUMN_PEOPLE_NAME ,
evname.getText().toString());
values.put(DeliveryAdr.DeliveryEntry.COLUMN_PEOPLE_TELE,
evtel.getText().toString());
values.put(DeliveryAdr.DeliveryEntry.COLUMN_DETAIL_ADR ,
evloc.getText().toString());
if(scdef.isChecked())
values.put(DeliveryAdr.DeliveryEntry.COLUMN_IS_RAW ,
"true");
else
values.put(DeliveryAdr.DeliveryEntry.COLUMN_IS_RAW ,
"false");
long r = db.insert(
DeliveryAdr.DeliveryEntry.TABLE_NAME,
null,
values);
db.close();
Log.e("TAG", "保存数据");
}
});
}
}

@ -0,0 +1,47 @@
package com.android.activity.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.R;
import com.android.activity.DeliverActivity;
import com.android.model.Delivery;
import java.util.List;
public class DeliverysAdapter extends ArrayAdapter<Delivery> {
private List<Delivery> mNewsData;
private Context mContext;
private int resourceId;
public DeliverysAdapter(Context context, int resourceId, List<Delivery> data) {
super(context, resourceId, data);
this.mContext = context;
this.mNewsData = data;
this.resourceId = resourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Delivery deliverys = getItem(position);
View view ;
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
TextView tvreceiver = view.findViewById(R.id.tv_delivery_receiver);
TextView tvphone = view.findViewById(R.id.tv_delivery_phone);
TextView tvloc = view.findViewById(R.id.tv_delivery_location);
tvreceiver.setText(deliverys.getReceiver());
tvphone.setText(deliverys.getPhone());
tvloc.setText(deliverys.getLocation());
return view;
}
}

@ -12,8 +12,7 @@ import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.android.DeliverActivity;
import com.android.MainActivity;
import com.android.activity.DeliverActivity;
import com.android.R;
import com.android.activity.LoginActivity;
import com.android.bean.ResponseData;

@ -0,0 +1,16 @@
package com.android.bean;
import android.provider.BaseColumns;
public final class DeliveryAdr {
private DeliveryAdr() {}
public static class DeliveryEntry implements BaseColumns {
public static final String TABLE_NAME = "tbl_deli";
public static final String COLUMN_PEOPLE_NAME = "rece_people";
public static final String COLUMN_PEOPLE_TELE = "tele";
public static final String COLUMN_DETAIL_ADR = "adr";
public static final String COLUMN_IS_RAW = "false";
}
}

@ -1,8 +1,8 @@
package com.android.model;
/**
* Created by xmfy on 2018/1/16.
*/
import androidx.appcompat.widget.SwitchCompat;
public class Delivery {
private int id;
@ -13,12 +13,12 @@ public class Delivery {
private String city;
private String district;
private String location;
private Integer defaults;
private SwitchCompat defaults;
public Delivery() {
}
public Delivery(int id, String account, String receiver, String phone, String province, String city, String district, String location, Integer defaults) {
public Delivery(int id, String account, String receiver, String phone, String province, String city, String district, String location, SwitchCompat defaults) {
this.id = id;
this.account = account;
this.receiver = receiver;
@ -94,11 +94,11 @@ public class Delivery {
this.location = location;
}
public Integer getDefaults() {
public SwitchCompat getDefaults() {
return defaults;
}
public void setDefaults(Integer defaults) {
public void setDefaults(SwitchCompat defaults) {
this.defaults = defaults;
}

@ -11,13 +11,13 @@
app:title_visible="true"
android:background="#E6E6E6">
<androidx.recyclerview.widget.RecyclerView
<ListView
android:id="@+id/rv_delivery"
android:background="#E6E6E6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</androidx.recyclerview.widget.RecyclerView>
android:layout_weight="1"
/>
<Button
android:id="@+id/btn_delivery_add"

Loading…
Cancel
Save