SQList基础+ListView基本使用

今日所学:

SQList基础语法

SDList下载地址 SQLite Download Page

SQList安装教程SQLite的安装与基本操作 - 极客开发者-博客

ListView用法

 

没遇到什么问题

 

成果展示

 

代码:

MainActivity.java
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.listview.addapt.BaseAddapt;
import com.example.listview.bin.Student;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView mListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        //初始化数据
        initData();
    }

    private void initData() {
        List<Student> students = new ArrayList<>();
        for(int i=0;i<100;i++){
            students.add(new Student(i,"学生"+i,"描述"+i));
        }
        //实例化构造器
        BaseAddapt myAdapter = new BaseAddapt(students,getApplicationContext());
        mListView.setAdapter(myAdapter);
        //注册点击事件
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),"当前位置:"+position,Toast.LENGTH_LONG);
            }
        });
    }

    private void initUI() {
        mListView = findViewById(R.id.list_view);
        
    }
}

 

activity_main.xml

<?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"
    tools:context=".MainActivity">

   <ListView
       android:id="@+id/list_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>

</LinearLayout>

 

item_list.xml

  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">
    <TextView
        android:id="@+id/tv_name"
        android:text="姓名"
        android:textColor="@android:color/holo_red_light"
        android:textSize="22sp"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/tv_des"
        android:text="描述"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        android:layout_height="wrap_content"/>

</LinearLayout>

 

 

github:查看代码

 

明日计划:

保存用户信息到数据库

 

posted @ 2021-01-20 22:48  我试试这个昵称好使不  阅读(190)  评论(0编辑  收藏  举报