ListView和CustomAdapter

时间:2017-03-18 18:56:36

标签: android listview custom-adapter

我有两个类,第一个包含列表视图,第二个包含扩展数组适配器,我有一个文本视图和一个广播组(包含3个单选按钮),每个项目,所以我想控制检查哪个单选按钮如果选中具有文本“存在”的单选按钮,则每个项目都计数。

这是包含listView的类。

public class StartAttendance extends ListActivity implements View.OnClickListener {
    private ListView listOfTakeAtt;
    private DBManager dbManager;
    private String studentSubjectId;
    private Button save;
    private Button cancel;
    List<UserModel> students;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_attendance);

        save=(Button)findViewById(R.id.save);

        cancel=(Button) findViewById(R.id.cancel);

        listOfTakeAtt=(ListView)findViewById(R.id.listOfTakingAttendance);


        Intent intent=getIntent();
        studentSubjectId=intent.getStringExtra("studentSubjectId");


        dbManager=new DBManager(StartAttendance.this);
        dbManager.open();
        ArrayList arrayList1=dbManager.getAllStudentsId(Integer.valueOf(studentSubjectId));//Get the ids from the database.
        ArrayList arrayList2=dbManager.getAllStudentsName(Integer.valueOf(studentSubjectId));//Get the names from database.



        students=new ArrayList<>();
        //Set all ids and names into userModel.
        for(int i=0;i<arrayList1.size();i++){
            students.add(new UserModel(false,(Integer)arrayList1.get(i), (String) arrayList2.get(i)));
        }



        CustomLayoutOfTakingAttendance adapter=new CustomLayoutOfTakingAttendance(StartAttendance.this,students);
        listOfTakeAtt.setAdapter(adapter);

        listOfTakeAtt.setDividerHeight(17);


        cancel.setOnClickListener(this);
        save.setOnClickListener(this);


        listOfTakeAtt.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

    }


    @Override
    public void onClick(View v){
            switch (v.getId()){
                //When save button is clicked i want to see present radioButton is checked or not for each item.
                //if checked save current date into database for this id.
        case R.id.save:
            SimpleDateFormat DateFormat=new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());

            Date d=new Date();
            String date=DateFormat.format(d);
            for(int i=0;i<students.size();i++)
            {
                UserModel model = students.get(i);

               if(model.isSelected){
                   dbManager=new DBManager(StartAttendance.this);
                   dbManager.open();

                   dbManager.UpdateStudentDate(model.getStudentId(),date);
               }
            }
            break;


        case R.id.cancel:

                break;

        }
    }
}

这是扩展BaseAdapter的类。

public class CustomLayoutOfTakingAttendance extends BaseAdapter{
        Activity context;
        DBManager dbManager;
        List<UserModel> students;
        LayoutInflater inflater;

        public CustomLayoutOfTakingAttendance(Activity context,List<UserModel> students) {

            this.context=context;
            this.students=students;
            inflater = context.getLayoutInflater();

        }

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

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

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

    @Override
        public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;

        if (convertView == null){

            convertView = inflater.inflate(R.layout.custom_layout_of_taking_attendance, parent, false);

            holder = new ViewHolder();

            holder.ID = (TextView) convertView.findViewById(R.id.id);
            holder.NAME = (TextView) convertView.findViewById(R.id.stName);
           holder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radioGroup);

            convertView.setTag(holder);
        } else holder = (ViewHolder) convertView.getTag();


        UserModel model = students.get(position);

        int checkedRB=holder.radioGroup.getCheckedRadioButtonId();

        if (checkedRB==R.id.present){
            model.setSelected(true);
        }

            holder.ID.setText(model.getStudentId());
            holder.NAME.setText(model.getStudentName());


        return convertView;
    }
        class ViewHolder{
            TextView ID;
            TextView NAME;
            RadioGroup radioGroup;
        }
}

这是UserModel类

public class UserModel {

    boolean isSelected;
    int id;
    String name;


    public UserModel(boolean isSelected, int id,String name) {
        this.isSelected = isSelected;
        this.id = id;
        this.name=name;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    public int getStudentId() {
        return id;
    }

    public void setStudentId(int id) {
        this.id = id;
    }

    public String getStudentName() {
        return name;
    }

    public void setStudentName(String name) {
        this.name = name;
    }

}

这是我对每个项目的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/id"
        android:layout_marginRight="50dp"
        android:minWidth="50dp"
        android:visibility="invisible"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:layout_weight="1"
        android:layout_marginBottom="15dp"
        android:textSize="25dp"
        android:textColor="#000"
        android:layout_marginLeft="10dp"
        />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/radioGroup"
        android:orientation="horizontal"
        android:layout_below="@id/name"
       android:layout_marginBottom="20dp"
        android:layout_marginLeft="10dp"
        >

        <RadioButton
            android:text="PRESENT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/present"
            android:layout_weight="1"

            android:background="@android:color/holo_green_light" />

        <RadioButton
            android:text="ABSENT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/absent"
            android:layout_weight="1"
            android:background="@android:color/holo_red_dark" />

        <RadioButton
            android:text="ICAZE"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/icaze"
            android:background="@android:color/holo_orange_light" />
    </RadioGroup>
</RelativeLayout>

这是包含列表视图的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eee"
    >

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/date"
        android:id="@+id/listOfTakingAttendance"
        android:background="@android:color/background_light" />

    <Button
        android:text="CANCEL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="43dp"
        android:layout_marginStart="43dp"
        android:id="@+id/cancel"
        style="@style/Widget.AppCompat.Button.Colored"
        android:background="@android:color/holo_red_dark"
        android:textSize="18dp"
        android:fontFamily="sans-serif-condensed"
        />

    <Button
        android:text="SAVE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/save"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/date"
        android:layout_toEndOf="@+id/date"
        android:layout_marginLeft="41dp"
        android:layout_marginStart="41dp"
        style="@style/Widget.AppCompat.Button.Colored"
        android:background="@android:color/holo_green_light"
        android:textSize="18dp"
        android:fontFamily="sans-serif-condensed"
        />


</RelativeLayout>

0 个答案:

没有答案