public class PersonAdapter extends BaseQuickAdapter<Person, BaseViewHolder>{ public PersonAdapter(int layoutResId, List<Person> data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder holder, Person bleDevice) { holder.setText(R.id.tv_device_name, bleDevice.getName()) .setText(R.id.tv_mac_address, bleDevice.getId()); } }
时间: 2023-07-14 18:14:00 浏览: 136
这是一个使用了第三方库BaseQuickAdapter的RecyclerView适配器类PersonAdapter,用于显示Person对象的数据列表。在构造方法中传入布局资源ID和数据列表,然后在convert()方法中将数据绑定到视图上。具体来说,BaseViewHolder是一个通用的ViewHolder,可以使用它来获取视图控件并设置数据。在这个例子中,我们使用了两个TextView来显示Person对象的名称和ID。
相关问题
检查这段代码public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.MyViewHolder>{ // 定义数据集合 private List<Person> mDataSet; private Context context; // 定义ViewHolder类 public static class MyViewHolder extends RecyclerView.ViewHolder { public TextView mNameTextView; public TextView mIdTextView; public MyViewHolder(View itemView) { super(itemView); mNameTextView = itemView.findViewById(R.id.person_name); mIdTextView = itemView.findViewById(R.id.person_id); } } // 构造函数,传入数据集合 public PersonAdapter(List<Person> dataSet,Context context) { this.mDataSet = dataSet; this.context = context; } // 创建ViewHolder @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_person, parent, false); return new MyViewHolder(view); } // 绑定ViewHolder的数据 @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { Person person = mDataSet.get(position); holder.mNameTextView.setText(person.getName()); holder.mIdTextView.setText(String.valueOf(person.getId())); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 点击事件处理,打开详情页 } }); } static class MyViewHolder extends RecyclerView.ViewHolder { TextView mNameTextView; TextView mIdTextView; public MyViewHolder(@NonNull View itemView) { super(itemView); mIdTextView = itemView.findViewById(R.id.person_id); mNameTextView = itemView.findViewById(R.id.person_name); } } // 返回数据集合大小 @Override public int getItemCount() { return mDataSet.size(); } }
这段代码是一个 RecyclerView 的适配器,用于绑定数据集合和 UI 控件。其中,数据集合为一个 Person 类型的 List,包含了每个 Person 对象的姓名和 ID。在 onCreateViewHolder 方法中,通过 LayoutInflater 填充了 item_person 布局文件,创建了一个 MyViewHolder 对象,并返回该对象。在 onBindViewHolder 方法中,将数据集合中的 Person 对象与 ViewHolder 中的控件绑定,同时为 itemView 添加了点击事件,用于处理打开详情页的逻辑。最后,在 getItemCount 方法中返回数据集合的大小。
需要注意的是,该类中存在两个 MyViewHolder 内部类,应该删除其中一个。建议保留第一个 MyViewHolder 类。
在Androidstudio中设计一个简单学生选课系统并给出代码以及XML代码,在这个应用程序中有两个Activity界面,分别是对课程进行操作时的CourseActivity界面,以及对学生操作的PersonActivity界面。在CourseActivity界面可以对课程进行添加,删除以及跳转到PersonActivity功能和通过ListView将所有课程显示在页面上,在PersonActivity中可以对person表数据进行增、删、改等操作和通过ListView将所有学生信息显示在页面上,在这个
应用程序中,我们需要创建两个Java类和两个XML布局文件,其中一个布局文件是用于CourseActivity,另一个布局文件是用于PersonActivity。以下是详细的代码和XML文件。
CourseActivity.java:
```
public class CourseActivity extends AppCompatActivity {
private ListView courseListView;
private CourseAdapter courseAdapter;
private List<Course> courseList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
courseListView = findViewById(R.id.course_list_view);
courseList = new ArrayList<>();
courseAdapter = new CourseAdapter(this, R.layout.course_list_item, courseList);
courseListView.setAdapter(courseAdapter);
Button addCourseBtn = findViewById(R.id.add_course_btn);
addCourseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CourseActivity.this, AddCourseActivity.class);
startActivityForResult(intent, 1);
}
});
courseListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(CourseActivity.this, PersonActivity.class);
intent.putExtra("course_name", courseList.get(i).getName());
startActivity(intent);
}
});
courseListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
AlertDialog.Builder builder = new AlertDialog.Builder(CourseActivity.this);
builder.setMessage("确定要删除此课程?")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int j) {
courseList.remove(i);
courseAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("取消", null);
builder.create().show();
return true;
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
String courseName = data.getStringExtra("course_name");
int credit = data.getIntExtra("credit", 0);
Course course = new Course(courseName, credit);
courseList.add(course);
courseAdapter.notifyDataSetChanged();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
```
PersonActivity.java:
```
public class PersonActivity extends AppCompatActivity {
private ListView personListView;
private PersonAdapter personAdapter;
private List<Person> personList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
personListView = findViewById(R.id.person_list_view);
personList = new ArrayList<>();
personAdapter = new PersonAdapter(this, R.layout.person_list_item, personList);
personListView.setAdapter(personAdapter);
Button addPersonBtn = findViewById(R.id.add_person_btn);
addPersonBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(PersonActivity.this, AddPersonActivity.class);
startActivityForResult(intent, 1);
}
});
personListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
AlertDialog.Builder builder = new AlertDialog.Builder(PersonActivity.this);
builder.setMessage("确定要删除此学生?")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int j) {
personList.remove(i);
personAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("取消", null);
builder.create().show();
return true;
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
String name = data.getStringExtra("name");
String sex = data.getStringExtra("sex");
int age = data.getIntExtra("age", 0);
Person person = new Person(name, sex, age);
personList.add(person);
personAdapter.notifyDataSetChanged();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
```
activity_course.xml:
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".CourseActivity">
<Button
android:id="@+id/add_course_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加课程"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"/>
<ListView
android:id="@+id/course_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/add_course_btn"/>
</RelativeLayout>
```
course_list_item.xml:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/course_name_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
<TextView
android:id="@+id/course_credit_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
</LinearLayout>
```
activity_person.xml:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/add_person_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加学生"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>
<ListView
android:id="@+id/person_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
```
person_list_item.xml:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/person_name_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
<TextView
android:id="@+id/person_sex_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
<TextView
android:id="@+id/person_age_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
</LinearLayout>
```
CourseAdapter.java:
```
public class CourseAdapter extends ArrayAdapter<Course> {
private int resourceId;
public CourseAdapter(Context context, int resourceId, List<Course> objects) {
super(context, resourceId, objects);
this.resourceId = resourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Course course = getItem(position);
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.courseNameTv = view.findViewById(R.id.course_name_tv);
viewHolder.courseCreditTv = view.findViewById(R.id.course_credit_tv);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.courseNameTv.setText(course.getName());
viewHolder.courseCreditTv.setText(String.valueOf(course.getCredit()));
return view;
}
class ViewHolder {
TextView courseNameTv;
TextView courseCreditTv;
}
}
```
PersonAdapter.java:
```
public class PersonAdapter extends ArrayAdapter<Person> {
private int resourceId;
public PersonAdapter(Context context, int resourceId, List<Person> objects) {
super(context, resourceId, objects);
this.resourceId = resourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Person person = getItem(position);
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.personNameTv = view.findViewById(R.id.person_name_tv);
viewHolder.personSexTv = view.findViewById(R.id.person_sex_tv);
viewHolder.personAgeTv = view.findViewById(R.id.person_age_tv);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.personNameTv.setText(person.getName());
viewHolder.personSexTv.setText(person.getSex());
viewHolder.personAgeTv.setText(String.valueOf(person.getAge()));
return view;
}
class ViewHolder {
TextView personNameTv;
TextView personSexTv;
TextView personAgeTv;
}
}
```
AddCourseActivity.java:
```
public class AddCourseActivity extends AppCompatActivity {
private EditText courseNameEt;
private EditText creditEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_course);
courseNameEt = findViewById(R.id.course_name_et);
creditEt = findViewById(R.id.credit_et);
Button confirmBtn = findViewById(R.id.confirm_btn);
confirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("course_name", courseNameEt.getText().toString());
intent.putExtra("credit", Integer.parseInt(creditEt.getText().toString()));
setResult(RESULT_OK, intent);
finish();
}
});
Button cancelBtn = findViewById(R.id.cancel_btn);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
```
AddPersonActivity.java:
```
public class AddPersonActivity extends AppCompatActivity {
private EditText nameEt;
private EditText sexEt;
private EditText ageEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_person);
nameEt = findViewById(R.id.name_et);
sexEt = findViewById(R.id.sex_et);
ageEt = findViewById(R.id.age_et);
Button confirmBtn = findViewById(R.id.confirm_btn);
confirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("name", nameEt.getText().toString());
intent.putExtra("sex", sexEt.getText().toString());
intent.putExtra("age", Integer.parseInt(ageEt.getText().toString()));
setResult(RESULT_OK, intent);
finish();
}
});
Button cancelBtn = findViewById(R.id.cancel_btn);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
```
以上是一个简单的学生选课系统的实现,其中CourseActivity界面可以对课程进行添加、删除以及跳转到PersonActivity功能,PersonActivity界面可以对person表数据进行增、删、改等操作。
阅读全文