正式看此文章前,请先浏览https://blog.csdn.net/weixin_44462664/article/details/102565639
实现登陆后在下个活动中显示ListView
在 包名下 ⋙ New ⋙ 新建实体类 Item.java 文件
public class Item {
private String account;
private String password;
public void setAccount(String account){
this.account =account;
}
public void setPassword(String password){
this.password =password;
}
public String getAccount(){
return account;
}
public String getPassword(){
return password;
}
}
在layout目录下 ⋙ New ⋙ XML ⋙ Layout XML File 新建 item.xml布局文件作为ListView子项的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/account"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="用户名:"
android:textSize="18dp"
/>
<TextView
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="密码:"
android:textSize="18dp"
/>
</LinearLayout>
效果图
在 包名下 ⋙ New ⋙ Activity ⋙ Empty Activity 新建新的活动,保持默认即可
修改layout目录下的 activity_main2.xml 添加ListView
注意: 必须要给该部件添加id值
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<ListView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
在 包名下 ⋙ New ⋙ Java 新建适配器类 MyAdapter ,使其继承自ArrayAdapter
public class LoginAdapter extends ArrayAdapter {
private int resourceId;/*ListView子项布局ID*/
public LoginAdapter(Context context, int resource, List<Item> objects) {
super(context, resource, objects);
resourceId = resource;/*ListView子项布局ID*/
}
@Override
public View getView(int positiion, View convertView, ViewGroup parent){/*positiion 相当于索引*/
Item item = (Item) getItem(positiion);/*获取当前项的Item实例*/
View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);/*为ListView 子项加载传入的布局*/
//LayoutInflater的inflate()方法接收3个参数:需要实例化布局资源的id、ViewGroup类型视图组对象、false
//false表示只让父布局中声明的layout属性生效,但不会为这个view添加父布局
TextView account = (TextView)view.findViewById(R.id.account);
TextView password = (TextView)view.findViewById(R.id.password);
account.setText("用户名:"+item.getAccount());
password.setText("密码:"+item.getPassword());
return view;/*返回子项布局*/
}
}
修改Dao.java内部代码,添加
private String sql2 = "select * from login ";
public List<Item> loginList() throws SQLException {
List<Item> list = new ArrayList<>();
conn = Dbconn.getConn();
Log.d("数据库连接返回值", "login: "+conn);
preStmt = conn.prepareStatement(sql2);
rs = preStmt.executeQuery();
while(rs.next()){
Item item = new Item();
item.setAccount(rs.getString(1));
item.setPassword(rs.getString(2));
list.add(item);
}
Dbconn.closeConn(conn,preStmt,rs);
return list;
}
修改Main2Activity内部代码
public class Main2Activity extends AppCompatActivity {
private static final String TAG = "Main2Activity活动";
private List<Item> itemList = new ArrayList<>();
private ListView listView;
@SuppressLint("HandlerLeak")
private Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what){
case 1:
Log.d(TAG, "run: UI更新成功");
MyAdapter myAdapter= new MyAdapter(Main2Activity.this, R.layout.item, itemList);
listView = (ListView)findViewById(R.id.list_item);
listView.setAdapter(myAdapter);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initLogin();
}
private void initLogin(){
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread);
thread.start();
}
private class MyThread implements Runnable{
@Override
public void run() {
Dao dao = new Dao();
try {
itemList = dao.loginList();
Log.d(TAG, "run: 数据库数据读取成功");
} catch (SQLException e) {
e.printStackTrace();
Log.d(TAG, "run: " +
"数据库数据读取失败");
}
Message msg = new Message();
msg.what =1;
myHandler.sendMessage(msg);
Log.d(TAG, "run: Message 唯一识别码 msg.what 发送成功");
}
}
}
修改MainActivity内部代码
//myHandler在主线程中创建,所以自动绑定主线程
@SuppressLint("HandlerLeak")
private Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what){
case 1:
Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
Log.d(TAG, "run: UI更新成功");
break;
default:
break;
}
}
};
运行结果