android 将数据存入SQLiteOpenHelper 用ListView展示出来 图片使用ViewPager做出滑块效果

mac2024-05-25  30

主页面

public class AddressActivity extends AppCompatActivity { private TextView mAddressAdd; private ListView mAddressLv; private AddressDB addressDB; private SQLiteDatabase writableDatabase; private Cursor cursor; private ArrayList<AddressBean> mList = new ArrayList<>(); private String name; private String phone; private MyAdaper myAdaper; private int img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_address); initView(); addressDB = new AddressDB(getApplicationContext()); getData(); } private void getData() { writableDatabase = addressDB.getWritableDatabase(); cursor = writableDatabase.query("address", null, null, null, null, null, null); int count = cursor.getCount(); if (count == 0) { Toast.makeText(getApplicationContext(), "暂无联系人", Toast.LENGTH_LONG).show(); return; } if (cursor.moveToFirst()) { do { img = cursor.getInt(cursor.getColumnIndex("img")); name = cursor.getString(cursor.getColumnIndex("name")); phone = cursor.getString(cursor.getColumnIndex("phone")); mList.add(new AddressBean(img,name,phone)); } while (cursor.moveToNext()); } changer(); } private void changer() { if (myAdaper == null) { myAdaper = new MyAdaper(); mAddressLv.setAdapter(myAdaper); } else { myAdaper.notifyDataSetChanged(); } } private void initView() { mAddressAdd = (TextView) findViewById(R.id.address_add); mAddressLv = (ListView) findViewById(R.id.address_lv); mAddressAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(AddressActivity.this, AddressAddActivity.class); startActivity(intent); finish(); } }); } class MyAdaper extends BaseAdapter { @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { viewHolder = new ViewHolder(); convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_address, null); viewHolder.mItemAddressImg = (ImageView) convertView.findViewById(R.id.item_address_img); viewHolder.mItemAddressName = (TextView) convertView.findViewById(R.id.item_address_name); viewHolder.mItemAddressPhone = (TextView) convertView.findViewById(R.id.item_address_phone); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } AddressBean addressBean = mList.get(position); viewHolder.mItemAddressImg.setImageResource(addressBean.getImg()); viewHolder.mItemAddressName.setText(addressBean.getName()); viewHolder.mItemAddressPhone.setText(addressBean.getPhone()); return convertView; } class ViewHolder { private ImageView mItemAddressImg; private TextView mItemAddressName; private TextView mItemAddressPhone; } } }

布局

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Activity.AddressActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="15" android:gravity="center" android:text="通讯录" android:textColor="#fff" android:textSize="30sp" /> <TextView android:id="@+id/address_add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right" android:text="添加" android:textColor="#fff" android:textSize="30sp" /> </LinearLayout> <ListView android:id="@+id/address_lv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>

添加页面

public class AddressAddActivity extends AppCompatActivity { private TextView mAddressAddRe; private EditText mAddressAddName; private EditText mAddressAddPhone; private ImageView mAddressAddImg; private Button mAddressAddNone; private Button mAddressAddAdd; private AddressDB addressDB; private SQLiteDatabase writableDatabase; private ViewPager mAddressAddViewPager; private View view1, view2, view3; private List<View> viewList = new ArrayList<>(); private int img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_address_add); initView(); addressDB = new AddressDB(getApplicationContext()); getImg(); } private void getImg() { LayoutInflater inflater = getLayoutInflater(); view1 = inflater.inflate(R.layout.item_address_add01, null); view2 = inflater.inflate(R.layout.item_address_add02, null); view3 = inflater.inflate(R.layout.item_address_add03, null); viewList.add(view1); viewList.add(view2); viewList.add(view3); PagerAdapter pagerAdapter = new PagerAdapter() { //返回要滑动的VIew的个数 @Override public int getCount() { return viewList.size(); } //视图View和键对象 object是否一致 @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } //从当前container中删除指定位置(position)的View; @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView(viewList.get(position)); } //初始化item,做了两件事,第一:将当前视图添加到container中,第二:返回当前View作为键 @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { container.addView(viewList.get(position)); return viewList.get(position); } }; mAddressAddViewPager.setAdapter(pagerAdapter); mAddressAddViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case 0: { img = R.drawable.img2; Log.e("TAG", img + ""); break; } case 1: { img = R.drawable.img3; Log.e("TAG", img + ""); break; } case 2: { img = R.drawable.img4; Log.e("TAG", img + ""); break; } } } @Override public void onPageScrollStateChanged(int state) { } }); } private void initView() { mAddressAddRe = (TextView) findViewById(R.id.address_add_re); mAddressAddName = (EditText) findViewById(R.id.address_add_name); mAddressAddPhone = (EditText) findViewById(R.id.address_add_phone); mAddressAddViewPager = (ViewPager) findViewById(R.id.address_add_viewPager); mAddressAddNone = (Button) findViewById(R.id.address_add_none); mAddressAddAdd = (Button) findViewById(R.id.address_add_add); mAddressAddRe.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(AddressAddActivity.this, AddressActivity.class); startActivity(intent); finish(); } }); mAddressAddAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { add(); } }); } private void add() { String name = mAddressAddName.getText().toString().trim(); String phone = mAddressAddPhone.getText().toString().trim(); if (TextUtils.isEmpty(name)) { Toast.makeText(getApplicationContext(), "请输入姓名", Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(phone)) { Toast.makeText(getApplicationContext(), "请输入电话号码", Toast.LENGTH_SHORT).show(); return; } writableDatabase = addressDB.getWritableDatabase(); ContentValues contentValues = new ContentValues(); if (img == 0){ img = R.drawable.img2; } contentValues.put("img", img); contentValues.put("name", name); contentValues.put("phone", phone); writableDatabase.insert("address", null, contentValues); Intent intent = new Intent(AddressAddActivity.this, AddressActivity.class); startActivity(intent); finish(); } }

布局

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f1f1f1" android:gravity="center" android:orientation="vertical" tools:context=".Activity.AddressAddActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000" android:orientation="horizontal"> <TextView android:id="@+id/address_add_re" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="<返回" android:textColor="#fff" android:textSize="30sp" /> </LinearLayout> <LinearLayout android:layout_width="500dp" android:layout_height="match_parent" android:layout_marginTop="10dp" android:orientation="vertical"> <EditText android:id="@+id/address_add_name" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginBottom="20dp" android:background="#fff" android:hint="请输入姓名" android:lines="1" /> <EditText android:id="@+id/address_add_phone" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginBottom="20dp" android:background="#fff" android:hint="请输入电话号码" android:inputType="number" android:lines="1" /> <androidx.viewpager.widget.ViewPager android:id="@+id/address_add_viewPager" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:layout_marginBottom="20dp"></androidx.viewpager.widget.ViewPager> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/address_add_none" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重置" /> <Button android:id="@+id/address_add_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" /> </LinearLayout> </LinearLayout> </LinearLayout>

还需要三个子布局 这是第一个,其他两个将图片资源改掉就行

<?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:layout_gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/img1" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/img2"></ImageView> </LinearLayout>

SQLiteOpenHelper

public class AddressDB extends SQLiteOpenHelper { String sql = "create table address(id integer primary key autoincrement,img integer,name varchar(20),phone varchar(20))"; public AddressDB(@Nullable Context context) { super(context, "address.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }

Bean类

public class AddressBean { private int img; private String name; private String phone; public AddressBean(int img, String name, String phone) { this.img = img; this.name = name; this.phone = phone; } public int getImg() { return img; } public void setImg(int img) { this.img = img; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }

最新回复(0)