我需要一段代码:android studio添加好友到sqLite数据库中,并显示好友列表
时间: 2024-01-23 17:02:03 浏览: 188
好的,以下是一个简单的示例代码,可以将好友添加到SQLite数据库中,并显示好友列表:
首先,在布局文件中添加一个ListView组件用于显示好友列表:
```xml
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
```
然后,在Activity中定义好友类和SQLite数据库辅助类:
```java
public class Friend {
private int id;
private String name;
public Friend() {}
public Friend(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "friends.db";
private static final int DATABASE_VERSION = 1;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE friends (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS friends;");
onCreate(db);
}
}
```
接下来,在Activity中实现添加好友和显示好友列表的功能:
```java
public class MainActivity extends AppCompatActivity {
private DBHelper dbHelper;
private List<Friend> friendList;
private FriendAdapter friendAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new DBHelper(this);
friendList = new ArrayList<>();
friendAdapter = new FriendAdapter(this, friendList);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(friendAdapter);
Button addButton = findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText nameEditText = findViewById(R.id.nameEditText);
String name = nameEditText.getText().toString();
if (!TextUtils.isEmpty(name)) {
Friend friend = new Friend(name);
addFriend(friend);
friendAdapter.add(friend);
nameEditText.setText("");
}
}
});
loadFriends();
}
private void addFriend(Friend friend) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", friend.getName());
db.insert("friends", null, values);
db.close();
}
private void loadFriends() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("friends", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
Friend friend = new Friend(name);
friend.setId(id);
friendList.add(friend);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
}
private static class FriendAdapter extends ArrayAdapter<Friend> {
private LayoutInflater inflater;
public FriendAdapter(Context context, List<Friend> objects) {
super(context, 0, objects);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Friend friend = getItem(position);
holder.nameTextView.setText(friend.getName());
return convertView;
}
private static class ViewHolder {
public TextView nameTextView;
public ViewHolder(View view) {
nameTextView = view.findViewById(android.R.id.text1);
}
}
}
}
```
以上是一个简单的示例代码,可以将好友添加到SQLite数据库中,并显示好友列表。请根据实际需求进行修改和优化。
阅读全文