安卓写一个读取mysql的user表的用户管理页面,实力完整代码
时间: 2024-02-19 14:57:57 浏览: 61
以下是一个简单的读取mysql的user表的用户管理页面的完整实例代码:
activity_user_management.xml(用户管理页面的布局文件):
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/user_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加用户" />
</LinearLayout>
```
UserAdapter.java(ListView的适配器类):
```java
public class UserAdapter extends BaseAdapter {
private List<User> mUsers;
private LayoutInflater mInflater;
public UserAdapter(Context context, List<User> users) {
mUsers = users;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mUsers.size();
}
@Override
public Object getItem(int position) {
return mUsers.get(position);
}
@Override
public long getItemId(int position) {
return mUsers.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_user, parent, false);
holder = new ViewHolder();
holder.usernameTv = convertView.findViewById(R.id.username_tv);
holder.emailTv = convertView.findViewById(R.id.email_tv);
holder.phoneTv = convertView.findViewById(R.id.phone_tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
User user = mUsers.get(position);
holder.usernameTv.setText(user.getUsername());
holder.emailTv.setText(user.getEmail());
holder.phoneTv.setText(user.getPhone());
return convertView;
}
private static class ViewHolder {
TextView usernameTv;
TextView emailTv;
TextView phoneTv;
}
}
```
item_user.xml(ListView的每个item的布局文件):
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/username_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/email_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
<TextView
android:id="@+id/phone_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
</LinearLayout>
```
UserManagementActivity.java(用户管理页面的Activity类):
```java
public class UserManagementActivity extends AppCompatActivity {
private ListView mUserList;
private Button mAddBtn;
private UserAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_management);
mUserList = findViewById(R.id.user_list);
mAddBtn = findViewById(R.id.add_btn);
mAdapter = new UserAdapter(this, getAllUsers());
mUserList.setAdapter(mAdapter);
mAddBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 添加用户的逻辑
}
});
}
private List<User> getAllUsers() {
List<User> users = new ArrayList<>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DbUtil.getConnection();
stmt = conn.createStatement();
String sql = "SELECT * FROM user";
rs = stmt.executeQuery(sql);
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
user.setPhone(rs.getString("phone"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtil.close(conn, stmt, rs);
}
return users;
}
}
```
其中,getAllUsers()方法和DbUtil类的实现可以参考我之前的回答。这个例子中,我们使用了JDBC连接数据库,执行SQL查询语句,将查询结果封装成User对象,并将User对象保存在List中返回。然后我们使用ListView和UserAdapter来显示这些用户的信息。在UserManagementActivity中,我们将UserAdapter设置为ListView的适配器,使ListView能够显示所有用户的信息。同时,我们还为添加用户按钮设置了点击事件,可以在点击按钮时执行添加用户的逻辑。
阅读全文