布局文件
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88FF0000"
android:id="@+id/lv_stu"
/>
</LinearLayout>
使用系统的Adapter
package com.example.listdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private ListView lv_stu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_stu = findViewById(R.id.lv_stu);
String[] names = {"张三","李四","王五","宋江","李逵","张三","李四","王五","宋江","李逵","张三","李四","王五","宋江","李逵","张三","李四","王五","宋江","李逵","张三","李四","王五","宋江","李逵","张三","李四","王五","宋江","李逵"};
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, names);
lv_stu.setAdapter(arrayAdapter);
}
}
使用自定义的studen_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center" />
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(MainActivity.this,
R.layout.student_item, names);
20210330
声明一个学生类型
package com.example.demo;
public class Student {
// 头像
int headImg;
// 姓名
String name;
// 年龄
int age;
public Student(int headImg, String name, int age) {
this.headImg = headImg;
this.name = name;
this.age = age;
}
public int getHeadImg() {
return headImg;
}
public void setHeadImg(int headImg) {
this.headImg = headImg;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
画出每一行的布局 student_item.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="50dp"
tools:context=".MainActivity"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:padding="4dp"
android:id="@+id/iv_head"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/dog1"/>
<TextView
android:id="@+id/tv_name"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"/>
<TextView
android:id="@+id/tv_age"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄"/>
</LinearLayout>
创建学生适配器
package com.example.demo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class StudentAdapter extends BaseAdapter {
// 上下文
private Context context;
// 数据源
private List<Student> studentList;
public StudentAdapter(Context context, List<Student> studentList) {
this.context = context;
this.studentList = studentList;
}
@Override
public int getCount() {
return studentList.size();
}
@Override
public Object getItem(int position) {
return studentList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
// 核心 关键函数 返回每一行的view
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.student_item,
parent, false);
ImageView ivHead = view.findViewById(R.id.iv_head);
TextView tvName = view.findViewById(R.id.tv_name);
TextView tvAge = view.findViewById(R.id.tv_age);
Student student = studentList.get(position);
ivHead.setImageResource(student.getHeadImg());
tvName.setText(student.getName());
tvAge.setText(String.valueOf(student.getAge()));
return 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"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_student"
/>
</LinearLayout>
显示学生列表 MainActivity.java
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView lv_student;
private StudentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Student> studentList = new ArrayList<>();
Student student1 = new Student(R.drawable.dog1, "刘荣", 18);
Student student2 = new Student(R.drawable.dog2, "杨树学", 19);
Student student3 = new Student(R.drawable.dog3, "黄焕秋", 20);
Student student4 = new Student(R.drawable.dog4, "黄心怡", 21);
Student student5 = new Student(R.drawable.dog5, "梁文盛", 22);
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
studentList.add(student4);
studentList.add(student5);
// View
lv_student = findViewById(R.id.lv_student);
// 适配器
adapter = new StudentAdapter(MainActivity.this, studentList);
// 绑定
lv_student.setAdapter(adapter);
}
}
convertView的复用
int count = 0;
// 核心 关键函数 返回每一行的view
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.student_item,
parent, false);
Log.e("StudentAdapter", "inflate " + position + " count = " + (count++));
}
ImageView ivHead = convertView.findViewById(R.id.iv_head);
TextView tvName = convertView.findViewById(R.id.tv_name);
TextView tvAge = convertView.findViewById(R.id.tv_age);
Student student = studentList.get(position);
ivHead.setImageResource(student.getHeadImg());
tvName.setText(student.getName());
tvAge.setText(String.valueOf(student.getAge()));
return convertView;
}
listview的点击事件
// 点击事件
lv_student.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Student student = studentList.get(position);
Toast.makeText(MainActivity.this, student.toString(), Toast.LENGTH_SHORT).show();
}
});
在Student中实现toString方法
@Override
public String toString() {
/*return "Student{" +
"headImg=" + headImg +
", name='" + name + '\'' +
", age=" + age +
'}';*/
return "我是"+ name + "今年" + age;
}
Listview的删除
1.在布局student_item.xml中增加删除图标
<ImageView
android:layout_marginLeft="100dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="10dp"
android:src="@drawable/del"
android:id="@+id/iv_del"
/>
2. 在StudentAdapter中处理点击事件
ImageView ivDel = convertView.findViewById(R.id.iv_del);
ivDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
studentList.remove(position);
// 通知界面刷新
notifyDataSetChanged();
}
});
修改布局 在底部增加一个添加按钮
<?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"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/lv_fruit"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加水果"
android:id="@+id/btn_add"/>
</LinearLayout>
创建添加水果的Activity
添加界面的布局如下:
<?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=".AddFruitActivity"
android:orientation="vertical">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/fruitimgs"
android:id="@+id/sp_fruit"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/apple"
android:id="@+id/iv_fruit"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入水果名字"
android:id="@+id/edt_name"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入水果价格"
android:id="@+id/edt_price"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:id="@+id/btn_add"/>
</LinearLayout>
其中,下拉框需要使用一个数组,该数组定义在string.xml文件中
<resources>
<string name="app_name">Demo</string>
<array name="fruitimgs">
<item>bolo</item>
<item>gold</item>
<item>pear</item>
<item>watermalen</item>
</array>
</resources>
添加界面的Java代码如下:
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
public class AddFruitActivity extends AppCompatActivity {
private Spinner spFruit;
private ImageView ivFruit;
private EditText edtName;
private EditText edtPrice;
private Button btnAdd;
private int imgId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_fruit);
spFruit = findViewById(R.id.sp_fruit);
ivFruit = findViewById(R.id.iv_fruit);
edtName = findViewById(R.id.edt_name);
edtPrice = findViewById(R.id.edt_price);
btnAdd = findViewById(R.id.btn_add);
spFruit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
imgId = R.drawable.boluo;
break;
case 1:
imgId = R.drawable.gold;
break;
case 2:
imgId = R.drawable.pear;
break;
case 3:
imgId = R.drawable.watermalen;
break;
}
ivFruit.setImageResource(imgId);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = edtName.getText().toString();
int price = Integer.parseInt(edtPrice.getText().toString());
Intent intent = new Intent();
intent.putExtra("IMGID", imgId);
intent.putExtra("NAME", name);
intent.putExtra("PRICE", price);
setResult(2001, intent);
finish();// 结束当前界面
}
});
}
}
在主界面中打开添加界面, 使用Intent
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Activity的跳转 使用Intent
Intent intent = new Intent();
intent.setClass(MainActivity.this, AddFruitActivity.class);
startActivityForResult(intent); // 打开界面为了获取结果
}
});
在主界面中接收添加界面返回的参数,并且添加到学生列表 ,然后刷新界面
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("onActivityResult", "requestCode = " + requestCode + " resultCode = " + resultCode);
int imgid = data.getIntExtra("IMGID", R.drawable.apple);
String name = data.getStringExtra("NAME");
int price = data.getIntExtra("PRICE", 0);
Fruit fruit = new Fruit(imgid, name, price);
fruitList.add(fruit);
adapter.notifyDataSetChanged();
}
水果的修改