vendredi 25 mars 2016

Checkbox seems checked but unchecked in Android

When I'm trying to investigate whether my checkboxes are checked or not (although it's obvious to just see checkboxes), it seems they are all unchecked in Android simulation and real device although some checkboxes look checked.

Here is what I've done. With the "oncreation", I request a post method to recall the data from the server. This information is stored in a couple of arrayLists.

Then according to the data, in the adapter of the listview, specifically in getView function, I make each button checked or unchecked by looking at the data arrayList ( here, priority_result is one of the data ).

It seems some of them are checked by looking at the view of each checkbox. However, if I call them by clicking current_status button ( that I create to investigate the status [id= current_status] ), then everything says it is false ( not checked ).

I am kind of confused because I set the initial value in getView and it looks checked but the real value says it's not. I don't know why this happened.

If you have any hunch, please teach me how to figure this out! The below is my code for getview and the main activity.

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.schedule_list, null);

    holder.mtime=(TextView) rowView.findViewById(R.id.lesson);
    holder.mtime.setText(time_result.get(position).toString());

    holder.mpriority=(TextView) rowView.findViewById(R.id.date);
    holder.mpriority.setText(priority_result.get(position).toString());

    holder.mclasstime=(TextView) rowView.findViewById(R.id.course);
    holder.mclasstime.setText(class_time_result.get(position).toString());

    holder.name = (CheckBox) rowView.findViewById(R.id.checkBox1);
    holder.name.setTag(position);

    //holder.name.setChecked(true);

    Log.i("checkbox output", priority_result.get(position).toString());
    if (priority_result.get(position).toString().equals("1"))
        holder.name.setChecked(true);
    else{
        holder.name.setChecked(false);
    }

    holder.name.setOnCheckedChangeListener(this);
    return rowView;
}

Below is my main java code that includes a post request to the server. I also include notifier to the adapter.

public class ClassApply2 extends ActionBarActivity {

    private CustomAdapterCheckbox adapter;

    private ArrayList<String> timeList = new ArrayList<String>();
    private ArrayList<String> priorityList = new ArrayList<String>();
    private ArrayList<String> classtimeList = new ArrayList<String>();
    private ArrayList<String> useridList = new ArrayList<String>();

    private Button buttonFinal;
    private Button current_status;

    private String auth_token_string;


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


        auth_token_string = settings.getString("token", "").toString();


        String product = getIntent().getStringExtra("product").toString();
        String frequency = getIntent().getStringExtra("frequency").toString();
        String course_1 = getIntent().getStringExtra("course_1").toString();
        String course_2 = getIntent().getStringExtra("course_2").toString();
        String course_3 = getIntent().getStringExtra("course_3").toString();

        new showScheduleTable().execute(auth_token_string);

        final ListView lv=(ListView) findViewById(R.id.listView_classroom);
        adapter = new CustomAdapterCheckbox(this, timeList, priorityList, classtimeList, useridList);
        lv.setAdapter(adapter);

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

        final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        current_status = (Button) findViewById(R.id.current_status);
        current_status.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                for(int i=0;i< timeList.size()  ;i++){
                    if(adapter.mCheckStates.get(i)==true) {
                        Toast.makeText(getApplicationContext(), "checked", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(getApplicationContext(), "unchecked", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

    }

    class showScheduleTable extends AsyncTask<String, String, String> {

        ProgressDialog pd;
        private Exception exception;
        protected void onPreExecute() {

            pd = new ProgressDialog(ClassApply2.this);
            pd.setMessage("loading");
            pd.show();
        }

        protected String doInBackground(String... args) {
            String token = args[0];


            try {
                URL url = new URL("http://ift.tt/1RsrNn4");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                String urlParameters = "token=" + token;
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);

                DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());

                dStream.writeBytes(urlParameters); 
                dStream.flush();
                dStream.close();

                InputStream in = connection.getInputStream();

                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                StringBuffer buffer = new StringBuffer();

                String line = "";
                while ((line = reader.readLine()) != null) {
                    buffer.append(line).append("\n");
                }
                reader.close();
                connection.disconnect();
                JSONObject jsonObj = null;

                jsonObj = new JSONObject(buffer.toString().trim());


                JSONArray items = null;
                items = jsonObj.getJSONArray("item");

                for (int i = 0; i < items.length(); i++) {
                    JSONObject c = items.getJSONObject(i);

                    timeList.add(c.getString("classtime_display"));
                    priorityList.add(c.getString("priority"));
                    classtimeList.add(c.getString("classtime_id"));
                    useridList.add(c.getString("user_id"));
                }

                return "good";

            }
            catch(Exception e) {
                Log.e("ERROR", e.getMessage(), e);
                return null;
            }
        }

        protected void onPostExecute(String response) {
            if(response == null) {
                response = "THERE WAS AN ERROR";
            }
            //progressBar.setVisibility(View.GONE);
            Log.i("INFO", response);

            if (pd != null) {
                pd.dismiss();
            }
            adapter.notifyDataSetChanged();

        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_sungpah, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}




Aucun commentaire:

Enregistrer un commentaire