java - 在 ListView 中单击复选框时,值未添加到数组列表中。

标签 java android listview arraylist

我有一个 DisplayStudentName.java 文件,它是我的主要 Activity 文件。有一个模型类用于获取和设置数据,还有一个 MyCustomAdapter.java 文件,它扩展了 ArrayAdapter 以实现 ListView 功能。

复选框被单击,但值未添加到数组列表中(命名数据;字符串类型)。

  1. DisplayStudentName.java

       public class DisplayStudentNames extends AppCompatActivity {
       String myJSON;
       private static final String TAG_RESULTS = "result";
       private static final String TAG_ROLL = "RollNo";
       private static final String TAG_NAME = "Name";
       JSONArray peoples = null;
       ListView list;
       ArrayList<String> checkedValue;
       Button b1;
       MyCustomAdapter dataAdapter = null;
       ArrayList<Student> personList=null;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_display_student_names);
       final Spinner sbranch = (Spinner) findViewById(R.id.branch);
       final Spinner ssemester = (Spinner) findViewById(R.id.semester);
       final Spinner ssubject = (Spinner) findViewById(R.id.edit_subject);
       //String branch=sbranch.getSelectedItem().toString();
       //String semester=ssemester.getSelectedItem().toString();
       //String subject=ssubject.getSelectedItem().toString();
       String branch = "cs";
       String semester = "7";
       String subject = "Soft Computing";
       b1 = (Button) findViewById(R.id.submit);
       list = (ListView) findViewById(R.id.listView);
    
       new DataFetch().execute(branch, semester, subject);
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
            Student student = (Student) parent.getItemAtPosition(position);
            String Name = student.getName();
            //String s=(String) ((TextView) view.findViewById(R.id.roll)).getText();
            Toast.makeText(getApplicationContext(), "Clicked: " + Name, Toast.LENGTH_SHORT).show();
    
        }
    });
    //submitAttendance();
    }
    
    class DataFetch extends AsyncTask<String, String, String> {
    ArrayList<NameValuePair> params;
    private ProgressDialog pDialog;
    private String url_fetch_data = "http://192.168.1.4/AttendanceApp/fetch_data.php";
    InputStream is = null;
    String res = "";
    
    protected void onPreExecute() {
        super.onPreExecute();
    }
    
    protected String doInBackground(String... args) {
        String branch = args[0];
    
        String semester = args[1];
        String subject1 = args[2];
    
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url_fetch_data);
            params = new ArrayList<NameValuePair>(3);
            params.add(new BasicNameValuePair("branch", branch));
            params.add(new BasicNameValuePair("semester", semester));
            params.add(new BasicNameValuePair("subject", subject1));
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
    
            StringBuilder total = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                total.append(line);
            }
            String result = total.toString();
            return result;
        } catch (Exception e1) {
            res = "error:" + e1.getMessage().toString();
            e1.printStackTrace();
        }
        return res;
    }
    
    protected void onPostExecute(String httpResponse) {
        myJSON = httpResponse;
        showList();
    }
    }
    
    protected void showList() {
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray("result");
    
        personList = new ArrayList<Student>();
    
        for (int i = 0; i < peoples.length(); i++) {
            Student student = new Student();
            JSONObject c = peoples.getJSONObject(i);
            String roll = c.getString(TAG_ROLL);
            String name = c.getString(TAG_NAME);
            student.setName(name);
            student.setRollNo(roll);
            personList.add(student);
        }
    
        dataAdapter = new MyCustomAdapter(this, R.layout.list_item, personList);
        list.setAdapter(dataAdapter);
    
    
    
    } catch (JSONException e) {
        e.printStackTrace();
    }
    }
    
  2. MyCustomAdapter.java

    public class MyCustomAdapter extends ArrayAdapter {
    public ArrayList<Student> personList=null;
    private Context context=null;
    private static LayoutInflater inflater=null;
    private ArrayList<String> data=null;
    private View vi;
    ViewHolder holder=null;
    
    public MyCustomAdapter(Context context, int resource, ArrayList<Student> personList) {
    super(context, resource, personList);
    this.context=context;
    this.personList=personList;
    }
    
    private class ViewHolder{
    TextView roll;
    TextView name;
    CheckBox check;
    }
    
    
    public View getView(int position, View convertView, ViewGroup parent){
    Log.v("ConvertView", String.valueOf(position));
    
    if(convertView==null){
        LayoutInflater vi = (LayoutInflater)context.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.list_item, null);
    
        holder=new ViewHolder();
        holder.roll=(TextView)convertView.findViewById(R.id.roll);
        holder.name=(TextView)convertView.findViewById(R.id.name);
        holder.check=(CheckBox)convertView.findViewById(R.id.checkBox);
        convertView.setTag(holder);
    
    }
    else{
        holder=(ViewHolder)convertView.getTag();
    }
    
    
    Student student=personList.get(position);
    holder.roll.setText(student.getRollno());
    holder.name.setText(student.getName());
    holder.check.setTag(student);
    
    holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                data.add(holder.check.getText().toString());
            } else {
                data.remove(holder.check.getText().toString());
            }
        }
    });
    
    return convertView;
    
    
    }
    

}

  • Student.java - 模型类

    public class Student {
    String rollno;
    String name;
    Boolean checkbox;
    
    public  Student(){
    
    }
    public Student(String rollno, String name, Boolean status){
    super();
    this.rollno=rollno;
    this.name=name;
    this.checkbox=status;
    }
    
    public String getRollno(){
    return rollno;
    }
    
    public void setRollNo(String rollno){
    this.rollno=rollno;
    }
    
    public String getName(){
    return name;
    }
    
    public void setName(String name){
    this.name=name;
    }
    
    public Boolean isCheckbox(){
    return checkbox;
    }
    
    public void setCheckbox(boolean checkbox) {
    this.checkbox= checkbox;
    }
    
  • 请告诉我我的代码有什么问题? }

    最佳答案

    每当您对数据集进行任何更改时,请调用 notifyDataSetChanged()

    因此它会是这样的:

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            data.add(holder.check.getText().toString());
        } else {
            data.remove(holder.check.getText().toString());
        }
        notifyDataSetChanged();
    }
    

    关于java - 在 ListView 中单击复选框时,值未添加到数组列表中。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36555015/

    相关文章:

    java - DynamoDBMappingException : no mapping for HASH key - even though I clearly mapped it

    java - Spring Data 方法名称中的关键字优先级

    android - 开启硬件加速时的模糊效果

    JavaFX ListView lambda

    java - 社交媒体网站上提及的主要网站图片

    Android - LazyAdapter 重叠 View

    android - 通过 GSON 将 jsonString 转换为列表

    flutter - Flutter:在GridView中选择卡

    android - Listview 自动滚动到顶部

    使用 URLClassLoader 加载类时出现 java.lang.ClassNotFoundException