选中一个框,选中多个框

时间:2016-08-28 17:26:25

标签: android listview android-fragments

我正在使用自定义listViewAdapter类,我想在一行中检查一个框,但它检查多行中的多个检查,这不是我的要求。请告诉我如何解决它。

CourseActivity.java

package com.technerdshub.vusocial.Activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.SaveCallback;
import com.technerdshub.vusocial.DBHelper;
import com.technerdshub.vusocial.Models.Course;
import com.technerdshub.vusocial.Models.StudyProgramModel;
import com.technerdshub.vusocial.Models.User;
import com.technerdshub.vusocial.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class CourseActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    ListView list;
    EditText editsearch;
    ArrayList arrayList = new ArrayList();
    CourseViewAdapter adapter;
    Course courseObject;
    boolean isChecked;

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


        List<Course> courseList;
        DBHelper db = new DBHelper(this);
        courseList = db.getAllCourses();

        list = (ListView) findViewById(R.id.listview1);

        for (Course c : courseList) {
            arrayList.add(c);
        }

        adapter = new CourseViewAdapter(this, arrayList);
        adapter.courseActivity = this;
        list.setAdapter(adapter);

        editsearch = (EditText) findViewById(R.id.searchF);
        editsearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
                adapter.filter(text);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        User user = User.getInstance();
        saveUserOnParse(user);

    }

    private void saveUserOnParse(User user) {
        ParseObject parseUser = new ParseObject("UserData");
        parseUser.put("name", user.getName());
        parseUser.put("vuId", user.getVuId());
        parseUser.put("studyProgram", user.getStudyProgram());
        parseUser.put("facebookId", user.getFacebookId());
        parseUser.put("facebookEmail", user.getFacebookEmail());
        parseUser.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
//                    Intent i = new Intent(getContext(), StudentDashboardActivity.class);
////                    i.putExtra("E-mail", "abc");
//                    startActivity(i);
                } else {
                    Toast temp = Toast.makeText(CourseActivity.this, "Data is not saved.", Toast.LENGTH_SHORT);
                    temp.show();
                }
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // check if the request code is same as what is passed  here it is 4

        if (requestCode == 2) {
            courseObject = (Course) data.getExtras().getSerializable("SelectedCourse");
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox);
//         TextView tv = (TextView) v.findViewById(R.id.textView1);
        cb.performClick();
        if (cb.isChecked()) {


//            checkedValue.add(tv.getText().toString());
        } else if (!cb.isChecked()) {
//            checkedValue.remove(tv.getText().toString());

        }
    }


}

CourseViewAdapter.java

    package com.technerdshub.vusocial.Activities;

    import android.content.Context;
    import android.content.Intent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.CheckBox;
    import android.widget.TextView;

    import com.technerdshub.vusocial.Models.Course;
    import com.technerdshub.vusocial.Models.StudyProgramModel;
    import com.technerdshub.vusocial.R;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Locale;

    /**
     * Created by Saroosh on 7/16/2016.
     */
    public class CourseViewAdapter extends BaseAdapter {

        protected CourseActivity courseActivity;
        Context mContext;
        LayoutInflater inflater;
        private List<Course>worldpopulationlist=null;
        private ArrayList<Course> arraylist;

        public CourseViewAdapter(Context context, List<Course>worldpopulationlist){
            mContext = context;
            this.worldpopulationlist = worldpopulationlist;
            inflater = LayoutInflater.from(mContext);
            this.arraylist = new ArrayList<Course>();
            this.arraylist.addAll(worldpopulationlist);
        }

        public class ViewHolder {
            TextView courseCode;
            TextView courseName;
            CheckBox checkBox;

        }

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

        @Override
        public Course getItem(int position) {
            return worldpopulationlist.get(position);
        }

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

        public View getView(final int position, View view, ViewGroup parent) {
            final ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = inflater.inflate(R.layout.c_list_item, null);
                // Locate the TextViews in listview_item.xml

                holder.courseName = (TextView) view.findViewById(R.id.course_name);
                holder.courseCode = (TextView) view.findViewById(R.id.course_code);
                holder.checkBox= (CheckBox) view.findViewById(R.id.checkBox);
                // holder.duration    = (TextView) view.findViewById(R.id.program_duration);

                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            // Set the results into TextViews
            holder.courseName.setText(worldpopulationlist.get(position).getCourseName());

            holder.courseCode.setText(worldpopulationlist.get(position).getCourseCode());

            //holder.duration.setText(worldpopulationlist.get(position).getDuration());

            view.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {


                    // Send single item click data to SingleItemView Class
                    Intent intent = new Intent(mContext, CourseActivity.class);
                    // Pass all data ran
                    intent.putExtra("SelectedCourse", worldpopulationlist.get(position));
                    // Pass all data country
                    courseActivity.setResult(2,intent);

                    courseActivity.finish();
                }
            });

            return view;
        }
        // Filter Class
        public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            worldpopulationlist.clear();
            if (charText.length() == 0) {
                worldpopulationlist.addAll(arraylist);
            }
            else
            {
                for (Course wp : arraylist)
                {
                    if (wp.getCourseName().toLowerCase(Locale.getDefault()).contains(charText))
                    {
                        worldpopulationlist.add(wp);
                    }
                }
            }
            notifyDataSetChanged();
        }
    }

1 个答案:

答案 0 :(得分:0)

在适配器getView方法中写下所有复选框逻辑。这将有效。