1. 涉及到的技术点
- 数据库SQLite的使用
- 列表控件ListView的使用
- ListView事件监听
- 适配器BaseAdapter的使用
- 线性布局LinearLayoutCompat的使用
2. 发环境
- 开发工具:AndroidStudio
- 开发语言:java
- jdk版本:8.0+以上
3.需求分析
在上集中,实现了将通讯录联系人插入到数据库中,这集将数据库中的数据通过ListView控件,将通讯录联系人展示出来
温馨提示:
通讯录的主页是BottomNavigationView底部导航栏的形式,底部导航栏的案例,本人出了教程视频,这里就忽略掉这步的实现过程,文章最后会给到底部导航栏教程视频
4. 实现步骤
- 编写fragment_home.xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/teal_200"
app:title="班级通讯录"
app:titleTextColor="@color/white">
<TextView
android:id="@+id/create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="12sp"
android:text="新建"
android:textColor="@color/white" />
</androidx.appcompat.widget.Toolbar>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
tools:listitem="@layout/list_item" />
<androidx.cardview.widget.CardView
android:id="@+id/music"
android:layout_width="58dp"
android:layout_height="58dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="20dp"
android:layout_marginBottom="80dp"
android:backgroundTint="@color/teal_200"
app:cardCornerRadius="34dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/baseline_library_music_24"
android:text="音乐" />
</androidx.cardview.widget.CardView>
</RelativeLayout>
效果图如下
ListView 需要一个布局文件 list_item.xml
在适配器中需要用到
- 编写Adapter适配器
public class MyListAdapter extends BaseAdapter {
private List<StudentInfo> mStudentInfoList = new ArrayList<>();
public void setStudentList(List<StudentInfo> studentInfoList) {
mStudentInfoList = studentInfoList;
notifyDataSetChanged();
}
@Override
public int getCount() {
return mStudentInfoList.size();
}
@Override
public StudentInfo getItem(int i) {
return mStudentInfoList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
//加载布局文件
View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item, null);
//初始化控件
TextView username = rootView.findViewById(R.id.username);
TextView mobile = rootView.findViewById(R.id.mobile);
TextView class_name = rootView.findViewById(R.id.class_name);
TextView first_letter = rootView.findViewById(R.id.first_letter);
//绑定数据
StudentInfo studentInfo = getItem(i);
username.setText(studentInfo.getUsername());
mobile.setText(studentInfo.getMobile());
class_name.setText(studentInfo.getClass_name());
//取名字的第一个字显示
String letter = studentInfo.getUsername().substring(0, 1);
first_letter.setText(letter);
// 创建Random类的实例
Random random = new Random();
// 生成1-4之间的随机数
int randomNumber = random.nextInt(4) + 1;
if (randomNumber == 1) {
first_letter.setBackgroundColor(Color.parseColor("#ff6161"));
} else if (randomNumber == 2) {
first_letter.setBackgroundColor(Color.parseColor("#00cee5"));
} else if (randomNumber == 3) {
first_letter.setBackgroundColor(Color.parseColor("#ffb120"));
} else {
first_letter.setBackgroundColor(Color.parseColor("#7484fc"));
}
return rootView;
}
}
温馨提示:StudentInfo实体,在第三集中有讲
5. 代码实现全部过程
public class HomeFragment extends Fragment {
private View rootView;
private ListView listview;
private MyListAdapter mListAdapter;
private StudentDb mStudentDb;
private int currentIndex = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_home, container, false);
//初始化控件
listview = rootView.findViewById(R.id.listview);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//初始化数据库
mStudentDb = StudentDb.getInstance(getActivity());
//初始化适配器
mListAdapter = new MyListAdapter();
//设置适配器
listview.setAdapter(mListAdapter);
//设置数据
mListAdapter.setStudentList(mStudentDb.queryListStudent());
//listview点击事件
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), EditStudentInfoActivity.class);
StudentInfo studentInfo = mListAdapter.getItem(position);
intent.putExtra("studentInfo", studentInfo);
startActivityForResult(intent, 1000);
}
});
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
StudentInfo item = mListAdapter.getItem(i);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("操作");
builder.setSingleChoiceItems(new String[]{"拨打电话", "删除"}, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
currentIndex = which;
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (currentIndex == 0) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + item.getMobile()));
startActivity(intent);
} else {
mStudentDb.delete(item.get_id());
Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show();
//刷新数据
mListAdapter.setStudentList(mStudentDb.queryListStudent());
}
currentIndex = 0;
}
});
builder.show();
return true;
}
});
//新建
rootView.findViewById(R.id.create).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(getActivity(), AddStudentInfoActivity.class), 1000);
}
});
//点击音乐播放
rootView.findViewById(R.id.music).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getActivity(), MusicActivity.class));
}
});
}
public class MyListAdapter extends BaseAdapter {
private List<StudentInfo> mStudentInfoList = new ArrayList<>();
public void setStudentList(List<StudentInfo> studentInfoList) {
mStudentInfoList = studentInfoList;
notifyDataSetChanged();
}
@Override
public int getCount() {
return mStudentInfoList.size();
}
@Override
public StudentInfo getItem(int i) {
return mStudentInfoList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
//加载布局文件
View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item, null);
//初始化控件
TextView username = rootView.findViewById(R.id.username);
TextView mobile = rootView.findViewById(R.id.mobile);
TextView class_name = rootView.findViewById(R.id.class_name);
TextView first_letter = rootView.findViewById(R.id.first_letter);
//绑定数据
StudentInfo studentInfo = getItem(i);
username.setText(studentInfo.getUsername());
mobile.setText(studentInfo.getMobile());
class_name.setText(studentInfo.getClass_name());
//取名字的第一个字显示
String letter = studentInfo.getUsername().substring(0, 1);
first_letter.setText(letter);
// 创建Random类的实例
Random random = new Random();
// 生成1-4之间的随机数
int randomNumber = random.nextInt(4) + 1;
if (randomNumber == 1) {
first_letter.setBackgroundColor(Color.parseColor("#ff6161"));
} else if (randomNumber == 2) {
first_letter.setBackgroundColor(Color.parseColor("#00cee5"));
} else if (randomNumber == 3) {
first_letter.setBackgroundColor(Color.parseColor("#ffb120"));
} else {
first_letter.setBackgroundColor(Color.parseColor("#7484fc"));
}
return rootView;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 1000) {
//刷新数据
mListAdapter.setStudentList(mStudentDb.queryListStudent());
}
}
}
onActivityResult
这个方法的实现,是处理在编辑通讯之后,回来需要及时刷新列表数据- HomeFragment就是BottomNavigationView底部导航栏的首页