如何将自定义列表视图中的所选项目添加到同一自定义列表视图中的其他活动

时间:2016-05-29 20:24:03

标签: android listview

正在开发一款让学生参加考试的应用程序,我希望在#34; TakeAttendance"当我点击" ViewAttendance"在同一自定义列表视图上。请问我该怎么做?

这是我的代码...... 自定义视图  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

ListAdapter enter code here公共类StudentListAdapter1扩展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;

}

}

出席活动 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);
        }
    });


}

}

查看出勤活动 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 个答案:

答案 0 :(得分:0)

起初我认为你应该在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);
    }
.......

现在回答您的问题,我认为您需要在lvStudentlist.setOnItemClickListener方法中将所选项目添加到数组中,然后将其作为Intent extra发送到您的下一个活动。您可以将String []作为Intent extra发送,这样您就不需要使用Bundle。