Android - 我的ListViewAdpter类,其中getCount始终返回0

时间:2017-06-03 11:23:12

标签: java android listview baseadapter listviewitem

我正在使用Listview来传递后端的值。我的问题是每当我运行我的代码时,我总是在getcount方法中得到arraylist的大小为零。 我有搜索但没有正确理解

任何人都可以帮助我吗朋友

我的Listview适配器类是

private class ListViewAdaptar extends BaseAdapter {
    private LayoutInflater _inflater;
    private List<Student_Location_Model> _list;
    private Context _context;
    String enroll_no;
    String driver_id, hash;
    //CharSequence[] show_data = {"Home Pick", "Home Drop", "School Pick", "School Drop", "Absent", "Other"};
    CharSequence[] show_data1 = {"School Drop", "School Pick", "Home Drop", "Absent"};
    String cat = "notselected";
    String changed_cat, remark = "remark";

    public ListViewAdaptar(Activity context, List<Student_Location_Model> lst) {
        if (context != null && lst != null) {
            this._context = context;
            _list = lst;
            _inflater = LayoutInflater.from(context);
        }
    }

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

    @Override
    public Object getItem(int i) {
        return _list.get(i);
    }

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

    @Override
    public int getViewTypeCount() {
        return getCount();
    }

    @Override
    public View getView(final int position, View listItems, ViewGroup parent) {
        final ViewHolder holder;
        SharedPreferences sharedPreferences = _context.getSharedPreferences(_context.getString(R.string.BUS_DETAILS), Context.MODE_PRIVATE);
        hash = sharedPreferences.getString(_context.getString(R.string.HASH), "");
        driver_id = sharedPreferences.getString(_context.getString(R.string.DRIVER_ID), "");
        //     if (listItems == null) {

        listItems = _inflater.inflate(R.layout.listview_adapter_design, null);

        holder = new ViewHolder();
        holder.txt_loc_id = (TextView) listItems.findViewById(R.id.location_id);
        holder.txt_enroll_no = (TextView) listItems.findViewById(R.id.textview_student_enroll);
        holder.txt_student_name = (TextView) listItems.findViewById(R.id.textview_student_name);
        holder.select_data = (TextView) listItems.findViewById(R.id.select_data);
        holder.checkbox_select_one = (CheckBox) listItems.findViewById(R.id.checkbox_select_one);

        final Student_Location_Model locationModel = _list.get(position);
        holder.txt_student_name.setText(locationModel.getStudent_name());
        holder.txt_enroll_no.setText(locationModel.getEnroll_num());
        holder.txt_loc_id.setText(locationModel.getLocation_name());
        holder.checkbox_select_one.setChecked(locationModel.isItemCheck());
        holder.select_data.setText(locationModel.getStudent_track_status());

        holder.checkbox_select_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (((CheckBox) view).isChecked()) {
                    Student_Location_Model model = _list.get(position);
                    model.setItemCheck(true);
                    notifyDataSetChanged();
                } else {
                    Student_Location_Model model = _list.get(position);
                    model.setItemCheck(false);
                    notifyDataSetChanged();
                }
                String selected_student = "";
                for (int i = 0; i < _list.size(); i++) {
                    Student_Location_Model locationModel1 = _list.get(i);
                    if (locationModel1.isItemCheck()) {
                        selected_student = locationModel1.getEnroll_num() + " , " + selected_student;
                    }
                }
                checkbox_select_all.setChecked(false);
                Log.e("String of Students :- ", selected_student);
                selected_enroll_no.setText(selected_student);
            }
        });

        if (holder.checkbox_select_one.isChecked()) {
            SharedPreferences preferences = New_Student_Details_Location.this.getSharedPreferences(getString(R.string.LATEST_SELECTION), MODE_PRIVATE);
            String new_selection = preferences.getString(getString(R.string.SELECTED_VALUE), "");
            holder.select_data.setText(new_selection);
            if (new_selection.equalsIgnoreCase("")) {
                holder.select_data.setText("Select");
            }
        }
        holder.select_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final AlertDialog.Builder builder;
                builder = new AlertDialog.Builder(_context);
                builder.setTitle("Select One");
                builder.setSingleChoiceItems(show_data1, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        switch (i) {
                            case 0:
                                cat = (String) show_data1[i];
                                break;
                            case 1:
                                cat = (String) show_data1[i];
                                //holder.select_data.setText(cat);
                                break;
                            case 2:
                                cat = (String) show_data1[i];
                                // holder.select_data.setText(cat);
                                break;
                            case 3:
                                cat = (String) show_data1[i];
                                // holder.select_data.setText(cat);
                                break;
                        }
                    }
                });
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                  /*  if (cat.equalsIgnoreCase("Home Pick")) {
                        holder.select_data.setText(cat);
                        //newcat = holder.select_data.getText().toString();
                        changed_cat = cat.replace("Home Pick", "home_pickup");
                        enroll_no = holder.txt_enroll_no.getText().toString();
                        Toast.makeText(_context, "You have Selected " + cat, Toast.LENGTH_SHORT).show();
                        new BackgroundJob().execute();
                        cat = "notselected";
                    }*/
                        if (cat.equalsIgnoreCase("Home Drop")) {
                            holder.select_data.setText(cat);
                            //newcat = holder.select_data.getText().toString();
                            changed_cat = cat.replace("Home Drop", "home_drop");
                            enroll_no = holder.txt_enroll_no.getText().toString();
                            StyleableToast.makeText(_context, "You Selected " + cat, Toast.LENGTH_SHORT, R.style.StyledToast).show();
                            new BackgroundJob().execute();
                            cat = "notselected";
                        } else if (cat.equalsIgnoreCase("School Pick")) {
                            holder.select_data.setText(cat);
                            //newcat = holder.select_data.getText().toString();
                            changed_cat = cat.replace("School Pick", "school_pickup");
                            enroll_no = holder.txt_enroll_no.getText().toString();
                            StyleableToast.makeText(_context, "You Selected " + cat, Toast.LENGTH_SHORT, R.style.StyledToast).show();
                            new BackgroundJob().execute();
                            cat = "notselected";
                        } else if (cat.equalsIgnoreCase("School Drop")) {
                            holder.select_data.setText(cat);
                            //newcat = holder.select_data.getText().toString();
                            changed_cat = cat.replace("School Drop", "school_drop");
                            enroll_no = holder.txt_enroll_no.getText().toString();
                            StyleableToast.makeText(_context, "You Selected " + cat, Toast.LENGTH_SHORT, R.style.StyledToast).show();
                            new BackgroundJob().execute();
                            cat = "notselected";
                        } else if (cat.equalsIgnoreCase("Absent")) {
                            holder.select_data.setText(cat);
                            //newcat = holder.select_data.getText().toString();
                            changed_cat = cat.replace("Absent", "absent");
                            enroll_no = holder.txt_enroll_no.getText().toString();
                            StyleableToast.makeText(_context, "You Selected " + cat, Toast.LENGTH_SHORT, R.style.StyledToast).show();
                            new BackgroundJob().execute();
                            cat = "notselected";
                        }
                   /* else if (cat.equalsIgnoreCase("Other")) {
                        holder.select_data.setText(cat);
                        //newcat = holder.select_data.getText().toString();
                        changed_cat = cat.replace("Other", "other");
                        enroll_no = holder.txt_enroll_no.getText().toString();
                        AlertDialog.Builder builder1 = new AlertDialog.Builder(_context);
                        final EditText edt_remark = new EditText(_context);
                        builder1.setTitle("Reason");
                        builder1.setMessage("Please enter a valid reason");
                        builder1.setView(edt_remark);
                        builder1.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                remark = edt_remark.getText().toString();
                                new BackgroundJob().execute();
                            }
                        });
                        builder1.show();
                        cat = "notselected";
                    }*/
                        else if (cat.equalsIgnoreCase("notselected")) {
                            StyleableToast.makeText(_context, "Please Select One", Toast.LENGTH_SHORT, R.style.StyledToast_one).show();
                        }
                    }
                });
                builder.setCancelable(false);
                builder.show();

            }
        });
        listItems.setTag(holder);
        listItems.setTag(R.id.checkbox_select_one, holder.checkbox_select_one);

        holder.checkbox_select_one.setTag(position);
        return listItems;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    private class ViewHolder {
        TextView txt_loc_id;
        TextView txt_enroll_no;
        TextView txt_student_name;
        TextView select_data;
        CheckBox checkbox_select_one;
    }

    private class BackgroundJob extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... strings) {
            return JsonParser.makeHttpUrlConnectionRequest(API.Update_Student_Details, dataparams());
        }

        private List<NameValuePair> dataparams() {
            ArrayList<NameValuePair> parameters = new ArrayList<>();
            parameters.add(new BasicNameValuePair("hash", hash));
            parameters.add(new BasicNameValuePair("driver_id", driver_id));
            parameters.add(new BasicNameValuePair("enroll_num", enroll_no));
            parameters.add(new BasicNameValuePair("track_status", changed_cat));
            parameters.add(new BasicNameValuePair("remark", remark));
            Log.e("Parematers", ":" + parameters);
            return parameters;
        }

        @Override
        protected void onPostExecute(String httpResponse) {
            JSONObject responseParameters;
            if (httpResponse == null) {
                StyleableToast.makeText(_context, "No Internet Connection", Toast.LENGTH_SHORT, R.style.StyledToast_one).show();
            } else {
                try {
                    responseParameters = new JSONObject(httpResponse);
                    String status = responseParameters.getString("status");
                    if (status.equalsIgnoreCase("success")) {
                        StyleableToast.makeText(_context, "Saved Successfully.", Toast.LENGTH_SHORT, R.style.StyledToast).show();

                    } else {
                        StyleableToast.makeText(_context, "Status Already Updated", Toast.LENGTH_SHORT, R.style.StyledToast_one).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

我的用于设置适配器的类

public class Student_Location_Model {
    String location_id, location_name, enroll_num, student_name, student_track_status;
    boolean isItemCheck;


    public Student_Location_Model(String location_id, String location_name,
                                  String enroll_num, String student_name,
                                  boolean isItemCheck, String student_track_status) {
        this.location_id = location_id;
        this.location_name = location_name;
        this.enroll_num = enroll_num;
        this.student_name = student_name;
        this.isItemCheck = isItemCheck;
        this.student_track_status = student_track_status;

    }
    public String getStudent_track_status() {
        return student_track_status;
    }

    public void setStudent_track_status(String student_track_status) {
        this.student_track_status = student_track_status;
    }

    public boolean isItemCheck() {
        return isItemCheck;
    }

    public void setItemCheck(boolean itemCheck) {
        isItemCheck = itemCheck;
    }

    public String getLocation_id() {
        return location_id;
    }

    public void setLocation_id(String location_id) {
        this.location_id = location_id;
    }

    public String getLocation_name() {
        return location_name;
    }

    public void setLocation_name(String location_name) {
        this.location_name = location_name;
    }

    public String getEnroll_num() {
        return enroll_num;
    }

    public void setEnroll_num(String enroll_num) {
        this.enroll_num = enroll_num;
    }

    public String getStudent_name() {
        return student_name;
    }

    public void setStudent_name(String student_name) {
        this.student_name = student_name;
    }
}

0 个答案:

没有答案