jeudi 9 mars 2017

How to use Checkbox in ListView with JSON data

I'm newbie Android developer. I want to someone help me. Now, I can call JSON data to my ListView. I did to import checkbox in to ListView. But It's not work. When i am selected on some checkbox just will have other checkbox are selected such as below example event.

checkbox name

X A

_ B

_ C

_ D

_ E

_ F

__________________ << bottom screen

X G

_ H

_ I

When i am selected checkbox "A". Just will has another one checkbox was selected too (checkbox G).

  1. I need to know how to resolve this case.
  2. How to do if i want to show all checkbox are selected after click button submit.

Sorry for my bad English language skill. Please help me sir.

SearchActivity.java

package com.example.rattapongt.rjp_b;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.rattapongt.rjp_b.Date.myCalendarView;
import com.example.rattapongt.rjp_b.Util.Config;
import com.example.rattapongt.rjp_b.Util.RequestHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class SearchActivity extends AppCompatActivity implements View.OnClickListener {

    private ProgressDialog loading;
    private String JSON_STRING;

    ListView listView;
    TextView tvDate, tvDateHide;
    EditText editTextId, editTextId2;
    ImageView ivSearch;
    Button btnDate, btnReject;

    private int mYear;
    private int mMonth;
    private int mDay;

    private String mYearCheck;

    static final int CALENDAR_VIEW_ID = 1;


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

        listView = (ListView) findViewById(R.id.listView);

        editTextId = (EditText) findViewById(R.id.editTextId);
        editTextId2 = (EditText) findViewById(R.id.editTextId2);

        ivSearch = (ImageView) findViewById(R.id.ivSearch);

        tvDate = (TextView) findViewById(R.id.tvDate);
        tvDateHide = (TextView) findViewById(R.id.tvDateHide);

        btnDate = (Button) findViewById(R.id.btnDate);
        btnReject = (Button) findViewById(R.id.btnReject);

        ivSearch.setOnClickListener(this);
        btnReject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Intent intent = new Intent(SearchActivity.this, RejectActivity.class);
                //startActivity(intent);
            }
        });

        editTextId.setText("1992");
        editTextId2.setText("PCB116A731 ");

        // add a click listener to the button
        btnDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(SearchActivity.this, myCalendarView.class);
                startActivityForResult(intent, CALENDAR_VIEW_ID);

            }
        });


    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case CALENDAR_VIEW_ID:
                if (resultCode == RESULT_OK) {

                    Bundle bundle = data.getExtras();
                    tvDate.setText(bundle.getString("dateSelected1"));
                    tvDateHide.setText(bundle.getString("dateSelected"));
                    break;
                }
        }
    }



    private void getJSON() {
        class GetJSON extends AsyncTask<Void, Void, String> {

            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(SearchActivity.this, "Fetching Data", "Wait...", false, false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                JSON_STRING = s;

              showEmployee();
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequest(Config.URL_GET_ALL);

                return s;
            }
        }
        GetJSON gj = new GetJSON();
        gj.execute();

    }


    private void showEmployee() {
        JSONObject jsonObject = null;
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);

            for (int i = 0; i < result.length(); i++) {
                JSONObject jo = result.getJSONObject(i);
                String Ta = jo.getString(Config.TAG_A);
                String Tb = jo.getString(Config.TAG_B);
                String Tc = jo.getString(Config.TAG_C);
                String Td = jo.getString(Config.TAG_D);
                String Te = jo.getString(Config.TAG_E);
                String Tf = jo.getString(Config.TAG_F);
                String Tg = jo.getString(Config.TAG_G);


                HashMap<String, String> employees = new HashMap<>();
                employees.put(Config.TAG_A, Ta);
                employees.put(Config.TAG_B, Tb);
                employees.put(Config.TAG_C, Tc);
                employees.put(Config.TAG_D, Td);
                employees.put(Config.TAG_E, Te);
                employees.put(Config.TAG_F, Tf);
                employees.put(Config.TAG_G, Tg);

                list.add(employees);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(
                SearchActivity.this, list, R.layout.item_list,
                new String[]{Config.TAG_A, Config.TAG_B, Config.TAG_C, Config.TAG_D, Config.TAG_E, Config.TAG_F, Config.TAG_G},
                new int[]{R.id.invoiceNo, R.id.code, R.id.name, R.id.itemNo, R.id.poNo, R.id.sq, R.id.qty});

        listView.setAdapter(adapter);
    }

    private void getData() {

        loading = ProgressDialog.show(this, "Please wait...", "Fetching...", false, false);

        //String url = Config.DATA_URL + editTextId.getText().toString().trim();
        String url = Config.DATA_URL + "&strKeyword=" + editTextId.getText().toString().trim()
                + "&strKeyword3=" + tvDateHide.getText().toString().trim();

        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                loading.dismiss();
                showJSON(response);

            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //Toast.makeText(SearchActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                        loading.dismiss();
                        lostConnection();

                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }



    private void showJSON(String response) {
        JSONObject jsonObject = null;
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        try {
            JSONObject jsonObjectSearch = new JSONObject(response);
            JSONArray result = jsonObjectSearch.getJSONArray(Config.JSON_ARRAY);

            //For map values for JSON to android
            /*
            JSONObject collegeData = result.getJSONObject(0);
            name = collegeData.getString(Config.KEY_NAME);
            address = collegeData.getString(Config.KEY_ADDRESS);
            vc = collegeData.getString(Config.KEY_VC);
            */

            for (int i = 0; i < result.length(); i++) {
                JSONObject jo = result.getJSONObject(i);
                String Ka = jo.getString(Config.TAG_A);
                String Kb = jo.getString(Config.TAG_B);
                String Kc = jo.getString(Config.TAG_C);
                String Kd = jo.getString(Config.TAG_D);
                String Ke = jo.getString(Config.TAG_E);
                String Kf = jo.getString(Config.TAG_F);
                String Kg = jo.getString(Config.TAG_G);

                HashMap<String, String> employees = new HashMap<>();
                employees.put(Config.TAG_A, Ka);
                employees.put(Config.TAG_B, Kb);
                employees.put(Config.TAG_C, Kc);
                employees.put(Config.TAG_D, Kd);
                employees.put(Config.TAG_E, Ke);
                employees.put(Config.TAG_F, Kf);
                employees.put(Config.TAG_G, Kg);
                list.add(employees);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        //That will be show information to textView.
        /*
        if (!name.toString().equals("null")) {
            //textViewResult.setText("Name:\t" + name + "\nDesignation:\t" + address + "\nSalary:\t" + vc);
            Toast.makeText(this, response, Toast.LENGTH_LONG).show();
        } else {
            textViewResult.setText("Data not found!");
        }
        //textViewResult.setText("Name:\t" + name + "\nDesignation:\t" + address + "\nSalary:\t" + vc);
*/

        ListAdapter adapter = new SimpleAdapter(
                SearchActivity.this, list, R.layout.item_list,
                new String[]{Config.KEY_A, Config.KEY_B, Config.KEY_C, Config.KEY_D, Config.KEY_E, Config.KEY_F, Config.KEY_G},
                new int[]{R.id.invoiceNo, R.id.code, R.id.name, R.id.itemNo, R.id.poNo, R.id.sq, R.id.qty});

        //Toast.makeText(this, response, Toast.LENGTH_LONG).show();
        listView.setAdapter(adapter);


    }



   

}

CustomAdapter.java

package com.example.rattapongt.rjp_b.Util;

/**
 * Created by Rattapongt on 2/23/2017.
 */

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

import com.example.rattapongt.rjp_b.R;

public class CustomAdapter extends BaseAdapter {
    Context mContext;
    String[] strName;
    String[] strName1;
    String[] strName2;
    String[] strName3;
    String[] strName4;
    String[] strName5;
    String[] strName6;
    int[] resId;

    public CustomAdapter(Context context, String[] strName, String[] strName1, String[] strName2, String[] strName3, String[] strName4, String[] strName5, String[] strName6) {
        this.mContext = context;
        this.strName = strName;
        this.strName1 = strName1;
        this.strName2 = strName2;
        this.strName3 = strName3;
        this.strName4 = strName4;
        this.strName5 = strName5;
        this.strName6 = strName6;
        this.resId = resId;
    }

    public int getCount() {
        return strName.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }


    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater mInflater =
                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (view == null)
            view = mInflater.inflate(R.layout.item_list, parent, false);

        TextView textView = (TextView) view.findViewById(R.id.textView);
        textView.setText(strName[position]);

        TextView textView1 = (TextView) view.findViewById(R.id.textView1);
        textView1.setText(strName1[position]);

        TextView textView2 = (TextView) view.findViewById(R.id.textView2);
        textView2.setText(strName2[position]);

        TextView textView3 = (TextView) view.findViewById(R.id.textView3);
        textView3.setText(strName3[position]);

        TextView textView4 = (TextView) view.findViewById(R.id.textView4);
        textView4.setText(strName4[position]);

        TextView textView5 = (TextView) view.findViewById(R.id.textView5);
        textView5.setText(strName5[position]);

        TextView textView6 = (TextView) view.findViewById(R.id.textView6);
        textView6.setText(strName6[position]);


        return view;
    }
}



Aucun commentaire:

Enregistrer un commentaire