【问题标题】:How to get selected items in a custom listview to another activity in same custom listview如何将自定义列表视图中的选定项目获取到同一自定义列表视图中的另一个活动
【发布时间】:2016-09-27 15:07:27
【问题描述】:

我正在开发一个可以让学生出勤的应用程序,当我在同一个自定义列表视图上单击“ViewAttendance”时,我想获得“TakeAttendance”上已检查姓名的列表。请问我该怎么做?

这是我的代码... 自定义视图 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 herepublic 类 StudentListAdapter1 扩展 BaseAdapter { 私有上下文 mContext; 私人名单 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);

}

}

【问题讨论】:

    标签: android listview


    【解决方案1】:

    起初我认为你应该在列表视图中重复使用你的项目

    @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。

    【讨论】:

    • 我不明白你。有没有简单的方法。
    • 使用类似这样的 Intent intent = new Intent(CurrentActivity.this, NextActivity.class); intent.putExtra("selectedItems", outputStrArr);开始活动(意图);
    • 对不起,我是这个 android 应用程序的新手......我怎样才能在 ViewAttendanceActivity 中获取 Extra 并将其放入列表视图中。
    • 简单的String[] a=getIntent().getStringArrayExtra("selectedItems");
    猜你喜欢
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多