1
votes

Am developing an app that take student attendance, i want to get the list of checked the names on "TakeAttendance" when i click a "ViewAttendance" on same custom listview . Please how can i do that?

here is my codes... The custom view enter code here

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_firstname"
    android:text="Firstname"
    android:textSize="11dp"
    android:ellipsize="start"
    android:lines="3"
    android:textColor="#000"
    android:textStyle="bold"
    android:gravity="center"
    android:paddingLeft="10dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Lastname"
    android:lines="3"
    android:textSize="11dp"
    android:textColor="#000"
    android:textStyle="bold"
    android:gravity="center"
    android:paddingLeft="10dp"
    android:id="@+id/tv_lastname" />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_surname"
    android:text="Surname"
    android:lines="3"
    android:textSize="11sp"
    android:textColor="#000"
    android:textStyle="bold"
    android:gravity="center"
    android:paddingRight="80dp"
    android:paddingLeft="10dp" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingStart="20dp"
    android:id="@+id/checkBox"
    android:checked="false"
    android:focusable="false"
    android:background="@color/white" />





The Listview

enter code here

The ListAdapter enter code herepublic class StudentListAdapter1 extends BaseAdapter { private Context mContext; private List mStudentList;

//Constructor

public StudentListAdapter1(Context mContext, List<StudentList> mStudentList) {
    this.mContext = mContext;
    this.mStudentList = mStudentList;
}

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

@Override
public Object getItem(int position) {
    return mStudentList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = View.inflate(mContext, R.layout.student_take, null);
    TextView tvReg_no = (TextView) v.findViewById(R.id.tv_reg_no);
    TextView tvFirstname = (TextView) v.findViewById(R.id.tv_firstname);
    TextView tvLastname = (TextView) v.findViewById(R.id.tv_lastname);
    TextView tvSurname = (TextView) v.findViewById(R.id.tv_surname);
    //Set text for TextView
    tvReg_no.setText(mStudentList.get(position).getReg_no());
    tvFirstname.setText(mStudentList.get(position).getFirstname());
    tvLastname.setText(mStudentList.get(position).getLasttname());
    tvSurname.setText(mStudentList.get(position).getSurname());

    //Save product id to tag
    v.setTag(mStudentList.get(position).getId());

    return v;

}

}

The Attendance Activity enter code here protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_student_take100);

    lvStudentlist= (ListView) findViewById(R.id.listview_studentlist);

    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();
            ArrayList<String> selectedItems = new ArrayList<>();

            for (int i = 0; i < checked.size(); i++) {

                // Item position in adapter

                position = checked.keyAt(i);

                // Add names if it is checked i.e.) == TRUE!

                if (checked.valueAt(i))

                    selectedItems.add((String) adapter.getItem(position));

            }

            String[] outputStrArr = new String[selectedItems.size()];

            for (int i = 0; i < selectedItems.size(); i++) {

                outputStrArr[i] = selectedItems.get(i);
            }

            Intent intent = new Intent(getApplicationContext(),

                    ViewAttendanceActivity.class);

            // Create a bundle object

            Bundle b = new Bundle();

            b.putStringArray("selectedItems", outputStrArr);

            // Add the bundle to the intent.

            intent.putExtras(b);

            // start the ResultActivity
            startActivity(intent);
        }
    });


}

}

The View Attendance Activity enter code here protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_attendance);

    Bundle b = getIntent().getExtras();
    String[] resultArr = b.getStringArray("selectedItems");
    lvStudentlist= (ListView) findViewById(R.id.listview_studentlist);


    adapter = new StudentListAdapter(getApplicationContext(), mStudentList);
    lvStudentlist.setAdapter(adapter);

}

}

1

1 Answers

0
votes

At first I think you should reuse your items in listview

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    if (convertView != null) {
        v = convertView;
    } else {
        v = View.inflate(mContext, R.layout.student_take, null);
    }
.......

now answer for your question, I think you need to add your selected items to array in lvStudentlist.setOnItemClickListener method and then send it as Intent extra to your next activity. You can send String[] as Intent extra so you have no need to use Bundle.