我有自定义的学生详细信息清单,我希望将所选项目的详细信息输入SQlite数据库。自定义Listview包含学生" reg_no" ,"名字","姓氏"和"姓"。任何快速帮助都非常感谢。这是我的代码。
MainActvity.java
public class StudentTake100Activity extends AppCompatActivity {
private ListView lvStudentlist;
private StudentListAdapter1 adapter;
private List<StudentList> mStudentList;
private CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_take100);
lvStudentlist= (ListView) findViewById(R.id.listview_studentlist);
lvStudentlist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
final DatabaseHandler db = new DatabaseHandler(this);
mStudentList = new ArrayList<>();
//Add sample data for list
//We can get data from DB, webservice here
mStudentList.add(new StudentList(1, "U11EE1001", "Bargo","S.","Mayafi"));
mStudentList.add(new StudentList(2, "U11EE1002", "Barnsbas","Snake.","Maciji"));
mStudentList.add(new StudentList(3, "U11EE1004", "Adamu","Tanko.","Sadau"));
mStudentList.add(new StudentList(4, "U11EE1005", "Munzali","","Cire Tallafi"));
//Init adapter
adapter = new StudentListAdapter1(getApplicationContext(), mStudentList);
lvStudentlist.setAdapter(adapter);
checkBox = (CheckBox) findViewById(R.id.checkBox);
lvStudentlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do something
SparseBooleanArray checked = lvStudentlist.getCheckedItemPositions();
int size = checked.size(); // number of name-value pairs in the array
for (int i = 0; i < size; i++) {
int key = checked.keyAt(i);
boolean value = checked.get(key);
if (value)
doSomethingWithSelectedIndex(key);
db.addAttendanceList(key);
}
}
});
}
}
答案 0 :(得分:0)
将onItemClickListener更改为以下实现:
lvStudentlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
StudentList studentList = (StudentList)parent.getItemAtPosition(position);
//Do something
//SparseBooleanArray checked = //lvStudentlist.getCheckedItemPositions();
//int size = checked.size(); // number of name-value pairs in the array
//for (int i = 0; i < size; i++) {
//int key = checked.keyAt(i);
//boolean value = checked.get(key);
//if (value)
// doSomethingWithSelectedIndex(key);
//db.addAttendanceList(key);
//}
// get items from the studentList object
String studentName = studentList.getName();
int regNo = studentList.getRegNo(); // other fields as well
}
});
根据需要使用这些字段,我希望你的StudentList类中有getter。这应该做到这一点。