如何从自定义列表视图中获取所有edittext值

时间:2017-08-08 11:00:02

标签: android listview android-arrayadapter

我的自定义列表视图中有20行,但我可以从ListView的屏幕可见性部分获取值。无法从自定义列表视图中获取所有20行值。在运行时我得到像

这样的错误
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

这是我的BaseAdapter类。在本课程中,我调用自己的arraylist,它从AddClassTestMarkActivity类中获取值。

public class StudentListClassTestMarkBaseAdapter extends BaseAdapter {

ArrayList<StudentsClassTestMarks> myList = new ArrayList<StudentsClassTestMarks>();
LayoutInflater inflater;
Context context;

public StudentListClassTestMarkBaseAdapter(Context context, ArrayList<StudentsClassTestMarks> myList) {
    this.myList = myList;
    this.context = context;
    inflater = LayoutInflater.from(this.context);
}

@Override
public int getCount() {
    return myList.size();
}

@Override
public StudentsClassTestMarks getItem(int position) {
    return myList.get(position);
}

@Override
public long getItemId(int position) {
    if (myList != null) {
        return myList.get(position).id;
    }
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MyViewHolder mViewHolder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.students_class_test_mark_list_item, parent, false);
        mViewHolder = new MyViewHolder(convertView);
        mViewHolder.edtStudentMarks = (EditText) convertView.findViewById(R.id.class_test_marks);
        mViewHolder.edtStudentMarks.setText("" + mViewHolder.defaultNumber);

        convertView.setTag(mViewHolder);
    } else {
        mViewHolder = (MyViewHolder) convertView.getTag();
    }

    StudentsClassTestMarks currentListData = getItem(position);

    mViewHolder.txtStudentId.setText(currentListData.getEnrollId());
    mViewHolder.txtStudentName.setText(currentListData.getStudentName());

    return convertView;
}

private class MyViewHolder {

    TextView txtStudentId;
    TextView txtStudentName;
    EditText edtStudentMarks;
    int defaultNumber = 0;

    public MyViewHolder(View item) {
        txtStudentId = (TextView) item.findViewById(R.id.txt_studentId);
        txtStudentName = (TextView) item.findViewById(R.id.txt_studentName);
    }
}
}

我的活动课

public class AddClassTestMarkActivity extends AppCompatActivity {

ListView lvStudent;
ArrayList<StudentsClassTestMarks> myList = new ArrayList<StudentsClassTestMarks>();   

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_class_test_mark);  

    lvStudent = (ListView) findViewById(R.id.listView_students);

    btnSave = (ImageView) findViewById(R.id.btnSave);
    btnSave.setOnClickListener(this);        

    GetStudentsList(classId);

    lvStudent.setItemsCanFocus(true);

    StudentListClassTestMarkBaseAdapter cadapter = new StudentListClassTestMarkBaseAdapter(AddClassTestMarkActivity.this, myList);
    lvStudent.setAdapter(cadapter);

}

private void GetStudentsList(String classSectionId) {

    myList.clear();

    try {
        Cursor c = db.getStudentsOfClassBasedOnClassId(classSectionId);
        if (c.getCount() > 0) {
            if (c.moveToFirst()) {
                int i = 0;
                do {
                    StudentsClassTestMarks lde = new StudentsClassTestMarks();
                    lde.setId(Integer.parseInt(c.getString(0)));
                    lde.setEnrollId(c.getString(1));
                    lde.setAdmissionId(c.getString(2));
                    lde.setClassId(c.getString(3));
                    storeClassId = c.getString(3);
                    lde.setStudentName(c.getString(4));
                    lde.setClassSection(c.getString(5));

                    // Add this object into the ArrayList myList
                    myList.add(lde);

                } while (c.moveToNext());
            }
        }


        db.close();

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG)
                .show();
        e.printStackTrace();
    }
}    

@Override
public void onClick(View v) {
    SaveStudentsClasstestMarks();
}    

private void SaveStudentsClasstestMarks() {


        ArrayList<String> mannschaftsnamen = new ArrayList<String>();

            for (int i = 0; i < lvStudent.getAdapter().getCount(); i++) {
                View viewTelefone = lvStudent.getChildAt(i);
                TextView et = (TextView) viewTelefone.findViewById(R.id.txt_studentId);
                TextView et1 = (TextView) viewTelefone.findViewById(R.id.txt_studentName);
                EditText edtMarks = (EditText) viewTelefone.findViewById(R.id.class_test_marks);

                if (et != null) {
                    mannschaftsnamen.add(String.valueOf(et.getText().toString()));
                    String enrollId = String.valueOf(et.getText().toString());
                    String studentName = String.valueOf(et1.getText().toString());
                    String marks = edtMarks.getText().toString();
                    String remarks = "";
                    if (marks.isEmpty()) {
                        marks = "0";
                    }

                    long c = db.class_test_mark_insert(enrollId, homeWorkId, serverHomeWorkId, marks, remarks, "Active", PreferenceStorage.getUserId(getApplicationContext()), formattedServerDate, PreferenceStorage.getUserId(getApplicationContext()), formattedServerDate, "NS");
                    if (c == -1) {
                        Toast.makeText(getApplicationContext(), "Error while marks add...",
                                Toast.LENGTH_LONG).show();
                    }
                    Log.v("ypgs", String.valueOf(et.getText()));
                }
            }

            db.updateClassTestHomeWorkMarkStatus(homeWorkId);
            Toast.makeText(getApplicationContext(), "Class Test - " + title + ".\n Marks Updated Successfully...",
                    Toast.LENGTH_LONG).show();

            finish();
}    
}

ListView还有其他选择吗?

1 个答案:

答案 0 :(得分:1)

public MyViewHolder(View item) {
    txtStudentId = (TextView) item.findViewById(R.id.txt_studentId);
    txtStudentName = (TextView) item.findViewById(R.id.txt_studentName);
    edtStudentMarks = (EditText) item.findViewById(R.id.class_test_marks);
}

从getview中删除此行

mViewHolder.edtStudentMarks = (EditText) convertView.findViewById(R.id.class_test_marks);