解释下列代码List<String> regionGuidList = (Integer.valueOf(1).equals(super.baseMapper.regionIsSiteOrNot(regionGuid))) ? this.listSiteSonRegionGuid(regionGuid) : this.listOnlyCurrentRegion(regionGuid); log.info("查询到的区划列表 = {}", CollUtil.isEmpty(regionGuidList) ? "为空,不存到cache中" : "不为空,存入 cache, 缓存时间 " + SystemConstance.ONE_HOUR + "s"); Optional.of(regionGuidList).filter(CollUtil::isNotEmpty).ifPresent(list -> AssertUtils.isTrue(redisUtil.set(siteRegionListKey, JsonUtils.toJson(regionGuidList), SystemConstance.ONE_HOUR), GlobalErrorEnum.SET_REDIS_KEY_ERROR));
时间: 2024-04-01 20:34:53 浏览: 143
这段代码的作用是查询特定区划的子区划列表或者当前区划列表,并将查询结果存储到缓存中。具体解释如下:
1. 首先,代码定义了一个 `List<String>` 类型的变量 `regionGuidList`,并根据 `regionGuid` 所在的区域是否为站点来选择不同的查询方式。如果 `regionGuid` 所在的区域是站点,则调用 `listSiteSonRegionGuid` 方法查询该站点的所有子区划列表,否则调用 `listOnlyCurrentRegion` 方法查询当前区划列表。
2. 然后,代码使用日志记录查询到的区划列表。如果区划列表为空,则不需要将其存储到缓存中,否则将其存储到缓存中,并指定缓存时间为1小时。
3. 最后,代码使用 `Optional` 对象对区划列表进行判断,如果区划列表不为空,则将其转换为 JSON 字符串并存储到 Redis 缓存中,缓存的 Key 值为 `siteRegionListKey`,缓存时间为1小时。如果存储失败,则抛出异常 `GlobalErrorEnum.SET_REDIS_KEY_ERROR`。
相关问题
android使用RecyclerView获取应用包名,按照首字母排序,支持字幕条索引,使用android系统自带的汉字转拼音HanziToPinyin类,汉字转拼音不使用import第三方库,代码使用androidx库完整布局实现代码,包含import,实现setOnTouchLetterChangeListenner
好的,以下是代码实现和解析:
1. 布局文件
我们需要在布局文件中添加一个RecyclerView和一个用于显示字幕索引的View,如下所示:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<View
android:id="@+id/letterIndexView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:gravity="center_vertical"
android:paddingStart="16dp"
android:paddingEnd="16dp" />
</LinearLayout>
```
2. 实体类
我们需要定义一个实体类,用于存储应用程序的信息,包括应用程序名和应用程序包名:
```java
public class AppInfo {
private String appName;
private String packageName;
public AppInfo(String appName, String packageName) {
this.appName = appName;
this.packageName = packageName;
}
public String getAppName() {
return appName;
}
public String getPackageName() {
return packageName;
}
}
```
3. ViewHolder类
接下来,我们需要定义一个ViewHolder类,用于显示每个应用程序的信息:
```java
public class AppViewHolder extends RecyclerView.ViewHolder {
private TextView tvName;
private TextView tvPackage;
public AppViewHolder(@NonNull View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tvName);
tvPackage = itemView.findViewById(R.id.tvPackage);
}
public void bind(AppInfo appInfo) {
tvName.setText(appInfo.getAppName());
tvPackage.setText(appInfo.getPackageName());
}
}
```
4. Adapter类
然后,我们需要定义一个Adapter类,用于将应用程序的信息显示在RecyclerView中:
```java
public class AppAdapter extends RecyclerView.Adapter<AppViewHolder> implements SectionIndexer {
private List<AppInfo> appList;
private Map<String, Integer> indexMap;
public AppAdapter(List<AppInfo> appList) {
this.appList = appList;
indexMap = new HashMap<>();
for (int i = 0; i < appList.size(); i++) {
String index = HanziToPinyin.getFirstLetter(appList.get(i).getAppName());
if (!indexMap.containsKey(index)) {
indexMap.put(index, i);
}
}
}
@NonNull
@Override
public AppViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app, parent, false);
return new AppViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AppViewHolder holder, int position) {
holder.bind(appList.get(position));
}
@Override
public int getItemCount() {
return appList.size();
}
@Nullable
@Override
public Object[] getSections() {
return indexMap.keySet().toArray();
}
@Override
public int getPositionForSection(int sectionIndex) {
return indexMap.get(getSections()[sectionIndex].toString());
}
@Override
public int getSectionForPosition(int position) {
String index = HanziToPinyin.getFirstLetter(appList.get(position).getAppName());
for (int i = 0; i < getSections().length; i++) {
if (getSections()[i].toString().equals(index)) {
return i;
}
}
return 0;
}
}
```
在Adapter类中,我们首先使用HanziToPinyin类将应用程序名转换为拼音,并按照首字母进行排序。然后,我们实现了SectionIndexer接口,以支持字幕索引。getSections()方法返回字幕索引数组,getPositionForSection()方法返回给定字幕索引位置的第一个应用程序的位置,getSectionForPosition()方法返回给定位置的应用程序的字幕索引位置。
5. Activity类
最后,我们需要在Activity类中设置RecyclerView的布局管理器和Adapter,并实现字幕索引的显示和触摸事件:
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private View letterIndexView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
letterIndexView = findViewById(R.id.letterIndexView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
List<AppInfo> appList = getAppList();
AppAdapter adapter = new AppAdapter(appList);
recyclerView.setAdapter(adapter);
setupLetterIndexer(adapter);
}
private List<AppInfo> getAppList() {
List<AppInfo> appList = new ArrayList<>();
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolveInfoList) {
String appName = resolveInfo.loadLabel(packageManager).toString();
String packageName = resolveInfo.activityInfo.packageName;
appList.add(new AppInfo(appName, packageName));
}
Collections.sort(appList, new Comparator<AppInfo>() {
@Override
public int compare(AppInfo o1, AppInfo o2) {
return o1.getAppName().compareTo(o2.getAppName());
}
});
return appList;
}
private void setupLetterIndexer(AppAdapter adapter) {
TextView[] letterViews = new TextView[26];
for (int i = 0; i < letterViews.length; i++) {
letterViews[i] = new TextView(this);
letterViews[i].setText(String.valueOf((char) ('A' + i)));
letterViews[i].setTextSize(14);
letterViews[i].setTextColor(Color.parseColor("#666666"));
letterViews[i].setPadding(0, 0, 0, 0);
letterViews[i].setGravity(Gravity.CENTER);
letterIndexView.addView(letterViews[i]);
}
letterIndexView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float y = event.getY();
int index = (int) (y / letterIndexView.getHeight() * letterViews.length);
if (index >= 0 && index < letterViews.length) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
letterViews[index].setTextColor(Color.parseColor("#1E90FF"));
break;
case MotionEvent.ACTION_MOVE:
for (TextView letterView : letterViews) {
letterView.setTextColor(Color.parseColor("#666666"));
}
letterViews[index].setTextColor(Color.parseColor("#1E90FF"));
int position = adapter.getPositionForSection(index);
((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, 0);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
for (TextView letterView : letterViews) {
letterView.setTextColor(Color.parseColor("#666666"));
}
break;
}
}
return true;
}
});
}
}
```
在Activity类中,我们首先获取所有应用程序的信息,并按应用程序名称进行排序。然后,我们设置RecyclerView的布局管理器和Adapter,并调用setupLetterIndexer()方法设置字幕索引的显示和触摸事件。在该方法中,我们首先使用一个TextView数组创建26个字母视图,并将它们添加到字幕索引View中。然后,我们设置字母索引视图的触摸事件,根据触摸位置计算字母索引位置,并将RecyclerView滚动到对应的位置。
安卓studio学生管理系统源代码
以下是两个基于Android Studio开发的学生信息管理系统的源代码:
1. 学生信息管理系统实现源码(安卓开发教程课后练习)
```java
// 省略部分代码
public class MainActivity extends AppCompatActivity {
private EditText et_name, et_age, et_major;
private Button btn_add, btn_query, btn_update, btn_delete;
private ListView lv_student;
private List<Student> studentList = new ArrayList<>();
private StudentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = findViewById(R.id.et_name);
et_age = findViewById(R.id.et_age);
et_major = findViewById(R.id.et_major);
btn_add = findViewById(R.id.btn_add);
btn_query = findViewById(R.id.btn_query);
btn_update = findViewById(R.id.btn_update);
btn_delete = findViewById(R.id.btn_delete);
lv_student = findViewById(R.id.lv_student);
adapter = new StudentAdapter(this, studentList);
lv_student.setAdapter(adapter);
// 添加学生信息
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = et_name.getText().toString().trim();
String age = et_age.getText().toString().trim();
String major = et_major.getText().toString().trim();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(age) || TextUtils.isEmpty(major)) {
Toast.makeText(MainActivity.this, "请填写完整信息", Toast.LENGTH_SHORT).show();
return;
}
Student student = new Student(name, Integer.parseInt(age), major);
studentList.add(student);
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
}
});
// 查询学生信息
btn_query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = et_name.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(MainActivity.this, "请输入姓名", Toast.LENGTH_SHORT).show();
return;
}
for (Student student : studentList) {
if (name.equals(student.getName())) {
et_age.setText(String.valueOf(student.getAge()));
et_major.setText(student.getMajor());
Toast.makeText(MainActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
return;
}
}
Toast.makeText(MainActivity.this, "未找到该学生信息", Toast.LENGTH_SHORT).show();
}
});
// 修改学生信息
btn_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = et_name.getText().toString().trim();
String age = et_age.getText().toString().trim();
String major = et_major.getText().toString().trim();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(age) || TextUtils.isEmpty(major)) {
Toast.makeText(MainActivity.this, "请填写完整信息", Toast.LENGTH_SHORT).show();
return;
}
for (Student student : studentList) {
if (name.equals(student.getName())) {
student.setAge(Integer.parseInt(age));
student.setMajor(major);
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
return;
}
}
Toast.makeText(MainActivity.this, "未找到该学生信息", Toast.LENGTH_SHORT).show();
}
});
// 删除学生信息
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = et_name.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(MainActivity.this, "请输入姓名", Toast.LENGTH_SHORT).show();
return;
}
for (Student student : studentList) {
if (name.equals(student.getName())) {
studentList.remove(student);
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
return;
}
}
Toast.makeText(MainActivity.this, "未找到该学生信息", Toast.LENGTH_SHORT).show();
}
});
}
}
```
2. 基于SQLite数据库编写的学生管理系统源码
```java
// 省略部分代码
public class MainActivity extends AppCompatActivity {
private EditText et_name, et_age, et_major;
private Button btn_add, btn_query, btn_update, btn_delete;
private ListView lv_student;
private List<Student> studentList = new ArrayList<>();
private StudentAdapter adapter;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = findViewById(R.id.et_name);
et_age = findViewById(R.id.et_age);
et_major = findViewById(R.id.et_major);
btn_add = findViewById(R.id.btn_add);
btn_query = findViewById(R.id.btn_query);
btn_update = findViewById(R.id.btn_update);
btn_delete = findViewById(R.id.btn_delete);
lv_student = findViewById(R.id.lv_student);
adapter = new StudentAdapter(this, studentList);
lv_student.setAdapter(adapter);
// 打开或创建数据库
db = openOrCreateDatabase("student.db", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, major TEXT)");
// 添加学生信息
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = et_name.getText().toString().trim();
String age = et_age.getText().toString().trim();
String major = et_major.getText().toString().trim();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(age) || TextUtils.isEmpty(major)) {
Toast.makeText(MainActivity.this, "请填写完整信息", Toast.LENGTH_SHORT).show();
return;
}
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", Integer.parseInt(age));
values.put("major", major);
db.insert("student", null, values);
queryData();
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
}
});
// 查询学生信息
btn_query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = et_name.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(MainActivity.this, "请输入姓名", Toast.LENGTH_SHORT).show();
return;
}
Cursor cursor = db.rawQuery("SELECT * FROM student WHERE name=?", new String[]{name});
if (cursor.moveToFirst()) {
et_age.setText(String.valueOf(cursor.getInt(cursor.getColumnIndex("age"))));
et_major.setText(cursor.getString(cursor.getColumnIndex("major")));
Toast.makeText(MainActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "未找到该学生信息", Toast.LENGTH_SHORT).show();
}
cursor.close();
}
});
// 修改学生信息
btn_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = et_name.getText().toString().trim();
String age = et_age.getText().toString().trim();
String major = et_major.getText().toString().trim();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(age) || TextUtils.isEmpty(major)) {
Toast.makeText(MainActivity.this, "请填写完整信息", Toast.LENGTH_SHORT).show();
return;
}
ContentValues values = new ContentValues();
values.put("age", Integer.parseInt(age));
values.put("major", major);
db.update("student", values, "name=?", new String[]{name});
queryData();
Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
}
});
// 删除学生信息
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = et_name.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(MainActivity.this, "请输入姓名", Toast.LENGTH_SHORT).show();
return;
}
db.delete("student", "name=?", new String[]{name});
queryData();
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
}
});
// 查询所有学生信息
queryData();
}
// 查询数据库中所有学生信息
private void queryData() {
studentList.clear();
Cursor cursor = db.rawQuery("SELECT * FROM student", null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
String major = cursor.getString(cursor.getColumnIndex("major"));
Student student = new Student(name, age, major);
studentList.add(student);
}
adapter.notifyDataSetChanged();
cursor.close();
}
}
```
阅读全文