第一步:在项目的app/build.gradle加上,连接上litepal库
dependencies {
implementation 'org.litepal.android:core:2.0.0'
}
第二步:新建一个MyApplication.java,初始化litepal.initialize
package com.example.administrator.WeChats;
import android.app.Application;
import org.litepal.LitePal;
public class MyApplication extends Application {
@Override
public void onCreate()
{
super.onCreate();
LitePal.initialize(this);
}
}
第三步:然后在androidManifest.xml,设置application:Android:name;为了正常使用litepal
第四步,新建Friend类
package com.example.administrator.WeChats.data;
import org.litepal.crud.LitePalSupport;
public class Friends extends LitePalSupport{
private int id;
private String name;
private int number;
private boolean sex;
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getNumber() {
return number;
}
public boolean isSex() {
return sex;
}
public void setId(int id){
this.id=id;
}
public void setName(String name) {
this.name = name;
}
public void setNumber(int number) {
this.number = number;
}
public void setSex(boolean sex) {
this.sex = sex;
}
}
第五步:新建一个DataAdapter
package com.example.administrator.WeChats;
import java.util.List;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DataArrayAdapter extends ArrayAdapter> {
public DataArrayAdapter(Context context, int textViewResourceId, List> objects) {
super(context, textViewResourceId, objects);
}
@Override
public @NonNull View getView(int position, View convertView,@NonNull ViewGroup parent) {
List dataList = getItem(position);
LinearLayout layout;
if (convertView == null) {
layout = new LinearLayout(getContext());
} else {
layout = (LinearLayout) convertView;
}
layout.removeAllViews();
int width = Utility.dp2px(getContext(), 100);
int height = Utility.dp2px(getContext(), 30);
for (String data : dataList) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
TextView textView = new TextView(getContext());
textView.setText(data);
textView.setSingleLine(true);
textView.setEllipsize(TruncateAt.END);
textView.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(textView, params);
}
return layout;
}
}
第六步:新建做数据添加的活动DataActivity
package com.example.administrator.WeChats.Activity;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.example.administrator.WeChats.DataArrayAdapter;
import com.example.administrator.WeChats.R;
import com.example.administrator.WeChats.data.Friends;
import org.litepal.LitePal;
import org.litepal.tablemanager.Connector;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DataActivity extends AppCompatActivity implements View.OnClickListener
{
public static void actionStart(Context context) {
Intent intent=new Intent(context,DataActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(intent);
}
public static int i=0;
private ListView mDataListView;
private DataArrayAdapter mAdapter;
private List> mList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data);
ActionBar actionBar=getSupportActionBar();
if(actionBar !=null)
actionBar.setDisplayHomeAsUpEnabled(true);
Button createDatabase = findViewById(R.id.createdb);
Button addData =findViewById(R.id.addData);
Button deleteData=findViewById(R.id.deleteData);
createDatabase.setOnClickListener(this);
addData.setOnClickListener(this);
deleteData.setOnClickListener(this);
mDataListView = findViewById(R.id.data_list_view);
mAdapter = new DataArrayAdapter(this, 0, mList);
mDataListView.setAdapter(mAdapter);
populateDataFromDB();
}
@Override
public void onClick(View v) {
Friends f = new Friends();
switch (v.getId()) {
case R.id.createdb:
{
LitePal.getDatabase();
}break;
case R.id.addData:
{
try {
i++;
f.setId(1995);
f.setName("old friends: "+i);
f.setNumber(i);
f.setSex(true);
f.save();
refreshListView(f.getId(), f.getName(),f.getNumber(), f.isSex());
}catch (Exception e){
e.printStackTrace();
Toast.makeText(this,"addData error",Toast.LENGTH_SHORT).show();
}
}break;
case R.id.deleteData:
try {
LitePal.delete(Friends.class,i--);
refreshListView(f.getId(), f.getName(),f.getNumber(), f.isSex());
} catch (Exception e) {
e.printStackTrace();
}break;
default :break;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
{
finish();
}break;
default :break;
}
return super.onOptionsItemSelected(item);
}
//----------填充来自数据库中的数据---------------------
private void populateDataFromDB() {
new Thread(new Runnable() {
@Override
public void run() {
mList.clear();
List columnList = new ArrayList();
columnList.add("id");
columnList.add("name");
columnList.add("number");
columnList.add("sex");
mList.add(columnList);
Cursor cursor = null;
try {
cursor = Connector.getDatabase().rawQuery("select * from friend order by id",null);
if (cursor.moveToFirst()) {
do {
long id = cursor.getLong(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int number = cursor.getInt(cursor.getColumnIndex("number"));
int sex = cursor.getInt(cursor.getColumnIndex("sex"));
List stringList = new ArrayList();
stringList.add(String.valueOf(id));
stringList.add(name);
stringList.add(String.valueOf(number));
stringList.add(String.valueOf(sex));
mList.add(stringList);
} while (cursor.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.notifyDataSetChanged();//重绘当前可见区域
}
});
}
}
}).start();
}
private void refreshListView(long id, String name, int number, boolean sex) {
List stringList = new ArrayList();
stringList.add(String.valueOf(id));
stringList.add(name);
stringList.add(String.valueOf(number));
stringList.add(String.valueOf(sex));
mList.add(stringList);
mAdapter.notifyDataSetChanged();
mDataListView.setSelection(mList.size());
}
}
第七步:布局activity_Data.xml
刚刚成功运行,后期优化了再来更新代码。